Skip to content

Commit

Permalink
[#1025] Added document upload to ProjectDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
KasperBrandt committed Feb 11, 2015
1 parent e06da91 commit 64ab816
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 20 deletions.
22 changes: 19 additions & 3 deletions akvo/rsr/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,21 @@ def get_extra(self, request, obj=None, **kwargs):
return 1


class ProjectDocumentInline(NestedStackedInline):
model = get_model('rsr', 'ProjectDocument')
fieldsets = (
(None, {
'fields': ('id', 'url', 'document', 'title', 'format', 'category', 'language')
}),
)

def get_extra(self, request, obj=None, **kwargs):
if obj:
return 1 if obj.documents.count() == 0 else 0
else:
return 1


class CountryBudgetInline(NestedStackedInline):
model = get_model('rsr', 'CountryBudgetItem')
extra = 0
Expand Down Expand Up @@ -544,8 +559,8 @@ class LegacyDataInline(NestedStackedInline):
class ProjectAdmin(TimestampsAdminDisplayMixin, ObjectPermissionsModelAdmin, NestedModelAdmin):
model = get_model('rsr', 'project')
inlines = (
ProjectContactInline, PartnershipInline, ProjectLocationInline, SectorInline, BudgetItemAdminInLine,
TransactionInline, ResultInline, LinkInline, ProjectConditionInline, CountryBudgetInline,
ProjectContactInline, PartnershipInline, ProjectDocumentInline, ProjectLocationInline, SectorInline,
BudgetItemAdminInLine, TransactionInline, ResultInline, LinkInline, ProjectConditionInline, CountryBudgetInline,
PlannedDisbursementInline, PolicyMarkerInline, RecipientCountryInline, RecipientRegionInline, LegacyDataInline,
BenchmarkInline, GoalInline,
)
Expand Down Expand Up @@ -607,7 +622,8 @@ class ProjectAdmin(TimestampsAdminDisplayMixin, ObjectPermissionsModelAdmin, Nes
'description': u'<p style="margin-left:0; padding-left:0; margin-top:1em; width:75%%;">%s</p>' % _(
u'You can add any additional supporting documents to your project here. This could be in the form of '
u'annual reports, baseline surveys, contextual information or any other report or summary that can '
u'help users understand more about the projects activities.'
u'help users understand more about the projects activities.<br><br>'
u'<strong>Important note:</strong> It is required to upload a document or to indicate an URL.'
),
'fields': (),
}),
Expand Down
55 changes: 47 additions & 8 deletions akvo/rsr/models/project_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.


from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import ugettext_lazy as _

Expand All @@ -13,22 +14,60 @@


class ProjectDocument(models.Model):
def document_path(self, filename):
return 'db/project/%s/document/%s' % (str(self.project.pk), filename)

project = models.ForeignKey('Project', related_name='documents', verbose_name=_(u'project'))
url = models.URLField(_(u'url'))
format = ValidXMLCharField(_(u'format'), max_length=75, blank=True)
title = ValidXMLCharField(_(u'title'), max_length=100, blank=True)
title_language = ValidXMLCharField(_(u'title language'), max_length=2, blank=True, choices=codelists.LANGUAGE)
url = models.URLField(
_(u'url'), blank=True,
help_text=_(u'You can indicate an URL of a document of the project. These documents will allow users to '
u'download and view to gain further insight in the project activities.')
)
document = models.FileField(
_(u'document'), blank=True, upload_to=document_path,
help_text=_(u'You can upload additional documents to your project. These documents will be stored on the RSR '
u'server and publicly available for users to download and view to gain further insight in the '
u'project activities.')
)
format = ValidXMLCharField(
_(u'format'), max_length=75, blank=True,
help_text=_(u'Indicate the IATI format code of the document. <a '
u'href="http://iatistandard.org/codelists/FileFormat/" target="_blank">Full list of IATI '
u'format codes</a>')
)
title = ValidXMLCharField(
_(u'title'), max_length=100, blank=True, help_text=_(u'Indicate the document title. (100 characters)')
)
title_language = ValidXMLCharField(
_(u'title language'), max_length=2, blank=True, choices=codelists.LANGUAGE,
help_text=_(u'Select the language of the document title.')
)
category = ValidXMLCharField(
_(u'title language'),
max_length=3, blank=True, choices=[codelist[:2] for codelist in codelists.DOCUMENT_CATEGORY]
_(u'title language'), max_length=3, blank=True,
choices=[codelist[:2] for codelist in codelists.DOCUMENT_CATEGORY],
help_text=_(u'Select a document category.')
)
language = ValidXMLCharField(
_(u'language'), max_length=2, blank=True, choices=codelists.LANGUAGE,
help_text=_(u'Select the language that the document is written in.')
)
language = ValidXMLCharField(_(u'language'), max_length=2, blank=True, choices=codelists.LANGUAGE)

