Imprevements on SaaS app
This commit is contained in:
parent
eec726fcc6
commit
ccf50d0515
|
@ -138,7 +138,7 @@ class SelectPluginAdminMixin(object):
|
||||||
|
|
||||||
def get_form(self, request, obj=None, **kwargs):
|
def get_form(self, request, obj=None, **kwargs):
|
||||||
if obj:
|
if obj:
|
||||||
self.form = obj.method_class().get_form()
|
self.form = getattr(obj, '%s_class' % self.plugin_field)().get_form()
|
||||||
else:
|
else:
|
||||||
self.form = self.plugin.get_plugin(self.plugin_value)().get_form()
|
self.form = self.plugin.get_plugin(self.plugin_value)().get_form()
|
||||||
return super(SelectPluginAdminMixin, self).get_form(request, obj=obj, **kwargs)
|
return super(SelectPluginAdminMixin, self).get_form(request, obj=obj, **kwargs)
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
from .options import PaymentMethod, PaymentSourceDataForm
|
from .options import PaymentMethod
|
||||||
|
|
|
@ -2,10 +2,12 @@ from django import forms
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from rest_framework import serializers
|
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"),
|
label = forms.CharField(max_length=128, label=_("Label"),
|
||||||
help_text=_("Use a name such as \"Jo's Visa\" to remember which "
|
help_text=_("Use a name such as \"Jo's Visa\" to remember which "
|
||||||
"card it is."))
|
"card it is."))
|
||||||
|
|
|
@ -26,6 +26,7 @@ class PaymentMethod(plugins.Plugin):
|
||||||
|
|
||||||
def get_form(self):
|
def get_form(self):
|
||||||
self.form.plugin = self
|
self.form.plugin = self
|
||||||
|
self.form.plugin_field = 'method'
|
||||||
return self.form
|
return self.form
|
||||||
|
|
||||||
def get_serializer(self):
|
def get_serializer(self):
|
||||||
|
@ -40,24 +41,3 @@ class PaymentMethod(plugins.Plugin):
|
||||||
|
|
||||||
def get_bill_message(self, source):
|
def get_bill_message(self, source):
|
||||||
return ''
|
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)
|
|
||||||
|
|
|
@ -12,11 +12,13 @@ from django_iban.forms import IBANFormField
|
||||||
from django_iban.validators import IBANValidator, IBAN_COUNTRY_CODE_LENGTH
|
from django_iban.validators import IBANValidator, IBAN_COUNTRY_CODE_LENGTH
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
from orchestra.forms import PluginDataForm
|
||||||
|
|
||||||
from .. import settings
|
from .. import settings
|
||||||
from .options import PaymentSourceDataForm, PaymentMethod
|
from .options import PaymentMethod
|
||||||
|
|
||||||
|
|
||||||
class SEPADirectDebitForm(PaymentSourceDataForm):
|
class SEPADirectDebitForm(PluginDataForm):
|
||||||
iban = IBANFormField(label='IBAN',
|
iban = IBANFormField(label='IBAN',
|
||||||
widget=forms.TextInput(attrs={'size': '50'}))
|
widget=forms.TextInput(attrs={'size': '50'}))
|
||||||
name = forms.CharField(max_length=128, label=_("Name"),
|
name = forms.CharField(max_length=128, label=_("Name"),
|
||||||
|
|
|
@ -8,7 +8,7 @@ from .services import SoftwareService
|
||||||
|
|
||||||
|
|
||||||
class SaaSAdmin(SelectPluginAdminMixin, AccountAdminMixin, admin.ModelAdmin):
|
class SaaSAdmin(SelectPluginAdminMixin, AccountAdminMixin, admin.ModelAdmin):
|
||||||
list_display = ('id', 'service', 'account_link')
|
list_display = ('description', 'service', 'account_link')
|
||||||
list_filter = ('service',)
|
list_filter = ('service',)
|
||||||
plugin = SoftwareService
|
plugin = SoftwareService
|
||||||
plugin_field = 'service'
|
plugin_field = 'service'
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.utils.functional import cached_property
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from jsonfield import JSONField
|
from jsonfield import JSONField
|
||||||
|
|
||||||
|
@ -17,5 +18,13 @@ class SaaS(models.Model):
|
||||||
verbose_name = "SaaS"
|
verbose_name = "SaaS"
|
||||||
verbose_name_plural = "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)
|
services.register(SaaS)
|
||||||
|
|
|
@ -11,3 +11,4 @@ class BSCWForm(SoftwareServiceForm):
|
||||||
class BSCWService(SoftwareService):
|
class BSCWService(SoftwareService):
|
||||||
verbose_name = "BSCW"
|
verbose_name = "BSCW"
|
||||||
form = BSCWForm
|
form = BSCWForm
|
||||||
|
description_field = 'username'
|
||||||
|
|
|
@ -6,8 +6,10 @@ from .options import SoftwareService, SoftwareServiceForm
|
||||||
|
|
||||||
class GitLabForm(SoftwareServiceForm):
|
class GitLabForm(SoftwareServiceForm):
|
||||||
project_name = forms.CharField(label=_("Project name"), max_length=64)
|
project_name = forms.CharField(label=_("Project name"), max_length=64)
|
||||||
|
email = forms.CharField(label=_("Email"), max_length=64)
|
||||||
|
|
||||||
|
|
||||||
class GitLabService(SoftwareService):
|
class GitLabService(SoftwareService):
|
||||||
verbose_name = "GitLab"
|
verbose_name = "GitLab"
|
||||||
form = GitLabForm
|
form = GitLabForm
|
||||||
|
description_field = 'project_name'
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
|
from orchestra.forms import PluginDataForm
|
||||||
from orchestra.utils import plugins
|
from orchestra.utils import plugins
|
||||||
from orchestra.utils.functional import cached
|
from orchestra.utils.functional import cached
|
||||||
from orchestra.utils.python import import_class
|
from orchestra.utils.python import import_class
|
||||||
|
@ -8,7 +9,7 @@ from orchestra.utils.python import import_class
|
||||||
from .. import settings
|
from .. import settings
|
||||||
|
|
||||||
|
|
||||||
class SoftwareServiceForm(forms.ModelForm):
|
class SoftwareServiceForm(PluginDataForm):
|
||||||
username = forms.CharField(label=_("Username"), max_length=64)
|
username = forms.CharField(label=_("Username"), max_length=64)
|
||||||
password = forms.CharField(label=_("Password"), max_length=64)
|
password = forms.CharField(label=_("Password"), max_length=64)
|
||||||
|
|
||||||
|
@ -17,7 +18,7 @@ class SoftwareServiceForm(forms.ModelForm):
|
||||||
|
|
||||||
|
|
||||||
class SoftwareService(plugins.Plugin):
|
class SoftwareService(plugins.Plugin):
|
||||||
label_field = 'label'
|
description_field = ''
|
||||||
form = SoftwareServiceForm
|
form = SoftwareServiceForm
|
||||||
serializer = None
|
serializer = None
|
||||||
|
|
||||||
|
@ -31,8 +32,12 @@ class SoftwareService(plugins.Plugin):
|
||||||
|
|
||||||
def get_form(self):
|
def get_form(self):
|
||||||
self.form.plugin = self
|
self.form.plugin = self
|
||||||
|
self.form.plugin_field = 'service'
|
||||||
return self.form
|
return self.form
|
||||||
|
|
||||||
def get_serializer(self):
|
def get_serializer(self):
|
||||||
self.serializer.plugin = self
|
self.serializer.plugin = self
|
||||||
return self.serializer
|
return self.serializer
|
||||||
|
|
||||||
|
def get_description(self, data):
|
||||||
|
return data[self.description_field]
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
from .options import *
|
|
@ -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)
|
Loading…
Reference in New Issue