add message

This commit is contained in:
baoliang 2020-11-17 08:45:44 +08:00
parent 82e98070f3
commit 235bdc73aa
17 changed files with 106 additions and 64 deletions

View File

@ -1,7 +1,7 @@
import uuid
from django.contrib.auth.models import User
from django.db import models
from django.db.models import Q
# 权限等级
@ -179,7 +179,7 @@ class Userprofile(models.Model):
ordering = ["-created"]
def is_level1(self):
return self.organization.level.level == 1
return self.organization.level.level == 1 or self.organization.level.level == 9
def is_level2(self):
return self.organization.level.level == 2
@ -190,7 +190,7 @@ class Userprofile(models.Model):
@classmethod
def level1_admin(cls):
return Userprofile.objects.filter(organization__level__level=1)
return Userprofile.objects.filter(Q(organization__level__level=1) | Q(organization__level__level=9))
def __str__(self):
if self.is_level1:

View File

@ -1,5 +1,4 @@
from django.apps import AppConfig
class PollsConfig(AppConfig):
name = 'polls'

View File

@ -216,3 +216,9 @@ class AppVersion(models.Model):
class Meta:
ordering = ["-version"]
class SMSNotifyRecord(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
phone = models.CharField('phone', max_length=32)
added = models.DateField(auto_now_add=True)

View File

@ -1,16 +1,19 @@
.title {
line-height: 1.42857;
line-height: 1.42857;
}
.source {
color: rgba(0, 0, 0, 0.35)
color: rgba(0, 0, 0, 0.35);
}
.content {
margin-top: 20px;
margin-top: 20px;
}
.content p {
text-indent: 2em;
line-height: 1.4;
}
line-height: 1.4;
}
.content img {
max-width: 320px !important;
}

View File

@ -1,19 +1,24 @@
from background_task import background
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
from polls.models import Message
from datetime import date, datetime, timedelta
from polls.models import Notice, SMSNotifyRecord
from polls.utils import sent_sms_notify
@background(schedule=1)
def process_task(task, groups, user_id):
print(groups)
for g in groups:
print(g)
# async_to_sync(channel_layer.group_send)(g['id'], {
# "type": 0,
# "send_from": user_id,
# "group_id": g['id'],
# "content": task['content']
# })
channel_layer = get_channel_layer()
async_to_sync(channel_layer.send)('test_channel', {'type': g['id']})
@background(remove_existing_tasks=True)
def process_notify_task():
notices = Notice.objects.filter(is_read=False)
for n in notices:
two_hours_later = n.added + timedelta(minutes=2)
now = datetime.now()
if two_hours_later < now:
phone = n.user.username
today = now.date()
hour = now.hour
exists = SMSNotifyRecord.objects.filter(phone=phone, added=today).exists()
if exists: return
if hour < 8 or hour > 17: return
print('============',phone, n.type)
SMSNotifyRecord.objects.create(phone=phone)
sent_sms_notify(phone, n.type)

View File

