add taskaddition

This commit is contained in:
baoliang 2020-09-18 15:52:06 +08:00
parent 3f5928a9cf
commit 292c3ee08d
5 changed files with 35 additions and 3 deletions

View File

@ -32,6 +32,7 @@ ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [ INSTALLED_APPS = [
'channels', 'channels',
'background_task',
'django.contrib.admin', 'django.contrib.admin',
'django.contrib.auth', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.contenttypes',

View File

@ -4,7 +4,7 @@
pip install -r requirements.txt pip install -r requirements.txt
pip install git+https://github.com/RobWeber3/django-token.git pip install git+https://github.com/RobWeber3/django-token.git
pip install git+https://github.com/arteria/django-background-tasks.git
``` ```
# migrations # migrations

View File

@ -60,6 +60,12 @@ class Task(models.Model):
added = models.DateTimeField(auto_now_add=True) added = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True) updated = models.DateTimeField(auto_now=True)
def add_groups(self, groups):
if not len(groups):
groups = Group.objects.filter(status='1')
for g in groups:
self.groups.add(g)
class Meta: class Meta:
ordering = ["-added"] ordering = ["-added"]
@ -75,6 +81,7 @@ class TaskAddition(models.Model):
picture = models.ImageField( picture = models.ImageField(
upload_to='task/image/%Y/%m/%d/', null=True, blank=True) upload_to='task/image/%Y/%m/%d/', null=True, blank=True)
TASK_RECORD_STATUS_CHOICES = ( TASK_RECORD_STATUS_CHOICES = (
(0, '未完成'), (0, '未完成'),
(1, '完成'), (1, '完成'),

View File

@ -5,7 +5,7 @@ from django.forms.models import model_to_dict
import datetime import datetime
from polls.decorators import polls_login_required from polls.decorators import polls_login_required
from polls.models import Task from polls.models import Task, TaskAddition
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
@ -28,3 +28,27 @@ def tasks(request):
result['added'] = o.added.strftime("%Y-%m-%d %H:%M:%S") result['added'] = o.added.strftime("%Y-%m-%d %H:%M:%S")
results.append(result) results.append(result)
return JsonResponse(results, safe=False) return JsonResponse(results, safe=False)
@csrf_exempt
@polls_login_required
def create_task(request):
if request.method == 'GET':
return HttpResponse(status=405)
user_id = request.user.id
content = request.POST.get('content')
if not content:
return JsonResponse({'status': 'error', 'message': '内容不能为空'})
groups = request.POST.getlist('groups',[])
task = Task.objects.create(created_by__id=user_id, content=content)
task.add_groups(groups)
url = request.POST.get('url')
file = request.FILES.get('file')
picture = request.FILES.get('picture')
if not url:
urlAddtion = TaskAddition.objects.create(task=task,category=0, url=url)
if not file:
fileAddtion = TaskAddition.objects.create(task=task,category=1, file=file)
if not picture:
pictureAddtion = TaskAddition.objects.create(task=task,category=2, image=picture)
return JsonResponse({'status': 'success'})