def __unicode__(self):
return self.title

def clean(self):
"""
Check if the user has at least uploaded a document or indicated an URL.
"""
if not (self.url or self.document):
raise ValidationError(u'It is required to upload a document or indicate an URL.')

def show_link(self):
return u'<a href="%s">%s</a>' % (self.url, self.title,)
title = self.title if self.title else u'Untitled document'
if self.url:
return u'<a href="%s">%s</a>' % (self.url, title,)
else:
return u'<a href="%s">%s</a>' % (self.document.url, title,)

def iati_category(self):
return dict([codelist[:2] for codelist in codelists.DOCUMENT_CATEGORY])[self.category] if self.category else ""
Expand Down
23 changes: 14 additions & 9 deletions akvo/templates/admin/rsr/project/change_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,40 +80,42 @@ <h2>{% trans 'Adding and Editing Projects.' %}</h2>
{% include inline_admin_formset.opts.template %}
{% endwith %}
{% endif %}
{% if forloop.counter == 8 %}
{% if forloop.counter == 7 %}
{% with inline_admin_formset=inline_admin_formsets.2 %}
{% include inline_admin_formset.opts.template %}
{% endwith %}
{% endif %}
{% if forloop.counter == 9 %}
{% if forloop.counter == 8 %}
{% with inline_admin_formset=inline_admin_formsets.3 %}
{% include inline_admin_formset.opts.template %}
{% endwith %}
{% endif %}
{% if forloop.counter == 10 %}
{% if forloop.counter == 9 %}
{% with inline_admin_formset=inline_admin_formsets.4 %}
{% include inline_admin_formset.opts.template %}
{% endwith %}
{% endif %}
{% if forloop.counter == 11 %}
{% if forloop.counter == 10 %}
{% with inline_admin_formset=inline_admin_formsets.5 %}
{% include inline_admin_formset.opts.template %}
{% endwith %}
{% endif %}
{% if forloop.counter == 12 %}
{% if forloop.counter == 11 %}
{% with inline_admin_formset=inline_admin_formsets.6 %}
{% include inline_admin_formset.opts.template %}
{% endwith %}
{% endif %}
{% if forloop.counter == 13 %}
{% if forloop.counter == 12 %}
{% with inline_admin_formset=inline_admin_formsets.7 %}
{% include inline_admin_formset.opts.template %}
{% endwith %}
{% endif %}
{% if forloop.counter == 15 %}
{% if forloop.counter == 13 %}
{% with inline_admin_formset=inline_admin_formsets.8 %}
{% include inline_admin_formset.opts.template %}
{% endwith %}
{% endif %}
{% if forloop.counter == 15 %}
{% with inline_admin_formset=inline_admin_formsets.9 %}
{% include inline_admin_formset.opts.template %}
{% endwith %}
Expand All @@ -132,14 +134,17 @@ <h2>{% trans 'Adding and Editing Projects.' %}</h2>
{% with inline_admin_formset=inline_admin_formsets.14 %}
{% include inline_admin_formset.opts.template %}
{% endwith %}
{% endif %}
{% if forloop.counter == 16 %}
{% with inline_admin_formset=inline_admin_formsets.15 %}
{% include inline_admin_formset.opts.template %}
{% endwith %}
{% endif %}
{% if forloop.counter == 16 %}
{% with inline_admin_formset=inline_admin_formsets.16 %}
{% include inline_admin_formset.opts.template %}
{% endwith %}
{% with inline_admin_formset=inline_admin_formsets.17 %}
{% include inline_admin_formset.opts.template %}

This comment has been minimized.

Copy link
@kardan

kardan Feb 14, 2015

Contributor

Getting TemplateDoesNotExist when loading the project in the admin. It's because inline_admin_formset is None.

This comment has been minimized.

Copy link
@KasperBrandt

KasperBrandt Feb 14, 2015

Author Contributor

Strange, it works for me. There's 18 inlines in the ProjectAdmin, so it shouldn't be None. This last one refers to the GoalInline.

This comment has been minimized.

Copy link
@kardan

kardan Feb 15, 2015

Contributor

I did make sure to apply all migrations and if I add a conditional statement outside the include inline_admin_formset the error disappears. As a comment I do find the "with" statement a bit confusing. I was first expecting "with" to skip the body of the statement if the binding did not succeed, but anyway..

{% endwith %}
{% endif %}
{% endfor %}

Expand Down

0 comments on commit 64ab816

Please sign in to comment.