diff --git a/apps/member/urls.py b/apps/member/urls.py index 6a7ed5ce..fdb2e911 100644 --- a/apps/member/urls.py +++ b/apps/member/urls.py @@ -15,6 +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//aliases', views.AliasView.as_view(), name="user_alias"), path('manage-auth-token/', views.ManageAuthTokens.as_view(), name='auth_token'), # API for the user autocompleter diff --git a/apps/member/views.py b/apps/member/views.py index d03a94e0..3ff10e8a 100644 --- a/apps/member/views.py +++ b/apps/member/views.py @@ -6,14 +6,17 @@ from django.contrib.auth.mixins import LoginRequiredMixin from django.shortcuts import redirect from django.utils.translation import gettext_lazy as _ from django.views.generic import CreateView, DetailView, UpdateView, TemplateView +from django.views.generic.edit import FormMixin from django.contrib.auth.models import User from django.urls import reverse_lazy from django.db.models import Q from django_tables2.views import SingleTableView from rest_framework.authtoken.models import Token + from note.models import Alias, NoteUser from note.models.transactions import Transaction -from note.tables import HistoryTable +from note.tables import HistoryTable, AliasTable +from note.forms import AliasForm from .models import Profile, Club, Membership from .forms import SignUpForm, ProfileForm, ClubForm, MembershipForm, MemberFormSet, FormSetHelper @@ -153,6 +156,36 @@ class UserListView(LoginRequiredMixin, SingleTableView): context["filter"] = self.filter return context +class AliasView(LoginRequiredMixin,FormMixin,DetailView): + model = User + template_name = 'member/user_alias.html' + context_object_name = 'user_object' + form_class = AliasForm + + def get_context_data(self,**kwargs): + context = super().get_context_data(**kwargs) + context["aliases"] = AliasTable(context['user_object'].note.alias_set.all()) + context["alias_form"] = AliasForm() + return context + + def get_success_url(self): + return reverse_lazy('member:user_alias', kwargs={'pk': self.object.id}) + + def post(self,request,*args,**kwargs): + self.object = self.get_object() + form = self.get_form() + if form.is_valid(): + return self.form_valid(form) + else: + return self.form_invalid(form) + + def form_valid(self, form): + alias = form.save(commit=False) + alias.note = self.object.note + alias.save() + alias.note.save() + print(alias,alias.pk) + return super().form_valid(form) class ManageAuthTokens(LoginRequiredMixin, TemplateView): """ diff --git a/apps/note/forms.py b/apps/note/forms.py index 3222acec..d920b9aa 100644 --- a/apps/note/forms.py +++ b/apps/note/forms.py @@ -6,6 +6,12 @@ from django import forms from django.utils.translation import gettext_lazy as _ from .models import Transaction, TransactionTemplate, TemplateTransaction +from .models import Alias + +class AliasForm(forms.ModelForm): + class Meta: + model = Alias + fields = ("name",) class TransactionTemplateForm(forms.ModelForm): diff --git a/apps/note/tables.py b/apps/note/tables.py index 43a1ef74..2248c412 100644 --- a/apps/note/tables.py +++ b/apps/note/tables.py @@ -5,7 +5,7 @@ import django_tables2 as tables from django.db.models import F from .models.transactions import Transaction - +from .models.notes import Alias class HistoryTable(tables.Table): class Meta: @@ -24,3 +24,16 @@ class HistoryTable(tables.Table): queryset = queryset.annotate(total=F('amount') * F('quantity')) \ .order_by(('-' if is_descending else '') + 'total') return (queryset, True) + +class AliasTable(tables.Table): + class Meta: + attrs = { + 'class': + 'table table condensed table-striped table-hover' + } + model = Alias + fields = ('name',) + template_name = 'django_tables2/bootstrap4.html' + + delete = tables.LinkColumn('member:user_alias_delete', args=[A('id')], attrs={ + 'a': {'class': 'btn'} }) diff --git a/templates/member/profile_detail.html b/templates/member/profile_detail.html index 6b5c127a..0a19eecd 100644 --- a/templates/member/profile_detail.html +++ b/templates/member/profile_detail.html @@ -30,7 +30,7 @@
{% trans 'balance'|capfirst %}
{{ object.note.balance | pretty_money }}
-
{% trans 'aliases'|capfirst %}
+
{% trans 'aliases'|capfirst %}
{{ object.note.alias_set.all|join:", " }}
diff --git a/templates/member/user_alias.html b/templates/member/user_alias.html new file mode 100644 index 00000000..f1a06e7e --- /dev/null +++ b/templates/member/user_alias.html @@ -0,0 +1,65 @@ +{% extends "base.html" %} +{% load i18n static pretty_money django_tables2 crispy_forms_tags %} + +{% block content %} +
+
+
+ +
+
+
{% trans 'name'|capfirst %}, {% trans 'first name' %}
+
{{ object.last_name }} {{ object.first_name }}
+ +
{% trans 'username'|capfirst %}
+
{{ object.username }}
+ +
{% trans 'password'|capfirst %}
+
+ + {% trans 'Change password' %} + +
+ +
{% trans 'section'|capfirst %}
+
{{ object.profile.section }}
+ +
{% trans 'address'|capfirst %}
+
{{ object.profile.address }}
+ +
{% trans 'balance'|capfirst %}
+
{{ object.note.balance | pretty_money }}
+ +
{% trans 'aliases'|capfirst %}
+
{{ object.note.alias_set.all|join:", " }}
+
+ + {% if object.pk == user.pk %} + {% trans 'Manage auth token' %} + {% endif %} +
+ +
+
+ +
+
+
+ {% csrf_token %} + {{ alias_form |crispy }} + +
+
+
+
+ {% render_table aliases %} +
+
+
+
+{% endblock %}