mirror of
https://github.com/lemeow125/DRF_Template.git
synced 2025-06-28 16:15:44 +08:00
Update webdriver utility functions and add a sample celery task for scraping Google search
This commit is contained in:
parent
0e902b1f04
commit
9e2e32fba8
11 changed files with 193 additions and 25 deletions
0
backend/search_results/__init__.py
Normal file
0
backend/search_results/__init__.py
Normal file
10
backend/search_results/admin.py
Normal file
10
backend/search_results/admin.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
from unfold.admin import ModelAdmin
|
||||
from django.contrib import admin
|
||||
from .models import SearchResult
|
||||
|
||||
|
||||
@admin.register(SearchResult)
|
||||
class SearchResultAdmin(ModelAdmin):
|
||||
model = SearchResult
|
||||
search_fields = ('id', 'title', 'link')
|
||||
list_display = ['id', 'title']
|
6
backend/search_results/apps.py
Normal file
6
backend/search_results/apps.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class SearchResultsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'search_results'
|
23
backend/search_results/migrations/0001_initial.py
Normal file
23
backend/search_results/migrations/0001_initial.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 5.0.6 on 2024-09-24 07:47
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='SearchResult',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=1000)),
|
||||
('link', models.CharField(max_length=1000)),
|
||||
('timestamp', models.DateTimeField(auto_now_add=True)),
|
||||
],
|
||||
),
|
||||
]
|
0
backend/search_results/migrations/__init__.py
Normal file
0
backend/search_results/migrations/__init__.py
Normal file
7
backend/search_results/models.py
Normal file
7
backend/search_results/models.py
Normal file
|
@ -0,0 +1,7 @@
|
|||
from django.db import models
|
||||
|
||||
|
||||
class SearchResult(models.Model):
|
||||
title = models.CharField(max_length=1000)
|
||||
link = models.CharField(max_length=1000)
|
||||
timestamp = models.DateTimeField(auto_now_add=True, editable=False)
|
16
backend/search_results/tasks.py
Normal file
16
backend/search_results/tasks.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
|
||||
|
||||
from celery import shared_task
|
||||
from .models import SearchResult
|
||||
|
||||
|
||||
@shared_task(autoretry_for=(Exception,), retry_kwargs={'max_retries': 0, 'countdown': 5})
|
||||
def create_search_result(title, link):
|
||||
if SearchResult.objects.filter(title=title, link=link).exists():
|
||||
return ("SearchResult entry already exists")
|
||||
else:
|
||||
SearchResult.objects.create(
|
||||
title=title,
|
||||
link=link
|
||||
)
|
||||
return f"Created new SearchResult entry titled: {title}"
|
Loading…
Add table
Add a link
Reference in a new issue