Add lazy way to make an user property editable once only

This commit is contained in:
Antoine Bartuccio 2016-10-13 22:58:26 +02:00
parent 579c25d63a
commit 178a4af196

View File

@ -318,25 +318,23 @@ class UserUpdateProfileView(UserTabsMixin, CanEditMixin, UpdateView):
template_name = "core/user_edit.jinja"
form_class = UserProfileForm
current_tab = "edit"
edit_once = ['profile_pict', 'date_of_birth', 'first_name', 'last_name']
def remove_once_edited_fields(self, request):
for i in self.edit_once:
if getattr(self.form.instance, i) and not (request.user.is_board_member or request.user.is_root):
self.form.fields.pop(i, None)
def get(self, request, *args, **kwargs):
self.object = self.get_object()
self.form = self.get_form()
if self.form.instance.profile_pict and not (request.user.is_board_member or request.user.is_root):
self.form.fields.pop('profile_pict', None)
if self.form.instance.date_of_birth and not (request.user.is_board_member or request.user.is_root):
self.form.fields.pop('date_of_birth')
if self.form.instance.first_name and not (request.user.is_board_member or request.user.is_root):
self.form.fields.pop('first_name')
if self.form.instance.last_name and not (request.user.is_board_member or request.user.is_root):
self.form.fields.pop('last_name')
self.remove_once_edited_fields(request)
return self.render_to_response(self.get_context_data(form=self.form))
def post(self, request, *args, **kwargs):
self.object = self.get_object()
self.form = self.get_form()
if self.form.instance.profile_pict and not request.user.is_in_group(settings.SITH_MAIN_BOARD_GROUP):
self.form.fields.pop('profile_pict', None)
self.remove_once_edited_fields(request)
files = request.FILES.items()
self.form.process(files)
if request.user.is_authenticated() and request.user.can_edit(self.object) and self.form.is_valid():