Imprevements on SaaS app

This commit is contained in:
Marc 2014-09-26 21:24:23 +00:00
parent eec726fcc6
commit ccf50d0515
12 changed files with 59 additions and 30 deletions

View File

@ -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)

View File

@ -1 +1 @@
from .options import PaymentMethod, PaymentSourceDataForm
from .options import PaymentMethod

View File

@ -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."))

View File

@ -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)

View File

@ -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"),

View File

@ -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'

View File

@ -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)

View File

@ -11,3 +11,4 @@ class BSCWForm(SoftwareServiceForm):
class BSCWService(SoftwareService):
verbose_name = "BSCW"
form = BSCWForm
description_field = 'username'

View File

@ -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'

View File

@ -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]

View File

@ -0,0 +1 @@
from .options import *

View File

@ -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)