diff --git a/account/migrations/0014_auto_20151110_1037.py b/account/migrations/0014_auto_20151110_1037.py new file mode 100644 index 00000000..288e4712 --- /dev/null +++ b/account/migrations/0014_auto_20151110_1037.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('account', '0013_userprofile'), + ] + + operations = [ + migrations.AddField( + model_name='userprofile', + name='phone_number', + field=models.CharField(max_length=15, null=True, blank=True), + ), + migrations.AddField( + model_name='userprofile', + name='school', + field=models.CharField(max_length=200, null=True, blank=True), + ), + ] diff --git a/account/models.py b/account/models.py index 0fb78589..e08ff6c8 100644 --- a/account/models.py +++ b/account/models.py @@ -68,6 +68,9 @@ class UserProfile(models.Model): submissions_number = models.IntegerField(default=0) # JSON字典用来表示该用户的问题的解决状态 1为ac,2为正在进行 problems_status = JSONField(default={}) + phone_number = models.CharField(max_length=15, blank=True, null=True) + school = models.CharField(max_length=200, blank=True, null=True) + class Meta: db_table = "user_profile" diff --git a/account/serializers.py b/account/serializers.py index c2a9a1ad..7bffe63f 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -1,7 +1,7 @@ # coding=utf-8 from rest_framework import serializers -from .models import User +from .models import User, UserProfile class UserLoginSerializer(serializers.Serializer): @@ -65,10 +65,20 @@ class SSOSerializer(serializers.Serializer): class EditUserProfileSerializer(serializers.Serializer): + avatar = serializers.CharField(max_length=50, required=False, default=None) - phone = serializers.CharField(min_length=11, max_length=11, required=False, allow_blank=True, default=None) - blog = serializers.URLField(required=False, allow_blank=True, default=None) - mood = serializers.CharField(max_length=60, required=False, allow_blank=True, default=None) - hduoj_username = serializers.CharField(max_length=30, required=False, allow_blank=True, default=None) - bestcoder_username = serializers.CharField(max_length=30, required=False, allow_blank=True, default=None) - codeforces_username = serializers.CharField(max_length=30, required=False, allow_blank=True, default=None) \ No newline at end of file + blog = serializers.URLField(required=False, allow_blank=True, default='') + mood = serializers.CharField(max_length=60, required=False, default='') + hduoj_username = serializers.CharField(max_length=30, required=False, allow_blank=True, default='') + bestcoder_username = serializers.CharField(max_length=30, required=False, allow_blank=True, default='') + codeforces_username = serializers.CharField(max_length=30, required=False, allow_blank=True, default='') + school = serializers.CharField(max_length=200, required=False, allow_blank=True, default='') + phone_number = serializers.CharField(max_length=15, required=False, allow_blank=True, default='') + + +class UserProfileSerializer(serializers.ModelSerializer): + + class Meta: + model = UserProfile + fields = ["avatar", "blog", "mood", "hduoj_username", "bestcoder_username", "codeforces_username", + "rank", "accepted_number", "submissions_number", "problems_status", "phone_number", "school"] diff --git a/account/views.py b/account/views.py index f675f0bd..8cc6e3d8 100644 --- a/account/views.py +++ b/account/views.py @@ -18,11 +18,13 @@ from mail.tasks import send_email from .decorators import login_required from .models import User, UserProfile + from .serializers import (UserLoginSerializer, UserRegisterSerializer, UserChangePasswordSerializer, UserSerializer, EditUserSerializer, ApplyResetPasswordSerializer, ResetPasswordSerializer, - SSOSerializer, EditUserProfileSerializer) + SSOSerializer, EditUserProfileSerializer, UserProfileSerializer) + from .decorators import super_admin_required @@ -226,6 +228,37 @@ class UserInfoAPIView(APIView): return success_response(UserSerializer(request.user).data) +class UserProfileAPIView(APIView): + @login_required + def get(self, request): + """ + 返回这个用户的个人信息 + --- + response_serializer: UserSerializer + """ + return success_response(UserSerializer(request.user).data) + + @login_required + def put(self, request): + serializer = EditUserProfileSerializer(data=request.data) + if serializer.is_valid(): + data = serializer.data + user_profile = request.user.userprofile + if data["avatar"]: + user_profile.avatar = data["avatar"] + user_profile.mood = data["mood"] + user_profile.hduoj_username = data["hduoj_username"] + user_profile.bestcoder_username = data["bestcoder_username"] + user_profile.codeforces_username = data["codeforces_username"] + user_profile.blog = data["blog"] + user_profile.school = data["school"] + user_profile.phone_number = data["phone_number"] + user_profile.save() + return success_response(u"修改成功") + else: + return serializer_invalid_response(serializer) + + class ApplyResetPasswordAPIView(APIView): def post(self, request): """ @@ -300,28 +333,6 @@ def user_index_page(request, username): return render(request, "oj/account/user_index.html", {"user": user, "blog_link": blog_link}) -class UserProfileAPIView(APIView): - @login_required - def put(self, request): - serializer = EditUserProfileSerializer(data=request.data) - if serializer.is_valid(): - data = serializer.data - user_profile = request.user.userprofile - - if data["avatar"]: - user_profile.avatar = data["avatar"] - user_profile.mood = data["mood"] - user_profile.hduoj_username = data["hduoj_username"] - user_profile.bestcoder_username = data["bestcoder_username"] - user_profile.codeforces_username = data["codeforces_username"] - user_profile.blog = data["blog"] - - user_profile.save() - return success_response(u"修改成功") - else: - return serializer_invalid_response(serializer) - - class SSOAPIView(APIView): def post(self, request): serializer = SSOSerializer(data=request.data) diff --git a/static/src/js/app/oj/account/settings.js b/static/src/js/app/oj/account/settings.js index bbbbef17..b4538ee5 100644 --- a/static/src/js/app/oj/account/settings.js +++ b/static/src/js/app/oj/account/settings.js @@ -7,17 +7,19 @@ require(["jquery", "bsAlert", "csrfToken", "validator"], function ($, bsAlert, c var codeforces_username = $("#codeforces_username").val(); var blog = $("#blog").val(); var mood = $("#mood").val(); + var school = $("#school").val(); $.ajax({ beforeSend: csrfTokenHeader, url: "/api/account/userprofile/", data: { - phone: phone, + phone_number: phone, hduoj_username: hduoj_username, bestcoder_username: bestcoder_username, codeforces_username: codeforces_username, blog: blog, - mood: mood + mood: mood, + school: school }, dataType: "json", method: "put", diff --git a/template/src/oj/account/settings.html b/template/src/oj/account/settings.html index 41495673..3dc75c6d 100644 --- a/template/src/oj/account/settings.html +++ b/template/src/oj/account/settings.html @@ -36,53 +36,53 @@
+ value="{{ request.user.userprofile.phone_number }}">
- +
- +
- +
- +
- +
-
- - -
-
+
+
+ + +