diff --git a/README.md b/README.md index 44404d2..fe7d3e3 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,16 @@ pip install -r requirements.txt pip install git+https://github.com/RobWeber3/django-token.git +``` + +# migrations + +不要在本机上做make migrations 和migrate! + +```shell +cd /home/g214/tools/nmm/NewMediaMonitoring +git pull # enter username && password +source .venv/bin/activate +python manage.py make migrations +python manage.py migrate ``` \ No newline at end of file diff --git a/polls/models.py b/polls/models.py index fd18c6e..18fdcd7 100644 --- a/polls/models.py +++ b/polls/models.py @@ -1,3 +1,28 @@ from django.db import models +import uuid +import datetime -# Create your models here. +VERIFY_CODE_TYPE_CHOICES = ( + (0, 'register'), + (1, 'password_recover'), +) + +class VerifyCode(models.Model): + id = models.UUIDField('id', primary_key=True, default=uuid.uuid4) + code = models.CharField('code', max_length=8, null=False) + phone = models.CharField('phone', max_length=11, null=False) + timeouted = models.DateTimeField('timeouted', null=False) + category = models.IntegerField('category', choices=VERIFY_CODE_TYPE_CHOICES, default=0) + added = models.DateTimeField(auto_now_add=True) + updated = models.DateTimeField(auto_now=True) + + class Meta: + ordering = ["-added"] + + def is_in_progress(self): + now = datetime.datetime.now() + return snow <= self.timeouted + + + def __str__(self): + return self.phone + ':' + self.code diff --git a/polls/utils.py b/polls/utils.py new file mode 100644 index 0000000..6285ae9 --- /dev/null +++ b/polls/utils.py @@ -0,0 +1,7 @@ +import random + +def sent_sms_code(phone): + pass + +def generate_code(): + return random.randint(1000, 9999) \ No newline at end of file diff --git a/polls/views.py b/polls/views.py index 50a59f7..602e4ec 100644 --- a/polls/views.py +++ b/polls/views.py @@ -2,8 +2,7 @@ from django.shortcuts import render from django.http import HttpResponse, JsonResponse from django.contrib.auth import get_user_model, authenticate, login from django.views.decorators.csrf import csrf_exempt - -# Create your views here. +from django_token.models import Token def index(request): @@ -34,10 +33,85 @@ def polls_login(request): u = authenticate(request, username=user.username, password=password) if u is not None: login(request, u) - print(u) + token = Token.objects.get_or_create(user=u) result = dict() - return JsonResponse(result) + profile = u.userprofile_set.first() + result['phone'] = u.first_name + result['token'] = token + if profile: + result['name'] = profile.name + result['gender'] = profile.sex + result['thumbnail'] = profile.image.path + result['organization'] = profile.organization.name + return JsonResponse({'status': 'success', 'message': result}) else: return JsonResponse({'status': 'error', 'message': '用户名或密码错误'}) else: - return JsonResponse({}) + return HttpResponse(status=405) + + +@csrf_exempt +def send_code(request): + if request.method == 'GET': + return HttpResponse(status=405) + phone = request.POST.get('phone') + category = request.POST.get('category', 0) + if not phone: + return JsonResponse({'status': 'error', 'message': '手机号不正确'}) + + exist_code = VerifyCode.objects.filter(phone=phone, category=category).first() + if exist_code and exist_code.in_progress(): + return JsonResponse({'status': 'error', 'message': '验证码使用中'}) + + code = generate_code() + now = datetime.datetime.now() + after_10mins = now + datetime.timedelta(minutes=10) + VerifyCode.objects.create(code=code, phone=phone, category=category) + return JsonResponse({'status': 'success'}) + + +@csrf_exempt +def register_step_one(request): + if request.method == 'GET': + return HttpResponse(status=405) + phone = request.POST.get('phone') + code = request.POST.get('code') + exist_code = VerifyCode.objects.filter(phone=phone, code=code, category=0).exist() + if exist_code.in_progress(): + return JsonResponse({'status': 'success', 'message': {phone: phone}}) + else: + return JsonResponse({'status': 'error', 'message': '验证码超时,请重发'}) + + +@csrf_exempt +def register_step_two(request): + if request.method == 'GET': + return HttpResponse(status=405) + phone = request.POST.get('phone') + name = request.POST.get('name') + gender = request.POST.get('gender') + organization = request.POST.get('organization') + # create user + return JsonResponse({'status': 'success', 'message': '注册成功'}) + + +@csrf_exempt +def password_recover_step_one(request): + if request.method == 'GET': + return HttpResponse(status=405) + phone = request.POST.get('phone') + code = request.POST.get('code') + exist_code = VerifyCode.objects.filter(phone=phone, code=code, category=1).exist() + if exist_code.in_progress(): + return JsonResponse({'status': 'success', 'message': {phone: phone}}) + else: + return JsonResponse({'status': 'error', 'message': '验证码超时,请重发'}) + + +@csrf_exempt +def password_recover_step_two(request): + if request.method == 'GET': + return HttpResponse(status=405) + phone = request.POST.get('phone') + password = request.POST.get('password') + password_confirm = request.POST.get('password_confirm') diff --git a/requirements.txt b/requirements.txt index 16db3bd..13355e8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ -# Django~=2.2.15 -# django-bootstrap3 -# django-simple-captcha -# psycopg2-binary -# django-cors-headers +Django~=2.2.15 +django-bootstrap3 +django-simple-captcha +psycopg2-binary +django-cors-headers