Commit b9e65644 authored by Leonidas Poulopoulos's avatar Leonidas Poulopoulos

Added urls in institution form

parent 54a9c979
settings.py settings.py
urls.py urls.py
*.pyc *.pyc
local.db
...@@ -47,7 +47,6 @@ class NameFormSetFact(BaseGenericInlineFormSet): ...@@ -47,7 +47,6 @@ class NameFormSetFact(BaseGenericInlineFormSet):
emptyForms = True emptyForms = True
for i in range(0, self.total_form_count()): for i in range(0, self.total_form_count()):
form = self.forms[i] form = self.forms[i]
pprint.pprint(form.__dict__)
if len(form.cleaned_data) != 0: if len(form.cleaned_data) != 0:
emptyForms = False emptyForms = False
langs.append(form.cleaned_data.get('lang', None)) langs.append(form.cleaned_data.get('lang', None))
...@@ -59,30 +58,26 @@ class NameFormSetFact(BaseGenericInlineFormSet): ...@@ -59,30 +58,26 @@ class NameFormSetFact(BaseGenericInlineFormSet):
class UrlFormSetFact(BaseGenericInlineFormSet): class UrlFormSetFact(BaseGenericInlineFormSet):
def clean(self): def clean(self):
#raise forms.ValidationError, "Clean method called"
if any(self.errors): if any(self.errors):
return return
pprint.pprint(self.forms)
for i in range(0, self.total_form_count()): for i in range(0, self.total_form_count()):
form = self.forms[i] form = self.forms[i]
pprint.pprint(len(form.cleaned_data))
if len(form.cleaned_data) == 0: if len(form.cleaned_data) == 0:
#raise forms.ValidationError, "Fill in at least one url "
pass pass
#print "ERROROROROR"
#self.append_non_form_error("not enough subs")
# pass
return return
# raise forms.ValidationError('Invalid date range') class UrlFormSetFactInst(BaseGenericInlineFormSet):
def clean(self):
if any(self.errors):
# def save(self, commit=True, request=None): return
# for uform in self.forms: url_types = []
# urls = uform.save(commit=False) emptyForms = True
# urls.content_object = self.instance for i in range(0, self.total_form_count()):
# urls.save() form = self.forms[i]
# return self.save_existing_objects(commit) + self.save_new_objects(commit) if len(form.cleaned_data) != 0:
emptyForms = False
url_types.append(form.cleaned_data.get('urltype',None))
if emptyForms:
raise forms.ValidationError, "Fill in at least the info url"
if "info" not in url_types:
raise forms.ValidationError, "Fill in at least the info url"
...@@ -86,22 +86,30 @@ def add_institution_details(request, institution_pk): ...@@ -86,22 +86,30 @@ def add_institution_details(request, institution_pk):
try: try:
inst_details = InstitutionDetails.objects.get(institution=inst) inst_details = InstitutionDetails.objects.get(institution=inst)
form = InstDetailsForm(instance=inst_details) form = InstDetailsForm(instance=inst_details)
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFactInst, can_delete=True)
except InstitutionDetails.DoesNotExist: except InstitutionDetails.DoesNotExist:
form = InstDetailsForm() form = InstDetailsForm()
form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=institution_pk), empty_label=None) form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=institution_pk), empty_label=None)
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, can_delete=True)
urls_form = UrlFormSet(prefix='urlsform')
form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst))) form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
return render_to_response('edumanage/institution_edit.html', { 'institution': inst, 'form': form}, return render_to_response('edumanage/institution_edit.html', { 'institution': inst, 'form': form, 'urls_form':urls_form},
context_instance=RequestContext(request, base_response(request))) context_instance=RequestContext(request, base_response(request)))
elif request.method == 'POST': elif request.method == 'POST':
request_data = request.POST.copy() request_data = request.POST.copy()
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFactInst, can_delete=True)
try: try:
inst_details = InstitutionDetails.objects.get(institution=inst) inst_details = InstitutionDetails.objects.get(institution=inst)
form = InstDetailsForm(request_data, instance=inst_details) form = InstDetailsForm(request_data, instance=inst_details)
urls_form = UrlFormSet(request_data, instance=inst_details, prefix='urlsform')
except InstitutionDetails.DoesNotExist: except InstitutionDetails.DoesNotExist:
form = InstDetailsForm(request_data) form = InstDetailsForm(request_data)
if form.is_valid(): urls_form = UrlFormSet(request_data, prefix='urlsform')
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFactInst, can_delete=True)
if form.is_valid() and urls_form.is_valid():
instdets = form.save() instdets = form.save()
urls_form.instance = instdets
urls_inst = urls_form.save()
return HttpResponseRedirect(reverse("institutions")) return HttpResponseRedirect(reverse("institutions"))
else: else:
try: try:
...@@ -109,9 +117,9 @@ def add_institution_details(request, institution_pk): ...@@ -109,9 +117,9 @@ def add_institution_details(request, institution_pk):
inst = profile.institution inst = profile.institution
except UserProfile.DoesNotExist: except UserProfile.DoesNotExist:
inst = False inst = False
form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=institution_pk), empty_label=None) form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=institution_pk), empty_label=None)
form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst))) form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
return render_to_response('edumanage/institution_edit.html', { 'institution': inst, 'form': form}, return render_to_response('edumanage/institution_edit.html', { 'institution': inst, 'form': form, 'urls_form': urls_form},
context_instance=RequestContext(request, base_response(request))) context_instance=RequestContext(request, base_response(request)))
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
<li class="active">Add-Edit</li> <li class="active">Add-Edit</li>
{% endblock %} {% endblock %}
{% block extrahead %} {% block extrahead %}
<script type="text/javascript" src="/static/js/jquery.formset.js"></script>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
$("#adduserSubmit").click(function(){ $("#adduserSubmit").click(function(){
...@@ -45,7 +46,23 @@ $(document).ready(function() { ...@@ -45,7 +46,23 @@ $(document).ready(function() {
return false; return false;
}); });
$('#urlsform tbody tr').formset({
prefix: '{{urls_form.prefix}}',
formCssClass: "dynamic-formset1",
added: addButton,
});
$(".delete-row").prepend('<i class="icon-remove-sign icon-white"></i> ').addClass('btn btn-small btn-warning');
$(".add-row").prepend('<i class="icon-plus-sign icon-white"></i> ').addClass('btn btn-small btn-info');
}); });
function addButton(row){
$(row).find(".delete-row").prepend('<i class="icon-remove-sign icon-white"></i> ').addClass('btn btn-small btn-warning');
}
</script> </script>
{% endblock %} {% endblock %}
...@@ -103,14 +120,27 @@ $(document).ready(function() { ...@@ -103,14 +120,27 @@ $(document).ready(function() {
{% endif %} <span class="help-block"> {{ form.contact.help_text }}</span> {% endif %} <span class="help-block"> {{ form.contact.help_text }}</span>
</div> </div>
</div> </div>
<div class="control-group {% if form.url.errors %} error {% endif %}"> <div class="control-group {% for err in urls_form.errors %}{% if err|length > 0 %}error{% endif %}{% endfor %}{% if urls_form.non_form_errors %}error{% endif %}">
<label class="control-label" for="id_url">URL(s)</label> <label class="control-label" for="id_urls">Urls</label>
<div class="controls"> {{urls_form.management_form}}
{{ form.url }} <div class="controls">
{% if form.url.errors %} <span class="help-inline"> {{ form.url.errors|join:", " }} </span> {% for err in urls_form.errors %}{% if err|length > 0 %}<span class="help-inline">{{err}}</span>{% endif %}{% endfor %}
{% endif %} <span class="help-block"> {{ form.url.help_text }}</span> {% if urls_form.non_form_errors %} <span class="help-inline"> {{ urls_form.non_form_errors|join:", "}}</span>{% endif %}
</div> <table id="urlsform"><thead><tr><td>url</td><td>type</td><td>language</td></tr></thead><tbody>
</div> {% for formset in urls_form.forms %}
{{ formset.id }}
<tr id="{{ formset.prefix }}-row">
<td> {% if formset.instance.pk %}{{ formset.DELETE }}{% endif %}{{ formset.url }}{% if formset.url.errors %}<br><div class="help-inline"> {{ formset.url.errors|join:", " }} </div>{% endif %}</td>
<td>{{formset.urltype}}{% if formset.urltype.errors %}<br><p class="help-inline"> {{ formset.urltype.errors|join:", " }} </p>{% endif %}</td>
<td>{{formset.lang}}{% if formset.lang.errors %}<br><p class="help-inline"> {{ formset.lang.errors|join:", " }} </p>{% endif %}</td>
</tr>
{% endfor %}
</tbody></table>
</div>
</div>
<div class="control-group {% if form.oper_name.errors %} error {% endif %}"> <div class="control-group {% if form.oper_name.errors %} error {% endif %}">
<label class="control-label" for="id_oper_name">Oper Name</label> <label class="control-label" for="id_oper_name">Oper Name</label>
<div class="controls"> <div class="controls">
......
...@@ -7,8 +7,7 @@ ...@@ -7,8 +7,7 @@
{% endblock %} {% endblock %}
{% block extrahead %} {% block extrahead %}
<script type="text/javascript" src="/static/js/jquery.formset.js"></script> <script type="text/javascript" src="/static/js/jquery.formset.js"></script>
<script type="text/javascript" <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script src="/static/js/jquery_csrf_protect.js" type="text/javascript"></script> <script src="/static/js/jquery_csrf_protect.js" type="text/javascript"></script>
<script type="text/javascript"> <script type="text/javascript">
...@@ -233,10 +232,10 @@ ...@@ -233,10 +232,10 @@
// Initialize jquery formset // Initialize jquery formset
$('#urlsform tbody tr').formset({ $('#urlsform tbody tr').formset({
prefix: '{{urls_form.prefix}}', prefix: '{{urls_form.prefix}}',
formCssClass: "dynamic-formset1", formCssClass: "dynamic-formset1",
added: addButton, added: addButton,
}); });
$('#locsform tbody tr').formset({ $('#locsform tbody tr').formset({
prefix: '{{services_form.prefix}}', prefix: '{{services_form.prefix}}',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment