diff --git a/apps/member/urls.py b/apps/member/urls.py index 87ae662b..d9dfd181 100644 --- a/apps/member/urls.py +++ b/apps/member/urls.py @@ -15,7 +15,7 @@ urlpatterns = [ path('user/', views.UserListView.as_view(), name="user_list"), path('user/', views.UserDetailView.as_view(), name="user_detail"), path('user//update', views.UserUpdateView.as_view(), name="user_update_profile"), - path('user//update_pic', views.UserUpdateView.as_view(), name="user_update_pic"), + path('user//update_pic', views.ProfilePictureUpdateView.as_view(), name="user_update_pic"), path('user//aliases', views.AliasView.as_view(), name="user_alias"), path('user/aliases/delete/', views.DeleteAliasView.as_view(), name="user_alias_delete"), path('manage-auth-token/', views.ManageAuthTokens.as_view(), name='auth_token'), diff --git a/apps/member/views.py b/apps/member/views.py index d2acb8f1..65f60cca 100644 --- a/apps/member/views.py +++ b/apps/member/views.py @@ -207,7 +207,36 @@ class DeleteAliasView(LoginRequiredMixin, DeleteView): def get(self, request, *args, **kwargs): return self.post(request, *args, **kwargs) - + +class ProfilePictureUpdateView(LoginRequiredMixin, FormMixin, DetailView): + model = User + template_name = 'member/profile_picture_update.html' + context_object_name = 'user_object' + form_class = ImageForm + def get_context_data(self,*args,**kwargs): + context = super().get_context_data(*args,**kwargs) + context['form'] = self.form_class(self.request.POST,self.request.FILES) + return context + + def get_success_url(self): + return reverse_lazy('member:user_detail', kwargs={'pk': self.object.id}) + + def post(self,request,*args,**kwargs): + form = self.get_form() + self.object = self.get_object() + if form.is_valid(): + return self.form_valid(form) + else: + print('is_invalid') + print(form) + return self.form_invalid(form) + + def form_valid(self,form): + print(form) + self.object.note.display_image = form.cleaned_data.get("image","pic/default.png") + self.object.note.save() + return super().form_valid(form) + class ManageAuthTokens(LoginRequiredMixin, TemplateView): """ Affiche le jeton d'authentification, et permet de le regénérer diff --git a/apps/note/forms.py b/apps/note/forms.py index 27050207..af6c97bc 100644 --- a/apps/note/forms.py +++ b/apps/note/forms.py @@ -23,15 +23,11 @@ class AliasForm(forms.ModelForm): self.fields["name"].widget.attrs={"placeholder":_('New Alias')} -class ImageForm(forms.ModelForm): - class Meta: - model = Note - fields = ('display_image',) +class ImageForm(forms.Form): + image = forms.ImageField(required = False, + label=_('select an image'), + help_text=_('Maximal size: 2MB')) - def __init__(self,*args,**kwargs): - super().__init__(*args,**kwargs) - self.fields["display_image"].label = _("select an image") - self.fields["display_image"].widget.attrs={"help_text":_('Maximal size: 2MB')} class TransactionTemplateForm(forms.ModelForm): class Meta: diff --git a/apps/note/models/notes.py b/apps/note/models/notes.py index ea62bd75..81e851d8 100644 --- a/apps/note/models/notes.py +++ b/apps/note/models/notes.py @@ -43,7 +43,8 @@ class Note(PolymorphicModel): display_image = models.ImageField( verbose_name=_('display image'), max_length=255, - blank=True, + blank=False, + null=False, default='pic/default.png' ) created_at = models.DateTimeField( diff --git a/templates/member/profile_detail.html b/templates/member/profile_detail.html index 930352bd..e997b333 100644 --- a/templates/member/profile_detail.html +++ b/templates/member/profile_detail.html @@ -6,7 +6,9 @@
- + + +
@@ -46,7 +48,6 @@ {%if request.get_full_path != user_profile_url %} {% trans 'View Profile' %} {% endif %} - {% trans 'Change Avatar' %}
diff --git a/templates/member/profile_picture_update.html b/templates/member/profile_picture_update.html new file mode 100644 index 00000000..a5de13dc --- /dev/null +++ b/templates/member/profile_picture_update.html @@ -0,0 +1,63 @@ +{% extends "member/profile_detail.html" %} +{% load i18n static pretty_money django_tables2 crispy_forms_tags %} + +{% block profile_content %} +
+
+ +

{% trans "current image" %}

+
+
+
+ {% csrf_token %} + {{ form |crispy }} + +
+
+
+
+
+
+{% endblock %} +{% block extrajavascript%} + +{% endblock %}