From ccf50d05154b995e3bacb781f3cae63754ec7d21 Mon Sep 17 00:00:00 2001 From: Marc Date: Fri, 26 Sep 2014 21:24:23 +0000 Subject: [PATCH] Imprevements on SaaS app --- orchestra/admin/options.py | 2 +- orchestra/apps/payments/methods/__init__.py | 2 +- orchestra/apps/payments/methods/creditcard.py | 6 +++-- orchestra/apps/payments/methods/options.py | 22 +-------------- .../apps/payments/methods/sepadirectdebit.py | 6 +++-- orchestra/apps/saas/admin.py | 2 +- orchestra/apps/saas/models.py | 9 +++++++ orchestra/apps/saas/services/bscw.py | 1 + orchestra/apps/saas/services/gitlab.py | 2 ++ orchestra/apps/saas/services/options.py | 9 +++++-- orchestra/forms/__init__.py | 1 + orchestra/forms/options.py | 27 +++++++++++++++++++ 12 files changed, 59 insertions(+), 30 deletions(-) create mode 100644 orchestra/forms/options.py diff --git a/orchestra/admin/options.py b/orchestra/admin/options.py index fdf8c534..795378e7 100644 --- a/orchestra/admin/options.py +++ b/orchestra/admin/options.py @@ -138,7 +138,7 @@ class SelectPluginAdminMixin(object): def get_form(self, request, obj=None, **kwargs): if obj: - self.form = obj.method_class().get_form() + self.form = getattr(obj, '%s_class' % self.plugin_field)().get_form() else: self.form = self.plugin.get_plugin(self.plugin_value)().get_form() return super(SelectPluginAdminMixin, self).get_form(request, obj=obj, **kwargs) diff --git a/orchestra/apps/payments/methods/__init__.py b/orchestra/apps/payments/methods/__init__.py index 17af0c2e..9a9aaa55 100644 --- a/orchestra/apps/payments/methods/__init__.py +++ b/orchestra/apps/payments/methods/__init__.py @@ -1 +1 @@ -from .options import PaymentMethod, PaymentSourceDataForm +from .options import PaymentMethod diff --git a/orchestra/apps/payments/methods/creditcard.py b/orchestra/apps/payments/methods/creditcard.py index 89de5ffd..b71872d0 100644 --- a/orchestra/apps/payments/methods/creditcard.py +++ b/orchestra/apps/payments/methods/creditcard.py @@ -2,10 +2,12 @@ from django import forms from django.utils.translation import ugettext_lazy as _ from rest_framework import serializers -from .options import PaymentSourceDataForm, PaymentMethod +from orchestra.forms import PluginDataForm + +from .options import PaymentMethod -class CreditCardForm(PaymentSourceDataForm): +class CreditCardForm(PluginDataForm): label = forms.CharField(max_length=128, label=_("Label"), help_text=_("Use a name such as \"Jo's Visa\" to remember which " "card it is.")) diff --git a/orchestra/apps/payments/methods/options.py b/orchestra/apps/payments/methods/options.py index 49e61ba6..d22969ae 100644 --- a/orchestra/apps/payments/methods/options.py +++ b/orchestra/apps/payments/methods/options.py @@ -26,6 +26,7 @@ class PaymentMethod(plugins.Plugin): def get_form(self): self.form.plugin = self + self.form.plugin_field = 'method' return self.form def get_serializer(self): @@ -40,24 +41,3 @@ class PaymentMethod(plugins.Plugin): def get_bill_message(self, source): return '' - - -class PaymentSourceDataForm(forms.ModelForm): - class Meta: - exclude = ('data', 'method') - - def __init__(self, *args, **kwargs): - super(PaymentSourceDataForm, self).__init__(*args, **kwargs) - instance = kwargs.get('instance') - if instance: - for field in self.declared_fields: - initial = self.fields[field].initial - self.fields[field].initial = instance.data.get(field, initial) - - def save(self, commit=True): - plugin = self.plugin - self.instance.method = plugin.get_plugin_name() - self.instance.data = { - field: self.cleaned_data[field] for field in self.declared_fields - } - return super(PaymentSourceDataForm, self).save(commit=commit) diff --git a/orchestra/apps/payments/methods/sepadirectdebit.py b/orchestra/apps/payments/methods/sepadirectdebit.py index 88cf1aff..97f305e9 100644 --- a/orchestra/apps/payments/methods/sepadirectdebit.py +++ b/orchestra/apps/payments/methods/sepadirectdebit.py @@ -12,11 +12,13 @@ from django_iban.forms import IBANFormField from django_iban.validators import IBANValidator, IBAN_COUNTRY_CODE_LENGTH from rest_framework import serializers +from orchestra.forms import PluginDataForm + from .. import settings -from .options import PaymentSourceDataForm, PaymentMethod +from .options import PaymentMethod -class SEPADirectDebitForm(PaymentSourceDataForm): +class SEPADirectDebitForm(PluginDataForm): iban = IBANFormField(label='IBAN', widget=forms.TextInput(attrs={'size': '50'})) name = forms.CharField(max_length=128, label=_("Name"), diff --git a/orchestra/apps/saas/admin.py b/orchestra/apps/saas/admin.py index 8856c47a..b6e8557f 100644 --- a/orchestra/apps/saas/admin.py +++ b/orchestra/apps/saas/admin.py @@ -8,7 +8,7 @@ from .services import SoftwareService class SaaSAdmin(SelectPluginAdminMixin, AccountAdminMixin, admin.ModelAdmin): - list_display = ('id', 'service', 'account_link') + list_display = ('description', 'service', 'account_link') list_filter = ('service',) plugin = SoftwareService plugin_field = 'service' diff --git a/orchestra/apps/saas/models.py b/orchestra/apps/saas/models.py index 6a3a938a..f0053fc2 100644 --- a/orchestra/apps/saas/models.py +++ b/orchestra/apps/saas/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.utils.functional import cached_property from django.utils.translation import ugettext_lazy as _ from jsonfield import JSONField @@ -16,6 +17,14 @@ class SaaS(models.Model): class Meta: verbose_name = "SaaS" verbose_name_plural = "SaaS" + + @cached_property + def service_class(self): + return SoftwareService.get_plugin(self.service) + + @cached_property + def description(self): + return self.service_class().get_description(self.data) services.register(SaaS) diff --git a/orchestra/apps/saas/services/bscw.py b/orchestra/apps/saas/services/bscw.py index 7cc4cced..ae6e2781 100644 --- a/orchestra/apps/saas/services/bscw.py +++ b/orchestra/apps/saas/services/bscw.py @@ -11,3 +11,4 @@ class BSCWForm(SoftwareServiceForm): class BSCWService(SoftwareService): verbose_name = "BSCW" form = BSCWForm + description_field = 'username' diff --git a/orchestra/apps/saas/services/gitlab.py b/orchestra/apps/saas/services/gitlab.py index 9aa5b57c..7a58d20d 100644 --- a/orchestra/apps/saas/services/gitlab.py +++ b/orchestra/apps/saas/services/gitlab.py @@ -6,8 +6,10 @@ from .options import SoftwareService, SoftwareServiceForm class GitLabForm(SoftwareServiceForm): project_name = forms.CharField(label=_("Project name"), max_length=64) + email = forms.CharField(label=_("Email"), max_length=64) class GitLabService(SoftwareService): verbose_name = "GitLab" form = GitLabForm + description_field = 'project_name' diff --git a/orchestra/apps/saas/services/options.py b/orchestra/apps/saas/services/options.py index 49999bca..bcf86bca 100644 --- a/orchestra/apps/saas/services/options.py +++ b/orchestra/apps/saas/services/options.py @@ -1,6 +1,7 @@ from django import forms from django.utils.translation import ugettext_lazy as _ +from orchestra.forms import PluginDataForm from orchestra.utils import plugins from orchestra.utils.functional import cached from orchestra.utils.python import import_class @@ -8,7 +9,7 @@ from orchestra.utils.python import import_class from .. import settings -class SoftwareServiceForm(forms.ModelForm): +class SoftwareServiceForm(PluginDataForm): username = forms.CharField(label=_("Username"), max_length=64) password = forms.CharField(label=_("Password"), max_length=64) @@ -17,7 +18,7 @@ class SoftwareServiceForm(forms.ModelForm): class SoftwareService(plugins.Plugin): - label_field = 'label' + description_field = '' form = SoftwareServiceForm serializer = None @@ -31,8 +32,12 @@ class SoftwareService(plugins.Plugin): def get_form(self): self.form.plugin = self + self.form.plugin_field = 'service' return self.form def get_serializer(self): self.serializer.plugin = self return self.serializer + + def get_description(self, data): + return data[self.description_field] diff --git a/orchestra/forms/__init__.py b/orchestra/forms/__init__.py index e69de29b..921dfe14 100644 --- a/orchestra/forms/__init__.py +++ b/orchestra/forms/__init__.py @@ -0,0 +1 @@ +from .options import * diff --git a/orchestra/forms/options.py b/orchestra/forms/options.py new file mode 100644 index 00000000..796317d5 --- /dev/null +++ b/orchestra/forms/options.py @@ -0,0 +1,27 @@ +from django import forms + + +class PluginDataForm(forms.ModelForm): + class Meta: + exclude = ('data',) + + def __init__(self, *args, **kwargs): + super(PluginDataForm, self).__init__(*args, **kwargs) + # TODO remove it weel + try: + self.fields[self.plugin_field].widget = forms.HiddenInput() + except KeyError: + pass + instance = kwargs.get('instance') + if instance: + for field in self.declared_fields: + initial = self.fields[field].initial + self.fields[field].initial = instance.data.get(field, initial) + + def save(self, commit=True): + plugin = self.plugin + setattr(self.instance, self.plugin_field, plugin.get_plugin_name()) + self.instance.data = { + field: self.cleaned_data[field] for field in self.declared_fields + } + return super(PluginDataForm, self).save(commit=commit)