diff --git a/dashboard/models.py b/dashboard/models.py index e489b0a..a8d208e 100644 --- a/dashboard/models.py +++ b/dashboard/models.py @@ -113,6 +113,10 @@ class Organization(models.Model): created = models.DateTimeField('创建时间', auto_now_add=True) updated = models.DateTimeField('更新时间', auto_now=True) + class Meta: + ordering = ["name"] + + def __str__(self): return self.name @@ -545,6 +549,10 @@ class Area_code_2020(models.Model): def __str__(self): return self.name + class Meta: + ordering = ["code"] + + # 新闻 diff --git a/polls/urls.py b/polls/urls.py index 7b41ac7..27e3090 100644 --- a/polls/urls.py +++ b/polls/urls.py @@ -17,7 +17,10 @@ urlpatterns = [ path('medias/create/', views.create_media, name='polls_add_media'), path('medias/update/', views.update_media, name='polls_update_media'), path('medias/list/', views.medias, name='polls_medias'), + path('medias/my/', views.my_medias, name='polls_my_medias'), + path('medias///', views.media_detail, name='polls_media_detail'), path('news/list/', views.news_list, name='polls_news'), + path('news/top/', views.news_top, name='polls_news_top'), path('news/detail//', views.news_detail, name='polls_news_detail'), path('monitor/statistics/', views.monitor_statistics, name='polls_monitor_statistics'), path('tasks/list/', views.tasks, name='polls_tasks_list'), @@ -26,4 +29,5 @@ urlpatterns = [ path('groups/list/', views.groups, name='polls_groups_list'), path('group/test-room/', views.room, name='polls_group_test_room'), path('compartments/list/', views.compartments, name='polls_compartments_list'), + path('organizations/list/', views.organizations, name='polls_organizations_list'), ] diff --git a/polls/views/__init__.py b/polls/views/__init__.py index a475e88..8c7ba9a 100644 --- a/polls/views/__init__.py +++ b/polls/views/__init__.py @@ -1,8 +1,9 @@ from .user import index, status_500, status_401, polls_login, send_code, register_step_one, register_step_two, password_recover_step_one, password_recover_step_two from .notice import notices, read_notice -from .media import medias, create_media, update_media -from .news import news_list, news_detail +from .media import medias, my_medias, create_media, update_media, media_detail +from .news import news_list, news_top, news_detail from .monitor import monitor_statistics from .task import tasks, create_task, create_test_task from .group import groups, room -from .compartment import compartments \ No newline at end of file +from .compartment import compartments +from .organizations import organizations \ No newline at end of file diff --git a/polls/views/__pycache__/__init__.cpython-38.pyc b/polls/views/__pycache__/__init__.cpython-38.pyc index dcd8919..55dffc5 100644 Binary files a/polls/views/__pycache__/__init__.cpython-38.pyc and b/polls/views/__pycache__/__init__.cpython-38.pyc differ diff --git a/polls/views/__pycache__/compartment.cpython-38.pyc b/polls/views/__pycache__/compartment.cpython-38.pyc index b6566dd..901367b 100644 Binary files a/polls/views/__pycache__/compartment.cpython-38.pyc and b/polls/views/__pycache__/compartment.cpython-38.pyc differ diff --git a/polls/views/__pycache__/group.cpython-38.pyc b/polls/views/__pycache__/group.cpython-38.pyc index 60c9ef5..6672315 100644 Binary files a/polls/views/__pycache__/group.cpython-38.pyc and b/polls/views/__pycache__/group.cpython-38.pyc differ diff --git a/polls/views/__pycache__/media.cpython-38.pyc b/polls/views/__pycache__/media.cpython-38.pyc index de596b7..e88cea7 100644 Binary files a/polls/views/__pycache__/media.cpython-38.pyc and b/polls/views/__pycache__/media.cpython-38.pyc differ diff --git a/polls/views/__pycache__/news.cpython-38.pyc b/polls/views/__pycache__/news.cpython-38.pyc index 75e1a53..b278239 100644 Binary files a/polls/views/__pycache__/news.cpython-38.pyc and b/polls/views/__pycache__/news.cpython-38.pyc differ diff --git a/polls/views/__pycache__/organizations.cpython-38.pyc b/polls/views/__pycache__/organizations.cpython-38.pyc new file mode 100644 index 0000000..2e3c399 Binary files /dev/null and b/polls/views/__pycache__/organizations.cpython-38.pyc differ diff --git a/polls/views/__pycache__/user.cpython-38.pyc b/polls/views/__pycache__/user.cpython-38.pyc index 5d0554f..7865218 100644 Binary files a/polls/views/__pycache__/user.cpython-38.pyc and b/polls/views/__pycache__/user.cpython-38.pyc differ diff --git a/polls/views/compartment.py b/polls/views/compartment.py index 20ffed4..f0a2f52 100644 --- a/polls/views/compartment.py +++ b/polls/views/compartment.py @@ -9,7 +9,7 @@ from polls.decorators import polls_login_required @csrf_exempt @polls_login_required def compartments(request): - parent_code = request.GET.get('pcode', '620000000000') + parent_code = request.GET.get('code', '620000000000') compartments = Area_code_2020.objects.filter(pcode=parent_code) results = [] for o in compartments: diff --git a/polls/views/group.py b/polls/views/group.py index c479519..1467a66 100644 --- a/polls/views/group.py +++ b/polls/views/group.py @@ -18,7 +18,7 @@ def groups(request): if request.method == 'POST': return HttpResponse(status=405) id = request.user.id - groups = Group.objects.filter(status='开启') + groups = Group.objects.filter(status='1') results = [] for o in groups: result = dict() @@ -26,4 +26,4 @@ def groups(request): result['name'] = o.name result['image'] = request.build_absolute_uri(o.image.url) results.append(result) - return JsonResponse(results, safe=False) + return JsonResponse({'status': 'success', 'message': results}, safe=False) diff --git a/polls/views/media.py b/polls/views/media.py index 4d5cd6a..0c880c2 100644 --- a/polls/views/media.py +++ b/polls/views/media.py @@ -18,39 +18,14 @@ def medias(request): @csrf_exempt @polls_login_required -def create_media(request): - profile = request.user.userprofile_set.first() - o = profile.organization - print(profile) - if request.method == 'GET': +def my_medias(request): + if request.method == 'POST': return HttpResponse(status=405) - category = request.POST.get('category') - if not category: - return JsonResponse({'status': 'error', 'message': '类型错误'}) - code = request.POST.get('code') - if not code: - return JsonResponse({'status': 'error', 'message': '公众号名称不能为空'}) - media_id = request.POST.get('media_id') - if not media_id: - return JsonResponse({'status': 'error', 'message': '公众号ID不能为空'}) - alias = request.POST.get('alias') - if not alias: - return JsonResponse({'status': 'error', 'message': '别名不能为空'}) - - if category == 'weixin': - instance = Weixin(code=code, weixinid=media_id, alias=alias, - image=request.FILES['file'], status=1, organization=o) - elif category == 'weibo': - instance = Weibo(code=code, weixinid=media_id, alias=alias, - image=request.FILES['file'], status=1, organization=o) - elif category == 'toutiao': - instance = Toutiao(code=code, weixinid=media_id, alias=alias, - image=request.FILES['file'], status=1, organization=o) - else: - instance = Qita(code=code, weixinid=media_id, alias=alias, - image=request.FILES['file'], status=1, organization=o) - instance.save() - return JsonResponse({'status': 'success'}) + user = request.user + profile = user.userprofile_set.first() + organization_id = profile.organization.id + new_medias = NewMedia.media_list(organization_id) + return JsonResponse({'status': 'success', 'message': new_medias}, safe=False) @csrf_exempt @@ -110,17 +85,78 @@ def update_media(request): value = request.POST.get('value') if not name: return JsonResponse({'status': 'error', 'message': '值不能为空'}) - + result = dict() if category == 'weixin': instance = Weixin.objects.get(pk=media_id) + result['media_id'] = instance.weixinid elif category == 'weibo': instance = Weibo.objects.get(pk=media_id) + result['media_id'] = instance.weiboid elif category == 'toutiao': instance = Toutiao.objects.get(pk=media_id) + result['media_id'] = instance.toutiaoid elif category == 'douyin': instance = Douyin.objects.get(pk=media_id) + result['media_id'] = instance.douyinid else: instance = Qita.objects.get(pk=media_id) + result['media_id'] = instance.qitaid + setattr(instance, name, value) instance.save(update_fields=[name]) - return JsonResponse({'status': 'success'}) \ No newline at end of file + admins = [] + for admin in instance.organization.userprofile_set.all(): + u = dict() + u['id'] = admin.user.id + u['name'] = admin.name + u['phone'] = admin.user.username + admins.append(u) + + result['id'] = instance.id + result['code'] = instance.code + result['alias'] = instance.alias + result['status'] = instance.status + result['attention'] = instance.attention + result['remark'] = instance.remark + result['organization'] = instance.organization.name + result['admin'] = admins + return JsonResponse({'status': 'success', 'message': result}) + + +@csrf_exempt +@polls_login_required +def media_detail(request, type, media_id): + result = dict() + if type == 'weixin': + media = Weixin.objects.get(pk=media_id) + result['media_id'] = media.weixinid + elif type == 'weibo': + media = Weibo.objects.get(pk=media_id) + result['media_id'] = media.weiboid + elif type == 'toutiao': + media = Toutiao.objects.get(pk=media_id) + result['media_id'] = media.toutiaoid + elif type == 'douyin': + media = Douyin.objects.get(pk=media_id) + result['media_id'] = media.douyinid + else: + media = Qita.objects.get(pk=media_id) + result['media_id'] = media.qitaid + + admins = [] + for admin in media.organization.userprofile_set.all(): + u = dict() + u['id'] = admin.user.id + u['name'] = admin.name + u['phone'] = admin.user.username + admins.append(u) + + result['id'] = media.id + result['code'] = media.code + result['alias'] = media.alias + result['status'] = media.status + result['attention'] = media.attention + result['remark'] = media.remark + result['organization'] = media.organization.name + result['admin'] = admins + return JsonResponse({'status': 'success', 'message': result}, safe=False) diff --git a/polls/views/news.py b/polls/views/news.py index 0a05b37..15b4020 100644 --- a/polls/views/news.py +++ b/polls/views/news.py @@ -24,6 +24,24 @@ def news_list(request): return JsonResponse({'status': 'success', 'message': results}, safe=False) +@csrf_exempt +@polls_login_required +def news_top(request): + category = request.GET.get('category', '3') + news_list = News.objects.filter(type=category)[:5] + results = [] + for o in news_list: + result = dict() + result['id'] = o.id + result['title'] = o.title + result['author'] = o.author + result['source'] = o.source + result['image'] = request.build_absolute_uri(o.image.url) if o.image else '' + result['date'] = o.date.strftime("%Y-%m-%d") + results.append(result) + return JsonResponse({'status': 'success', 'message': results}, safe=False) + + def news_detail(request, news_id): news = News.objects.get(pk=news_id) return render(request, 'polls/news_detail.html', {'news': news}) diff --git a/polls/views/organizations.py b/polls/views/organizations.py new file mode 100644 index 0000000..d8a50b8 --- /dev/null +++ b/polls/views/organizations.py @@ -0,0 +1,46 @@ +from django.shortcuts import render +from dashboard.models import Organization +from django.http import JsonResponse + +from django.views.decorators.csrf import csrf_exempt +from polls.decorators import polls_login_required + + +@csrf_exempt +@polls_login_required +def organizations(request): + category = request.GET.get('category', '1') + code = request.GET.get('code') + print(category, code) + if category == '1': + organizations = Organization.objects.exclude( + province='' + ).filter( + cities='', + district='' + ) + elif category == '2': + organizations = Organization.objects.exclude( + province='' + ).filter( + cities=code, + district='' + ) + elif category == '3': + organizations = Organization.objects.exclude( + province='', + cities='', + ).filter( + district=code + ) + results = [] + for o in organizations: + result = dict() + result['id'] = o.id + result['name'] = o.name + result['province'] = o.province + result['cities'] = o.cities + result['district'] = o.district + results.append(result) + + return JsonResponse({'status': 'success', 'message': results}, safe=False) \ No newline at end of file diff --git a/polls/views/user.py b/polls/views/user.py index b48ac16..bf3cccc 100644 --- a/polls/views/user.py +++ b/polls/views/user.py @@ -34,7 +34,7 @@ def polls_login(request): user = UserModel.objects.filter(username=phone).first() if not user: return JsonResponse({'status': 'error', 'message': '用户名或密码错误'}) - profile = request.user.userprofile_set.first() + profile = user.userprofile_set.first() if not profile or profile.status == 0: return JsonResponse({'status': 'error', 'message': '用户在审核中'})