diff options
author | Weitian LI <liweitianux@gmail.com> | 2014-04-27 13:24:55 +0800 |
---|---|---|
committer | Weitian LI <liweitianux@gmail.com> | 2014-04-27 13:24:55 +0800 |
commit | d666b7654b29645d70f85012d3008928b38a4031 (patch) | |
tree | 1ceea2b872472c1267c6caf04edbc9b5292984cb | |
parent | 309e105ca4fce548cf0e482dca0e4dd0c47daace (diff) | |
download | django-skaschool-d666b7654b29645d70f85012d3008928b38a4031.tar.bz2 |
* 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'
-rw-r--r-- | account/models.py | 34 | ||||
-rw-r--r-- | account/static/css/account_profile.css | 18 | ||||
-rw-r--r-- | account/templates/account/profile.html | 43 | ||||
-rw-r--r-- | account/views.py | 17 |
4 files changed, 100 insertions, 12 deletions
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 %} + <link href="{% static 'css/account_profile.css' %}" rel="stylesheet"> +{% endblock %} + {% block content %} <div class="container"> <h2>个人主页</h2> <br> + {# display alert if 'reason' empty, or transcript required but not provided #} + {% if not profile.reason %} + <div class="alert alert-warning"> + “为什么参加”的信息是我们审核的重要参考,请您请及时填写。 + </div> + {% endif %} + {% if profile.is_transcript_required and not profile.transcript %} + <div class="alert alert-warning"> + “成绩单”是我们审核本科生(大三及以下)和决定资助的重要参考,请您请及时上传。 + </div> + {% endif %} + <table class="table table-striped table-bordered table-hover"> <tbody> <tr> @@ -48,6 +64,20 @@ <td class="profile-identify-data">{% trans profile.get_identify_value %}</td> </tr> <tr> + <th class="profile-reason">为什么参加</th> + <td class="profile-reason-data">{% if profile.reason %}{{ profile.reason }}{% else %}<span class="label label-warning">请及时填写参加理由</span>{% endif %}</td> + </tr> + {% if profile.is_transcript_required %} + <tr> + <th class="profile-transcript">成绩单</th> + <td class="profile-transcript-data">{% if profile.transcript %}<a href="{{ profile.transcript.url }}">{{ profile.get_transcript_filename }}{% else %}<span class="label label-warning">请及时上传成绩单</span>{% endif %}</a></td> + </tr> + {% endif %} + <tr> + <th class="profile-supplement">补充说明</th> + <td class="profile-supplement-data">{% if profile.supplement %}{{ profile.supplement }}{% else %}无{% endif %}</td> + </tr> + <tr> <th class="profile-approval">是否审定</th> <td class="profile-approval-data"> {% if profile.is_approved == 'Y' %} @@ -75,6 +105,19 @@ {% endif %} </td> </tr> + {# user uploaded files #} + <tr> + <th class="attachments">附件</th> + <td class="attachments-data"> + {% if userfiles %} + <ul> + {% for attachment in userfiles %} + <li><a href="{{ attachment.file.url }}">{{ attachment.title }}</a></li> + {% endfor %} + </ul> + {% endif %} + </td> + </tr> </table> <br> 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()) |