modify userprofile

This commit is contained in:
baoliang 2020-09-03 17:46:04 +08:00
parent 817ec56f84
commit 85a8b0f96e
14 changed files with 159 additions and 46 deletions

View File

@ -23,4 +23,9 @@ python manage.py migrate
```shell
sudo /usr/sbin/passenger-memory-stats
```
```
eb3e67e892173f14c600cbfa39da053b111e47ff
675063ff1d43f2e897fda5d70bb6c5bea57ce451

View File

@ -106,7 +106,11 @@ class Userprofile(models.Model):
organization = models.ForeignKey(Organization, on_delete=models.CASCADE, null=True, blank=True)
#用户状态:注册进来默认为0为未审核状态审核后status=1
status = models.IntegerField('用户状态',null=True,blank=True,default=0)
created = models.DateTimeField('创建时间', auto_now_add=True)
updated = models.DateTimeField('更新时间', auto_now=True)
class Meta:
ordering = ["-created"]
def __str__(self):
return self.user.username
@ -130,7 +134,7 @@ class Weixin(models.Model):
code = models.CharField('微信公众号', max_length=256, null=True, blank=True)
weixinid = models.CharField('微信ID',max_length=256,null=True,blank=True)
alias = models.CharField('别名',max_length=256,null=True,blank=True)
image = models.FileField(upload_to='cover', null=True, blank=True)
image = models.FileField(upload_to='cover/%Y/%m/%d/', null=True, blank=True)
organization = models.ForeignKey(Organization, on_delete=models.CASCADE, null=True, blank=True)
status = models.CharField('状态',max_length=256,null=True,blank=True,choices=WEIXIN_STATUS_CHOICES)
created = models.DateTimeField('创建时间', auto_now_add=True)
@ -199,7 +203,7 @@ class Weibo(models.Model):
code = models.CharField('微博号', max_length=256, null=True, blank=True)
weiboid = models.CharField('微博ID', max_length=256, null=True, blank=True)
alias = models.CharField('别名', max_length=256, null=True, blank=True)
image = models.FileField(upload_to='cover', null=True, blank=True)
image = models.FileField(upload_to='cover/%Y/%m/%d/', null=True, blank=True)
organization = models.ForeignKey(Organization, on_delete=models.CASCADE, null=True, blank=True)
status = models.CharField('状态', max_length=256, null=True, blank=True, choices=WEIBO_STATUS_CHOICES)
created = models.DateTimeField('创建时间', auto_now_add=True)
@ -255,7 +259,7 @@ class Toutiao(models.Model):
code = models.CharField('头条号', max_length=256, null=True, blank=True)
toutiaoid = models.CharField('头条ID', max_length=256, null=True, blank=True)
alias = models.CharField('别名', max_length=256, null=True, blank=True)
image = models.FileField(upload_to='cover', null=True, blank=True)
image = models.FileField(upload_to='cover/%Y/%m/%d/', null=True, blank=True)
organization = models.ForeignKey(Organization, on_delete=models.CASCADE, null=True, blank=True)
status = models.CharField('状态', max_length=256, null=True, blank=True, choices=TOUTIAO_STATUS_CHOICES)
created = models.DateTimeField('创建时间', auto_now_add=True)
@ -325,7 +329,7 @@ class Qita(models.Model):
name = models.CharField('新媒体名称', max_length=256, null=True, blank=True)
qitaid = models.CharField('新媒体ID', max_length=256, null=True, blank=True)
alias = models.CharField('别名', max_length=256, null=True, blank=True)
image = models.FileField(upload_to='cover', null=True, blank=True)
image = models.FileField(upload_to='cover/%Y/%m/%d/', null=True, blank=True)
organization = models.ForeignKey(Organization, on_delete=models.CASCADE, null=True, blank=True)
status = models.CharField('状态', max_length=256, null=True, blank=True, choices=QITA_STATUS_CHOICES)
created = models.DateTimeField('创建时间', auto_now_add=True)

26
polls/forms.py Normal file
View File

@ -0,0 +1,26 @@
from django import forms
from dashboard.models import Weixin, Weibo, Toutiao, Qita
class WeixinImageForm(forms.ModelForm):
class Meta:
model = Weixin
fields = ['image', ]
class WeiboImageForm(forms.ModelForm):
class Meta:
model = Weibo
fields = ['image', ]
class ToutiaoImageForm(forms.ModelForm):
class Meta:
model = Toutiao
fields = ['image', ]
class QitaImageForm(forms.ModelForm):
class Meta:
model = Qita
fields = ['image', ]

View File

@ -48,7 +48,7 @@ class Notice(models.Model):
return now <= self.timeouted
def __str__(self):
return self.phone + ':' + self.code
return self.content + ':' + self.user.username
MESSAGE_TYPE_CHOICES = (

View File

@ -6,6 +6,13 @@ urlpatterns = [
path('', views.index, name='index'),
path('status_500', views.status_500, name='status_500'),
path('status_401', views.status_401, name='status_401'),
path('login', views.polls_login, name='polls_login'),
path('notices/list/', views.notices, name='polls_notices')
path('login/', views.polls_login, name='polls_login'),
path('send_code/', views.send_code, name='polls_send_code'),
path('register/', views.register_step_one, name='polls_register'),
path('register/save/', views.register_step_two, name='polls_register_save'),
path('password/recovery/', views.password_recover_step_one, name='polls_password_recovery'),
path('password/recovery/save/', views.password_recover_step_two, name='polls_password_recovery_save'),
path('notices/list/', views.notices, name='polls_notices'),
path('notices/read/', views.read_notice, name='polls_read_notice'),
path('medias/create/', views.create_media, name='polls_add_media'),
]

View File

@ -8,7 +8,7 @@ from .exceptions import ClientError
from django.conf import settings
def sent_sms_code(phone):
def sent_sms_code(phone, code):
client = AcsClient(settings.SMS_ACCESS_KEY_ID,
settings.SMS_ACCESS_KEY_SECRET, settings.SMS_REGION)
request = CommonRequest()
@ -20,13 +20,13 @@ def sent_sms_code(phone):
request.set_action_name('SendSms')
request.add_query_param('RegionId', "cn-hangzhou")
request.add_query_param('PhoneNumbers', "13993199566")
request.add_query_param('PhoneNumbers', phone)
request.add_query_param('SignName', "短信验证")
request.add_query_param('TemplateCode', "SMS_12330409")
request.add_query_param('TemplateParam', '{"number":"1111"}')
request.add_query_param('TemplateParam', '{"number":"%s"}' % (code,))
response = client.do_action(request)
print(type(response.decode('utf8')))
return response
def generate_code():

View File

@ -1,2 +1,3 @@
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
from .notice import notices, read_notice
from .media import create_media

Binary file not shown.

41
polls/views/media.py Normal file
View File

@ -0,0 +1,41 @@
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from dashboard.models import Weixin, Weibo, Toutiao, Qita
from polls.decorators import polls_login_required
@csrf_exempt
@polls_login_required
def create_media(request):
profile = request.user.userprofile_set.first()
o = profile.organization
print(profile)
if request.method == 'GET':
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'})

View File

@ -5,6 +5,7 @@ import datetime
from polls.decorators import polls_login_required
from polls.models import Notice
from django.core.exceptions import ObjectDoesNotExist
@csrf_exempt
@polls_login_required
@ -12,13 +13,30 @@ def notices(request):
if request.method == 'POST':
return HttpResponse(status=405)
id = request.user.id
notices = Notice.objects.filter(
is_read=False)[:10]
notices = Notice.objects.filter(user__id=id,is_read=False)[:10]
results = []
for o in notices:
result = dict()
result['id'] = o.id
result['content'] = o.title
result['content'] = o.content
result['added'] = o.added.strftime("%Y-%m-%d %H:%M:%S")
results.append(result)
return JsonResponse(results, safe=False)
@csrf_exempt
@polls_login_required
def read_notice(request):
if request.method == 'GET':
return HttpResponse(status=405)
id = request.user.id
notice_id = request.POST.get('notice')
if not notice_id:
return JsonResponse({'status': 'error', 'message': '通知ID错误'})
try:
notice = Notice.objects.get(id=notice_id)
notice.is_read = True
notice.save()
return JsonResponse({'status': 'success'})
except ObjectDoesNotExist:
return JsonResponse({'status': 'error', 'message': '通知ID错误'})

View File

@ -7,6 +7,7 @@ import datetime
from polls.models import VerifyCode
from polls.utils import generate_code, sent_sms_code
from dashboard.models import Organization, Userprofile
def index(request):
@ -37,11 +38,11 @@ def polls_login(request):
u = authenticate(request, username=phone, password=password)
if u is not None:
login(request, u)
token = Token.objects.get_or_create(user=u)
token, created = Token.objects.get_or_create(user=u)
result = dict()
profile = u.userprofile_set.first()
result['phone'] = u.first_name
result['token'] = token
result['token'] = token.key
if profile:
result['name'] = profile.name
result['gender'] = profile.sex
@ -65,16 +66,17 @@ def send_code(request):
exist_code = VerifyCode.objects.filter(
phone=phone, category=category).first()
if exist_code and exist_code.in_progress():
if exist_code and exist_code.is_in_progress():
return JsonResponse({'status': 'error', 'message': '验证码使用中'})
code = generate_code()
now = datetime.datetime.now()
after_10mins = now + datetime.timedelta(minutes=10)
after_1min = now + datetime.timedelta(minutes=1)
response = sent_sms_code(phone, code)
result = response.decode('utf8')
if "OK" in result:
VerifyCode.objects.create(code=code, phone=phone, category=category)
VerifyCode.objects.create(
code=code, phone=phone, category=category, timeouted=after_1min)
return JsonResponse({'status': 'success'})
return JsonResponse({'status': 'error', 'message': '验证码发送失败'})
@ -85,6 +87,9 @@ def register_step_one(request):
return HttpResponse(status=405)
phone = request.POST.get('phone')
code = request.POST.get('code')
category = request.POST.get('category', 0)
if not phone:
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
UserModel = get_user_model()
is_exists = UserModel.objects.filter(username=phone).exists()
@ -92,8 +97,8 @@ def register_step_one(request):
return JsonResponse({'status': 'error', 'message': '该手机号已经使用'})
verify_code = VerifyCode.objects.filter(
phone=phone, code=code, category=0).first()
if verify_code and verify_code.in_progress():
phone=phone, code=code, category=category).first()
if verify_code and verify_code.is_in_progress():
return JsonResponse({'status': 'success', 'message': {phone: phone}})
else:
return JsonResponse({'status': 'error', 'message': '验证码超时,请重发'})
@ -104,12 +109,26 @@ def register_step_two(request):
if request.method == 'GET':
return HttpResponse(status=405)
phone = request.POST.get('phone')
if not phone:
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
name = request.POST.get('name')
gender = request.POST.get('gender')
if not name:
return JsonResponse({'status': 'error', 'message': '姓名不能为空'})
password = request.POST.get('password')
if not password:
return JsonResponse({'status': 'error', 'message': '密码不能为空'})
organization = request.POST.get('organization')
if not organization:
return JsonResponse({'status': 'error', 'message': '单位不能为空'})
UserModel = get_user_model()
UserModel.objects.create_user(username=phone, password=password)
is_exists = UserModel.objects.filter(username=phone).exists()
if is_exists:
return JsonResponse({'status': 'error', 'message': '该手机号已经使用'})
u = UserModel.objects.create_user(username=phone, password=password)
o, created = Organization.objects.get_or_create(name=organization)
profile = Userprofile.objects.create(name=name, user=u, organization=o)
return JsonResponse({'status': 'success', 'message': '注册成功'})
@ -118,10 +137,14 @@ def password_recover_step_one(request):
if request.method == 'GET':
return HttpResponse(status=405)
phone = request.POST.get('phone')
if not phone:
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
code = request.POST.get('code')
if not code:
return JsonResponse({'status': 'error', 'message': '验证码不正确'})
verify_code = VerifyCode.objects.filter(
phone=phone, code=code, category=1).first()
if verify_code and verify_code.in_progress():
if verify_code and verify_code.is_in_progress():
return JsonResponse({'status': 'success', 'message': {phone: phone}})
else:
return JsonResponse({'status': 'error', 'message': '验证码超时,请重发'})
@ -132,8 +155,14 @@ def password_recover_step_two(request):
if request.method == 'GET':
return HttpResponse(status=405)
phone = request.POST.get('phone')
if not phone:
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
password = request.POST.get('password')
if not password:
return JsonResponse({'status': 'error', 'message': '密码不能为空'})
password_confirm = request.POST.get('password_confirm')
if not password_confirm:
return JsonResponse({'status': 'error', 'message': '密码确认不正确'})
if password != password_confirm:
return JsonResponse({'status': 'error', 'message': '两次密码输入不一致'})
UserModel = get_user_model()
@ -141,23 +170,5 @@ def password_recover_step_two(request):
if not user:
return JsonResponse({'status': 'error', 'message': '用户名不存在'})
user.set_password(password)
return JsonResponse({'status': 'success', 'message': {phone: phone}})
@csrf_exempt
def office(request):
if request.method == 'POST':
return HttpResponse(status=405)
top = request.GET.get('top', '10')
offices = Notice.objects.filter(deleted=False).order_by('-view_count')[:int(top)]
results = []
for o in offices:
result = dict()
result['id'] = o.id
result['title'] = o.title
current_thumbnail = o.current_thumbnail()
if current_thumbnail:
result['icon'] = o.current_thumbnail().thumbnail.url
result['view_count'] = o.view_count
results.append(result)
return JsonResponse(results, safe=False)
user.save()
return JsonResponse({'status': 'success', 'message': {phone: phone}})