From d666b7654b29645d70f85012d3008928b38a4031 Mon Sep 17 00:00:00 2001 From: Weitian LI Date: Sun, 27 Apr 2014 13:24:55 +0800 Subject: * updated 'profile.html' to show new fields * added account_profile.css * fixes to 'save' method of model UserProfile and UserFile * fixes to 'UpdateProfileView' with formset 'instance' --- account/models.py | 34 +++++++++++++++++++++++---- account/static/css/account_profile.css | 18 ++++++++++++++ account/templates/account/profile.html | 43 ++++++++++++++++++++++++++++++++++ account/views.py | 17 +++++++------- 4 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 account/static/css/account_profile.css diff --git a/account/models.py b/account/models.py index cb806da..9886f6c 100644 --- a/account/models.py +++ b/account/models.py @@ -15,6 +15,8 @@ from registration.signals import user_registered from account.extra import ContentTypeRestrictedFileField, OverwriteStorage +import os + ###### account models ###### class UserProfile(models.Model): @@ -92,11 +94,21 @@ class UserProfile(models.Model): """ overwrite the original save method to delete old file """ + if not self.pk: + # -> create + return super(UserProfile, self).save(*args, **kwargs) + # already exists -> edit old_obj = type(self).objects.get(pk=self.pk) result = super(UserProfile, self).save(*args, **kwargs) # if the path is the same, the file is overwritten already - if old_obj.transcript.path != self.transcript.path: - old_obj.transcript.delete(save=False) + if old_obj.transcript.name: + # old_obj has transcript file + if not self.transcript.name: + # new object has no transcript + old_obj.transcript.delete(save=False) + elif old_obj.transcript.path != self.transcript.path: + # new object has transcript file, and differ from old_obj + old_obj.transcript.delete(save=False) # return result @@ -110,6 +122,10 @@ class UserProfile(models.Model): else: return False + def get_transcript_filename(self): + # return the base filename of transcript FileField + return os.path.basename(self.transcript.name) + def get_approved(self, *args, **kwargs): """ return list of approved object @@ -186,11 +202,21 @@ class UserFile(models.Model): """ overwrite the original save method to delete old file """ + if not self.pk: + # -> create + return super(UserFile, self).save(*args, **kwargs) + # already exists -> edit old_obj = type(self).objects.get(pk=self.pk) result = super(UserFile, self).save(*args, **kwargs) # if the path is the same, the file is overwritten already - if old_obj.file.path != self.file.path: - old_obj.file.delete(save=False) + if old_obj.file.name: + # old_obj has file + if not self.file.name: + # new object has no file + old_obj.file.delete(save=False) + elif old_obj.file.path != self.file.path: + # new object has file, and differ from old_obj + old_obj.file.delete(save=False) # return result diff --git a/account/static/css/account_profile.css b/account/static/css/account_profile.css new file mode 100644 index 0000000..3522143 --- /dev/null +++ b/account/static/css/account_profile.css @@ -0,0 +1,18 @@ +/* + * account_profile.css + * used in 'profile.html' template + */ + +/* table th cell vertical middle */ +/* +th.attachments { + vertical-align: middle; +} +*/ + +/* ul padding margin */ +td.attachments-data ul { + padding-left: 15px; + margin-bottom: 0px; +} + diff --git a/account/templates/account/profile.html b/account/templates/account/profile.html index 4f5532d..aa99650 100644 --- a/account/templates/account/profile.html +++ b/account/templates/account/profile.html @@ -10,11 +10,27 @@ 个人主页 | 2014 SKA Summer School {% endblock %} +{% block css_extra %} + +{% endblock %} + {% block content %}

个人主页


+ {# display alert if 'reason' empty, or transcript required but not provided #} + {% if not profile.reason %} +
+ “为什么参加”的信息是我们审核的重要参考,请您请及时填写。 +
+ {% endif %} + {% if profile.is_transcript_required and not profile.transcript %} +
+ “成绩单”是我们审核本科生(大三及以下)和决定资助的重要参考,请您请及时上传。 +
+ {% endif %} + @@ -47,6 +63,20 @@ + + + + + {% if profile.is_transcript_required %} + + + + + {% endif %} + + + + + {# user uploaded files #} + + + +
身份 {% trans profile.get_identify_value %}
为什么参加{% if profile.reason %}{{ profile.reason }}{% else %}请及时填写参加理由{% endif %}
成绩单{% if profile.transcript %}{{ profile.get_transcript_filename }}{% else %}请及时上传成绩单{% endif %}
补充说明{% if profile.supplement %}{{ profile.supplement }}{% else %}无{% endif %}
是否审定 @@ -75,6 +105,19 @@ {% endif %}
附件 + {% if userfiles %} + + {% endif %} +

diff --git a/account/views.py b/account/views.py index 432a8fc..faf0a43 100644 --- a/account/views.py +++ b/account/views.py @@ -41,8 +41,10 @@ class ProfileView(TemplateView): context = super(ProfileView, self).get_context_data(**kwargs) user = self.request.user profile = user.userprofile_set.get(user=user) + userfiles = user.userfile_set.all() context['user'] = user context['profile'] = profile + context['userfiles'] = userfiles return context @@ -70,8 +72,9 @@ class UpdateProfileView(UpdateView): # initialize form 'email' field user = self.request.user form.fields['email'].initial = user.email - # formset - formset = UserFileFormSet() + # formset and initialize with instances + qset = user.userfile_set.all() + formset = UserFileFormSet(instance=user, queryset=qset) return self.render_to_response( self.get_context_data(form=form, formset=formset)) @@ -83,12 +86,11 @@ class UpdateProfileView(UpdateView): self.object = self.get_object() form_class = self.get_form_class() form = self.get_form(form_class) - # formset - formset = UserFileFormSet(self.request.POST, self.request.FILES) + ## formset + # must pass 'instance' here, otherwise raise IndexError user = self.request.user - # set instance for formset first, otherwise cannot generate - # right upload_to due to the empty of user - formset.instance = user # UserFileFormSet parent_model + formset = UserFileFormSet(self.request.POST, self.request.FILES, + instance=user) if (form.is_valid() and formset.is_valid()): return self.form_valid(form, formset) else: @@ -106,7 +108,6 @@ class UpdateProfileView(UpdateView): user.email = form_data.get('email', user.email) user.save() # formset - formset.instance = user # UserFileFormSet parent_model formset.save() return HttpResponseRedirect(self.get_success_url()) -- cgit v1.2.2