修改用户属性设置页面,添加学校字段,合并了一些冲突

This commit is contained in:
esp 2015-11-10 18:46:51 +08:00
parent d64659ec6f
commit 0a9b9df13a
6 changed files with 100 additions and 50 deletions

View File

@ -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),
),
]

View File

@ -68,6 +68,9 @@ class UserProfile(models.Model):
submissions_number = models.IntegerField(default=0)
# JSON字典用来表示该用户的问题的解决状态 1为ac2为正在进行
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"

View File

@ -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)
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"]

View File

@ -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)

View File

@ -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",

View File

@ -36,53 +36,53 @@
<div class="form-group col-md-6"><label>手机</label>
<input name="phone" type="text" maxlength="11" id="phone"
class="form-control"
value="{% if request.user.userprofile.phone %}{{ request.user.userprofile.phone }}{% endif %}">
value="{{ request.user.userprofile.phone_number }}">
</div>
<div class="help-block with-errors"></div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label>学校</label>
<input name="school" type="text" class="form-control"
value="{{ request.user.hduoj_username }}">
<input name="school" type="text" class="form-control" id="school"
value="{{ request.user.userprofile.school }}">
<div class="help-block with-errors"></div>
</div>
<div class="form-group col-md-6">
<label>hduoj 用户名</label>
<input name="hduoj" type="text" class="form-control"
value="{{ request.user.hduoj_username }}">
<input name="hduoj" type="text" class="form-control" id="hduoj_username"
value="{{ request.user.userprofile.hduoj_username }}">
<div class="help-block with-errors"></div>
</div>
<div class="form-group col-md-6">
<label>BestCoder 用户名</label>
<input name="bestcoder" type="text" class="form-control"
value="{{ request.user.bestcoder_username }}">
<input name="bestcoder" type="text" class="form-control" id="bestcoder_username"
value="{{ request.user.userprofile.bestcoder_username }}">
<div class="help-block with-errors"></div>
</div>
<div class="form-group col-md-6">
<label>Codeforces 用户名</label>
<input name="codeforces" type="text" class="form-control"
value="{{ request.user.bestcoder_username }}">
<input name="codeforces" type="text" class="form-control" id="codeforce_username"
value="{{ request.user.userprofile.bestcoder_username }}">
<div class="help-block with-errors"></div>
</div>
<div class="form-group col-md-12"><label>blog</label>
<input name="blog" type="url" class="form-control"
value="{{ request.user.blog }}">
<input name="blog" type="url" class="form-control" id="blog"
value="{{ request.user.userprofile.blog }}">
<div class="help-block with-errors"></div>
<div class="form-group col-md-12">
<label>签名</label>
<input name="mood" type="text" maxlength="60" class="form-control" id="mood"
value="{% if request.user.userprofile.mood %}{{ request.user.userprofile.mood }}{% endif %}"
data-error="字数限制在30字以内">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group col-md-12">
<label>签名</label>
<input name="mood" type="text" maxlength="60" class="form-control" id="mood"
value="{% if request.user.userprofile.mood %}{{ request.user.userprofile.mood }}{% endif %}"
data-error="字数限制在30字以内">
<div class="help-block with-errors"></div>
</div>
<div class="form-group">