From 35d5bcdf0f495201b329dea5b1d5fa517bed9e3b Mon Sep 17 00:00:00 2001 From: Pierre-antoine Comby Date: Sun, 1 Mar 2020 00:25:53 +0100 Subject: [PATCH] Deletion of alias now possible! --- apps/member/views.py | 19 ++++++++++++++++++- apps/note/models/notes.py | 8 -------- apps/note/tables.py | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/apps/member/views.py b/apps/member/views.py index ce3beb39..32b414a5 100644 --- a/apps/member/views.py +++ b/apps/member/views.py @@ -8,11 +8,14 @@ from django.utils.translation import gettext_lazy as _ from django.views.generic import CreateView, DetailView, UpdateView, TemplateView,DeleteView from django.views.generic.edit import FormMixin from django.contrib.auth.models import User +from django.contrib import messages from django.urls import reverse_lazy +from django.http import HttpResponseRedirect + from django.db.models import Q from django_tables2.views import SingleTableView from rest_framework.authtoken.models import Token - +from django.core.exceptions import ValidationError from note.models import Alias, NoteUser from note.models.transactions import Transaction from note.tables import HistoryTable, AliasTable @@ -189,8 +192,22 @@ class AliasView(LoginRequiredMixin,FormMixin,DetailView): class DeleteAliasView(LoginRequiredMixin, DeleteView): model = Alias + + def delete(self,request,*args,**kwargs): + try: + self.object = self.get_object() + self.object.delete() + except ValidationError as e: + # TODO: pass message to redirected view. + messages.error(self.request,str(e)) + else: + messages.success(self.request,_("Alias successfully deleted")) + return HttpResponseRedirect(get_success_url) + def get_success_url(self): + print(self.request) return reverse_lazy('member:user_alias',kwargs={'pk':self.object.note.user.pk}) + def get(self, request, *args, **kwargs): return self.post(request, *args, **kwargs) diff --git a/apps/note/models/notes.py b/apps/note/models/notes.py index 62811735..8ee83888 100644 --- a/apps/note/models/notes.py +++ b/apps/note/models/notes.py @@ -219,14 +219,6 @@ class Alias(models.Model): if all(not unicodedata.category(char).startswith(cat) for cat in {'M', 'P', 'Z', 'C'})).casefold() - def save(self, *args, **kwargs): - """ - Handle normalized_name - """ - self.normalized_name = Alias.normalize(self.name) - if len(self.normalized_name) < 256: - super().save(*args, **kwargs) - def clean(self): normalized_name = Alias.normalize(self.name) if len(normalized_name) >= 255: diff --git a/apps/note/tables.py b/apps/note/tables.py index c8dbf74a..588c3745 100644 --- a/apps/note/tables.py +++ b/apps/note/tables.py @@ -36,4 +36,4 @@ class AliasTable(tables.Table): template_name = 'django_tables2/bootstrap4.html' delete = tables.LinkColumn('member:user_alias_delete', args=[A('pk')], attrs={ - 'a': {'class': 'btn btn-danger'} },text='delete') + 'a': {'class': 'btn btn-danger'} },text='delete',accessor='pk')