Permalink
Browse files

create a CommonLanguageDesc abstract class and a LanguageDescModelFor…

…m for DocURL and EnableURL classes, move common template code to different files for reuse and small fixes from pull request 9
  • Loading branch information...
1 parent bfa20e7 commit 77257926dc26c381785d3e1e4dc003ef8b5bcdf8 @sergiocharpineljr sergiocharpineljr committed Aug 17, 2012
View
@@ -13,6 +13,7 @@
from django.conf import settings
from django.utils.translation import get_language_info
from django.utils.functional import curry
+from django.utils.translation.trans_real import parse_accept_lang_header
import ispdb.audit as audit
class Domain(models.Model):
@@ -188,49 +189,56 @@ class Issue(models.Model):
status = models.CharField(max_length=20, choices=STATUS_CHOICES,
default="open")
-class DocURL(models.Model):
+
+class CommonConfigURL(models.Model):
url = models.URLField(
- verbose_name="URL of the page describing these settings")
- config = models.ForeignKey(Config)
+ verbose_name="URL of the page")
+ config = models.ForeignKey(Config, related_name="%(class)s_set")
+
+ class Meta:
+ abstract = True
def __str__(self): return str(self.url)
def __unicode__(self): return self.url
-class DocURLDesc(models.Model):
+
+class CommonURLDesc(models.Model):
description = models.TextField(
max_length=100,
- verbose_name="Description of the settings page")
+ verbose_name="Description")
language = models.CharField(
max_length=10,
verbose_name="Language",
choices=settings.LANGUAGES)
- docurl = models.ForeignKey(DocURL, related_name="descriptions")
+
+ class Meta:
+ abstract = True
def __str__(self): return str(self.description)
def __unicode__(self): return self.description
-class EnableURL(models.Model):
- url = models.URLField(
- verbose_name="URL of the page with enable instructions")
- config = models.ForeignKey(Config)
+class DocURL(CommonConfigURL):
+ pass
+DocURL._meta.get_field('url').verbose_name = ("URL of the page describing "
+ "these settings")
- def __str__(self): return str(self.url)
- def __unicode__(self): return self.url
+class DocURLDesc(CommonURLDesc):
+ docurl = models.ForeignKey(DocURL, related_name="descriptions")
+DocURLDesc._meta.get_field('description').verbose_name = ('Description of the '
+ 'settings page')
-class EnableURLInst(models.Model):
- instruction = models.TextField(
- max_length=100,
- verbose_name="Instruction")
- language = models.CharField(
- max_length=10,
- verbose_name="Language",
- choices=settings.LANGUAGES)
- enableurl = models.ForeignKey(EnableURL, related_name="instructions")
- def __str__(self): return str(self.instruction)
- def __unicode__(self): return self.instruction
+class EnableURL(CommonConfigURL):
+ pass
+EnableURL._meta.get_field('url').verbose_name = ("URL of the page with enable "
+ "instructions")
+
+
+class EnableURLInst(CommonURLDesc):
+ enableurl = models.ForeignKey(EnableURL, related_name="instructions")
+EnableURLInst._meta.get_field('description').verbose_name = ('Instruction')
# Forms
@@ -272,6 +280,18 @@ def clean(self, **kwargs):
self.errors.clear()
return cleaned_data
+ def save(self, commit=True):
+ super(DynamicModelForm, self).save(commit=False)
+ if commit:
+ # delete if exists
+ if self.cleaned_data and self.cleaned_data['DELETE']:
+ if self.instance.pk:
+ return self.instance.delete()
+ else:
+ return None
+ super(DynamicModelForm, self).save()
+ return self.instance
+
class DynamicBaseModelFormSet(BaseModelFormSet):
"""
@@ -312,20 +332,18 @@ class Meta:
fields = ['title', 'description']
-class DocURLDescForm(DynamicModelForm):
- class Meta:
- model = DocURLDesc
- exclude = ['docurl']
-
+class LanguageDescModelForm(DynamicModelForm):
+ """
+ A DynamicModelForm for classes with a language and a description fields
+ """
def __init__(self, *args, **kwargs):
http_accept_language = kwargs.pop('http_accept_language', '')
- super(DocURLDescForm, self).__init__(*args, **kwargs)
- self.fields['description'].widget.attrs.update({'rows': 2, 'cols': 20})
+ super(LanguageDescModelForm, self).__init__(*args, **kwargs)
+ self.fields['description'].widget.attrs.update({'rows': 1, 'cols': 20})
choices = []
# add HTTP_ACCEPTED_LANG first
if http_accept_language:
- codes = re.split(';|,', http_accept_language)
- for code in codes:
+ for code, q in parse_accept_lang_header(http_accept_language):
try:
li = get_language_info(code)
except:
@@ -341,17 +359,11 @@ def __init__(self, *args, **kwargs):
choices.append((code, lang + ' - ' + li['name_local']))
self.fields['language'].choices = choices
- def save(self, commit=True):
- super(DocURLDescForm, self).save(commit=False)
- if commit:
- # delete if exists
- if self.cleaned_data and self.cleaned_data['DELETE']:
- if self.instance.pk:
- return self.instance.delete()
- else:
- return None
- super(DocURLDescForm, self).save()
- return self.instance
+
+class DocURLDescForm(LanguageDescModelForm):
+ class Meta:
+ model = DocURLDesc
+ exclude = ['docurl']
class BaseDocURLDescFormSet(DynamicBaseModelFormSet):
@@ -479,47 +491,11 @@ def delete(self):
form.instance.delete()
-class EnableURLInstForm(DynamicModelForm):
+class EnableURLInstForm(LanguageDescModelForm):
class Meta:
model = EnableURLInst
exclude = ['enableurl']
- def __init__(self, *args, **kwargs):
- http_accept_language = kwargs.pop('http_accept_language', '')
- super(EnableURLInstForm, self).__init__(*args, **kwargs)
- self.fields['instruction'].widget.attrs.update({'rows': 2, 'cols': 20})
- choices = []
- # add HTTP_ACCEPTED_LANG first
- if http_accept_language:
- codes = re.split(';|,', http_accept_language)
- for code in codes:
- try:
- li = get_language_info(code)
- except:
- continue
- choices.append((code, li['name'] + ' - ' + li['name_local']))
- # Redefine our choices, so we can add the translated language names and
- # sort the list by language name
- choices.append(self.fields['language'].choices[0])
- langs = self.fields['language'].choices[1:]
- langs.sort(key=lambda l: l[1].lower())
- for code, lang in langs:
- li = get_language_info(code)
- choices.append((code, lang + ' - ' + li['name_local']))
- self.fields['language'].choices = choices
-
- def save(self, commit=True):
- super(EnableURLInstForm, self).save(commit=False)
- if commit:
- # delete if exists
- if self.cleaned_data and self.cleaned_data['DELETE']:
- if self.instance.pk:
- return self.instance.delete()
- else:
- return None
- super(EnableURLInstForm, self).save()
- return self.instance
-
class BaseEnableURLInstFormSet(DynamicBaseModelFormSet):
model = EnableURLInst
@@ -612,12 +588,12 @@ def __init__(self, *args, **kwargs):
prefix=prefix,
delete=delete,
queryset=qs)
- # create a empty_inst_form
+ # create a empty_desc_form
if self.forms:
- self.empty_inst_form = self.forms[0].inst_formset.empty_form
+ self.empty_desc_form = self.forms[0].inst_formset.empty_form
else:
formset = self.EnableURLInstFormSet()
- self.empty_inst_form = formset.empty_form
+ self.empty_desc_form = formset.empty_form
def clean(self, **kwargs):
super(BaseEnableURLFormSet, self).clean(**kwargs)
@@ -76,7 +76,7 @@ def xmlOneDotOne(data):
for inst in enableurl.instructions.all():
d = ET.SubElement(enable, "instruction")
d.attrib["lang"] = inst.language
- d.text = unicode(inst.instruction)
+ d.text = unicode(inst.description)
# DocURL
for docurl in data.docurl_set.all():
doc = ET.SubElement(emailProvider, "documentation")
@@ -91,6 +91,6 @@
margin-top: 10px;
}
-.tab_desc, .tab_inst {
+.tab_desc {
margin-left: 10px;
}
@@ -63,6 +63,6 @@
float: right;
}
-.tab_desc, .tab_inst {
+.tab_desc {
margin-left: 10px;
}
@@ -67,7 +67,7 @@
"model": "config.enableurlinst",
"fields": {
"enableurl": 1,
- "instruction": "test",
+ "description": "test",
"language": "en"
}
}
@@ -90,7 +90,7 @@
"model": "config.enableurlinst",
"fields": {
"enableurl": 1,
- "instruction": "test",
+ "description": "test",
"language": "en"
}
},
@@ -107,7 +107,7 @@
"model": "config.enableurlinst",
"fields": {
"enableurl": 2,
- "instruction": "test",
+ "description": "test",
"language": "en"
}
},
@@ -116,7 +116,7 @@
"model": "config.enableurlinst",
"fields": {
"enableurl": 2,
- "instruction": "test2",
+ "description": "test2",
"language": "fr"
}
}
@@ -253,18 +253,10 @@ <h4>Outgoing Server</h4>
{% if docurls %}
<div id="docurl">
<h4>Documentation</h4>
- {% for docurl in docurls %}
- <table>
- <tr>
- <td>{{ docurl.url.label}}:</td><td>{{ docurl.url.value}}</td>
- </tr>
- </table>
- {% for desc in docurl.desc_formset %}
- <table class="tab_desc">
- <tr>
- <td>{{ desc.description.label }}:</td><td>{{ desc.description.value }}</td><td> {{ desc.language|data_verbose_field }}</td>
- </tr>
- </table>
+ {% for form in docurls %}
+ {% include "config/urls/url_list.html" %}
+ {% for desc_form in form.desc_formset %}
+ {% include "config/urls/desc_list.html" %}
{% endfor %}
{% endfor %}
</div>
@@ -273,18 +265,10 @@ <h4>Documentation</h4>
{% if enableurls %}
<div id="enableurl">
<h4>Enable Instructions</h4>
- {% for enableurl in enableurls %}
- <table>
- <tr>
- <td>{{ enableurl.url.label}}:</td><td>{{ enableurl.url.value}}</td>
- </tr>
- </table>
- {% for inst in enableurl.inst_formset %}
- <table class="tab_inst">
- <tr>
- <td>{{ inst.instruction.label }}:</td><td>{{ inst.instruction.value }}</td><td> {{ inst.language|data_verbose_field }}</td>
- </tr>
- </table>
+ {% for form in enableurls %}
+ {% include "config/urls/url_list.html" %}
+ {% for desc_form in form.inst_formset %}
+ {% include "config/urls/desc_list.html" %}
{% endfor %}
{% endfor %}
</div>
Oops, something went wrong.

0 comments on commit 7725792

Please sign in to comment.