@ -1,7 +1,7 @@
from django.urls import path
from . import views
from . import tasks
urlpatterns = [
path('', views.index, name='index'),
path('status_500', views.status_500, name='status_500'),
@ -74,3 +74,5 @@ urlpatterns = [
path('app/download/', views.download, name='polls_app_download'),
path('app/has/update/', views.has_update, name='polls_app_has_update')
]
tasks.process_notify_task(repeat=5)

View File

@ -1,31 +1,28 @@
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
import requests
import random
from parsel import Selector
from channels.db import database_sync_to_async
from django.conf import settings
from itertools import chain
from yunpian_python_sdk.model import constant as YC
from yunpian_python_sdk.ypclient import YunpianClient
def sent_sms_code(phone, code):
client = AcsClient(settings.SMS_ACCESS_KEY_ID,
settings.SMS_ACCESS_KEY_SECRET, settings.SMS_REGION)
request = CommonRequest()
request.set_accept_format('json')
request.set_domain('dysmsapi.aliyuncs.com')
request.set_method('POST')
request.set_protocol_type('https') # https | http
request.set_version('2017-05-25')
request.set_action_name('SendSms')
clnt = YunpianClient('304eb08353f7ebf00596737acfc31f53')
param = {YC.MOBILE: phone ,YC.TEXT:'【甘肃大未来科技】您的验证码是%s。如非本人操作,请忽略本短信' %(code,)}
r = clnt.sms().single_send(param)
return r
request.add_query_param('RegionId', "cn-hangzhou")
request.add_query_param('PhoneNumbers', phone)
request.add_query_param('SignName', "短信验证")
request.add_query_param('TemplateCode', "SMS_12330409")
request.add_query_param('TemplateParam', '{"number":"%s"}' % (code,))
response = client.do_action(request)
return response
def sent_sms_notify(phone, type):
clnt = YunpianClient('304eb08353f7ebf00596737acfc31f53')
if type == 0:
param = {YC.MOBILE: phone ,YC.TEXT:'【甘肃大未来科技】政务新媒体管理APP有新消息请及时登录处理。若其他管理员已处理请忽略'}
else:
param = {YC.MOBILE: phone ,YC.TEXT:'【甘肃大未来科技】政务新媒体管理APP有新任务请及时登录处理。'}
r = clnt.sms().single_send(param)
return r
def generate_code():
@ -109,14 +106,15 @@ def queryset_to_list(q, fields):
if __name__ == '__main__':
# sent_sms_code('13993199566')
r = sent_sms_code('13993199566', 4321)
print(r.code(), type(r.code()))
# og_title, og_description, og_url, og_image = parse(
# 'https://mp.weixin.qq.com/s/EhX0Pm1e0FAfse0zz9ow8Q')
# og_title, og_description, og_url, og_image = parse(
# 'https://m.toutiao.com/i6883651337003729420/?tt_from=weixin&utm_campaign=client_share&app=news_article&utm_source=weixin&iid=1494959660475024&utm_medium=toutiao_android&wxshare_count=1')
# og_title, og_description, og_url, og_image = parse(
# 'http://www.gov.cn/xinwen/2020-10/13/content_5550906.htm')
og_title, og_description, og_url, og_image = parse(
'http://xgs.gansudaily.com.cn/pages/h5/hot/b3297046a53e47f594ed19db90c1183c.html')
# og_title, og_description, og_url, og_image = parse(
# 'http://xgs.gansudaily.com.cn/pages/h5/hot/b3297046a53e47f594ed19db90c1183c.html')
print(og_title, og_description, og_url, og_image)
# print(og_title, og_description, og_url, og_image)

View File

@ -54,7 +54,8 @@ def pick_groups(request):
return HttpResponse(status=405)
user_id = request.user.id
category = request.GET.get('category')
q = Group.objects.filter(status=1, group_admin__user__id=user_id).distinct()
q = Group.objects.filter(
status=1, group_admin__user__id=user_id).distinct()
groups = q.filter(classify=category) if category else q
results = []
for o in groups:
@ -66,6 +67,7 @@ def pick_groups(request):
results.append(result)
return JsonResponse({'status': 'success', 'message': results}, safe=False)
@csrf_exempt
@polls_login_required
def group_manager(request):
@ -74,12 +76,18 @@ def group_manager(request):
group_id = request.GET.get('group_id')
if not group_id:
return JsonResponse({'status': 'error', 'message': '参数错误'})
managers = Userprofile.objects.filter(user__group_admin__group_id=group_id).order_by('user__group_admin__created')
group = Group.objects.get(id=group_id)
q = Userprofile.objects.filter(user__group_admin__group_id=group_id)
if group.classify != '3':
q = q.exclude(Q(organization__level__level=1) |
Q(organization__level__level=9))
q = q.order_by('user__group_admin__created')
results = []
for m in managers:
for m in q:
result = dict()
result['id'] = m.user.id
result['name'] = m.name
result['status'] = m.status
result['thumbnail'] = request.build_absolute_uri(
m.image.url) if m.image else None
result['phone'] = m.user.username
@ -102,6 +110,7 @@ def group_member(request):
result = dict()
result['id'] = m.user.id
result['name'] = m.name
result['status'] = m.status
result['thumbnail'] = request.build_absolute_uri(
m.image.url) if m.image else None
result['phone'] = m.user.username
@ -119,7 +128,8 @@ def group_enter(request):
group_id = request.POST.get('group_id')
if not group_id:
return JsonResponse({'status': 'error', 'message': '参数错误'})
obj,created = GroupRecord.objects.get_or_create(user=user, group_id=group_id)
obj, created = GroupRecord.objects.get_or_create(
user=user, group_id=group_id)
return JsonResponse({'status': 'success'})
@ -132,8 +142,9 @@ def group_leave(request):
group_id = request.POST.get('group_id')
if not group_id:
return JsonResponse({'status': 'error', 'message': '参数错误'})
obj,created = GroupRecord.objects.get_or_create(user=user, group_id=group_id)
obj, created = GroupRecord.objects.get_or_create(
user=user, group_id=group_id)
if not created:
obj.leave_at = datetime.now()
obj.save()
return JsonResponse({'status': 'success'})
return JsonResponse({'status': 'success'})

View File

@ -54,7 +54,7 @@ def create_media(request):
attention = request.POST.get('attention', '')
remark = request.POST.get('remark', '')
model_name = ''
status = 2 if o.directly == '省直部门' else 1
status = 2 if o.is_direct() else 1
if category == 'weixin':
model_name = 'Weixin'
@ -78,7 +78,7 @@ def create_media(request):
instance = Qita(code=code, qitaid=media_id, alias=alias,
image=f, status=status, organization=o, attention=attention, remark=remark)
instance.save()
if o.directly == '省直部门':
if o.is_direct():
admins = Userprofile.level1_admin()
for a in admins:
content = '%s创建了新媒体,请审核' % (profile,)
@ -224,7 +224,6 @@ def delete_media(request):
model_name = 'Qita'
instance = Qita.objects.get(pk=media_id)
result['media_id'] = instance.qitaid
if o.is_direct():
instance.status = 5
instance.save()

View File

@ -79,6 +79,7 @@ def detail_notice(request, notice_id):
results['id'] = notice.id
results['category'] = notice.type
results['content'] = notice.content
results['isRead'] = notice.is_read
if notice.type == 1:
ReplyModel = apps.get_model(notice.app, notice.model)
if notice.model == 'Userprofile':
@ -144,6 +145,10 @@ def pass_notice(request):
if p.admin_status == 4:
p.admin_status = 5
p.save()
group_notices = Notice.objects.filter(record_id=notice.record_id)
for n in group_notices:
n.is_read = True
n.save()
admins = Userprofile.level1_admin()
for a in admins:
Notice.create_reply_notice(
@ -156,6 +161,10 @@ def pass_notice(request):
if p.admin_status == 2:
p.admin_status = 3
p.save()
group_notices = Notice.objects.filter(record_id=notice.record_id)
for n in group_notices:
n.is_read = True
n.save()
return JsonResponse({'status': 'success', 'message': '申请已同意'})
else:
m = ReplyModel.objects.get(pk=notice.record_id)
@ -163,6 +172,10 @@ def pass_notice(request):
m.save(update_fields=[notice.field])
notice.is_read = True
notice.save()
group_notices = Notice.objects.filter(record_id=notice.record_id)
for n in group_notices:
n.is_read = True
n.save()
pass_value = notice.record_pass_value
if pass_value == '2':
admins = Userprofile.level1_admin()
@ -202,6 +215,11 @@ def reject_notice(request):
if p.admin_status == 4 or p.admin_status == 5:
p.admin_status = 3
p.save()
group_notices = Notice.objects.filter(record_id=notice.record_id)
for n in group_notices:
n.is_read = True
n.save()
return JsonResponse({'status': 'success', 'message': '申请已拒绝'})
else:
m = ReplyModel.objects.get(pk=notice.record_id)
@ -209,6 +227,10 @@ def reject_notice(request):
m.save(update_fields=[notice.field])
notice.is_read = True
notice.save()
group_notices = Notice.objects.filter(record_id=notice.record_id)
for n in group_notices:
n.is_read = True
n.save()
return JsonResponse({'status': 'success', 'message': '申请已拒绝'})
except ObjectDoesNotExist:
return JsonResponse({'status': 'error', 'message': '通知ID错误'})

View File

@ -2,12 +2,9 @@ from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
import datetime
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
from polls.decorators import polls_login_required
from polls.models import Message, Notice, Task, TaskAddition
from polls.tasks import process_task
from django.core.exceptions import ObjectDoesNotExist
from polls.utils import model_to_dict, queryset_to_list, parse
from dashboard.models import Group, Group_user, Userprofile

View File

@ -84,13 +84,13 @@ def send_code(request):
now = datetime.datetime.now()
after_1min = now + datetime.timedelta(minutes=1)
response = sent_sms_code(phone, code)
result = response.decode('utf8')
result = response.code()
print(phone, result)
if "OK" in result:
if result == 0:
VerifyCode.objects.create(
code=code, phone=phone, category=category, timeouted=after_1min)
return JsonResponse({'status': 'success'})
return JsonResponse({'status': 'error', 'message': '验证码发送失败'})
return JsonResponse({'status': 'error', 'message': response.msg()})
@csrf_exempt