From d3d87d37c336404e617d49187e2741795d3b150a Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Wed, 20 Nov 2019 20:07:35 +0100 Subject: [PATCH] Code Profile template. --- musician/api.py | 3 ++ musician/models.py | 40 +++++++++++++++++++ musician/templates/musician/profile.html | 51 ++++++++++++++++++++++++ musician/urls.py | 1 + musician/views.py | 28 +++++++++++-- 5 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 musician/templates/musician/profile.html diff --git a/musician/api.py b/musician/api.py index eb138e1..048f34e 100644 --- a/musician/api.py +++ b/musician/api.py @@ -19,6 +19,9 @@ API_PATHS = { 'mailbox-list': 'mailboxes/', 'mailinglist-list': 'lists/', # ... TODO (@slamora) complete list of backend URLs + + # other + 'payment-source-list': 'payment-sources/', } diff --git a/musician/models.py b/musician/models.py index 9ba8d00..28eb035 100644 --- a/musician/models.py +++ b/musician/models.py @@ -39,6 +39,46 @@ class OrchestraModel: return c +class BillingContact(OrchestraModel): + param_defaults = { + 'name': None, + 'address': None, + 'city': None, + 'zipcode': None, + 'country': None, + 'vat': None, + } + + +class PaymentSource(OrchestraModel): + api_name = 'payment-source' + param_defaults = { + "method": None, + "data": [], + "is_active": False, + } + + +class UserAccount(OrchestraModel): + api_name = 'accounts' + param_defaults = { + 'username': None, + 'type': None, + 'language': None, + 'short_name': None, + 'full_name': None, + 'billing': {}, + } + + @classmethod + def new_from_json(cls, data, **kwargs): + billing = None + + if 'billcontact' in data: + billing = BillingContact.new_from_json(data['billcontact']) + return super().new_from_json(data=data, billing=billing) + + class DatabaseUser(OrchestraModel): api_name = 'databaseusers' fields = ('username',) diff --git a/musician/templates/musician/profile.html b/musician/templates/musician/profile.html new file mode 100644 index 0000000..e9da7be --- /dev/null +++ b/musician/templates/musician/profile.html @@ -0,0 +1,51 @@ +{% extends "musician/base.html" %} +{% load i18n %} + +{% block content %} + +

Profile

+

Little description of what to be expected...

+ +
+
User information
+
+
+
+ {# #} +
+
+
+

{{ profile.username }}

+

{{ profile.type }}

+

Preferred language: {{ profile.language }}

+
+
+
+ +{% with profile.billing as contact %} +
+
Billing information
+
+
{{ contact.name }}
+
{{ contact.address }}
+
+ {{ contact.zipcode }} + {{ contact.city }} + {{ contact.country }} +
+
+ {{ contact.vat }} +
+ +
+ payment method: {{ payment.method }} +
+
+ {# TODO(@slamora) format payment method details #} + {{ payment.data.data }} +
+ +
+
+{% endwith %} +{% endblock %} diff --git a/musician/urls.py b/musician/urls.py index e18f828..e08bd0a 100644 --- a/musician/urls.py +++ b/musician/urls.py @@ -15,6 +15,7 @@ urlpatterns = [ path('auth/login/', views.LoginView.as_view(), name='login'), path('auth/logout/', views.LogoutView.as_view(), name='logout'), path('dashboard/', views.DashboardView.as_view(), name='dashboard'), + path('profile/', views.ProfileView.as_view(), name='profile'), path('mails/', views.MailView.as_view(), name='mails'), path('mailing-lists/', views.MailingListsView.as_view(), name='mailing-lists'), path('databases/', views.DatabasesView.as_view(), name='databases'), diff --git a/musician/views.py b/musician/views.py index 071f9ed..632f8ea 100644 --- a/musician/views.py +++ b/musician/views.py @@ -1,4 +1,5 @@ +from django.views.generic.detail import DetailView from itertools import groupby from django.core.exceptions import ImproperlyConfigured @@ -14,8 +15,9 @@ from . import api, get_version from .auth import login as auth_login from .auth import logout as auth_logout from .forms import LoginForm -from .mixins import (CustomContextMixin, ExtendedPaginationMixin, UserTokenRequiredMixin) -from .models import DatabaseService, MailinglistService, MailService +from .mixins import (CustomContextMixin, + ExtendedPaginationMixin, UserTokenRequiredMixin) +from .models import DatabaseService, MailinglistService, MailService, UserAccount, PaymentSource class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView): @@ -34,6 +36,25 @@ class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView): return context +class ProfileView(CustomContextMixin, UserTokenRequiredMixin, TemplateView): + template_name = "musician/profile.html" + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + json_data = self.orchestra.retreve_profile() + try: + pay_source = self.orchestra.retrieve_service_list( + PaymentSource.api_name)[0] + except IndexError: + pay_source = {} + context.update({ + 'profile': UserAccount.new_from_json(json_data[0]), + 'payment': PaymentSource.new_from_json(pay_source) + }) + + return context + + class ServiceListView(CustomContextMixin, ExtendedPaginationMixin, UserTokenRequiredMixin, ListView): """Base list view to all services""" service_class = None @@ -44,7 +65,8 @@ class ServiceListView(CustomContextMixin, ExtendedPaginationMixin, UserTokenRequ raise ImproperlyConfigured( "ServiceListView requires a definiton of 'service'") - json_qs = self.orchestra.retrieve_service_list(self.service_class.api_name) + json_qs = self.orchestra.retrieve_service_list( + self.service_class.api_name) return [self.service_class.new_from_json(data) for data in json_qs] def get_context_data(self, **kwargs):