Skip to content

Commit

Permalink
Merge pull request #1845 from akvo/#1746-add-org-checks
Browse files Browse the repository at this point in the history
[#1746] Improvement of the 'Add organisation' functionality
  • Loading branch information
KasperBrandt committed Oct 6, 2015
2 parents 2fbafdc + 745761b commit 05f3698
Show file tree
Hide file tree
Showing 9 changed files with 481 additions and 100 deletions.
3 changes: 3 additions & 0 deletions akvo/rest/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@
url(r'^project/(?P<pk>[0-9]+)/step_10/$',
views.project_editor_step10,
name='project_editor_step10'),
url(r'^organisation/(?P<pk>[0-9]+)/add_logo/$',
views.project_editor_organisation_logo,
name='project_editor_add_org_logo'),
)

# Typeahead
Expand Down
4 changes: 3 additions & 1 deletion akvo/rest/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
project_editor_step7,
project_editor_step8,
project_editor_step9,
project_editor_step10)
project_editor_step10,
project_editor_organisation_logo)
from .project_comment import ProjectCommentViewSet
from .project_document import ProjectDocumentViewSet
from .project_condition import ProjectConditionViewSet
Expand Down Expand Up @@ -125,6 +126,7 @@
'project_editor_step8',
'project_editor_step9',
'project_editor_step10',
'project_editor_organisation_logo',
'PublishingStatusViewSet',
'RecipientCountryViewSet',
'RecipientRegionViewSet',
Expand Down
30 changes: 30 additions & 0 deletions akvo/rest/views/project_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@
## Custom fields ##

CUSTOM_FIELD = ('value', 'custom-field-', 'text')
ORGANISATION_LOGO_FIELD = ('logo', 'logo', 'none')


def add_error(errors, message, field_name):
Expand Down Expand Up @@ -1612,3 +1613,32 @@ def project_editor_step10(request, pk=None):
'rel_objects': rel_objects,
}
)


@api_view(['POST'])
@permission_classes((IsAuthenticated, ))
def project_editor_organisation_logo(request, pk=None):
org = Organisation.objects.get(pk=pk)
user = request.user

if not user.has_perm('rsr.change_organisation', org):
return HttpResponseForbidden()

files = request.FILES
logo = None
errors = []

if 'logo' in files.keys():
errors, changes = process_field(org, files, ORGANISATION_LOGO_FIELD, errors, [])

if not errors:
logo = get_thumbnail(
org.logo, '250x250', format="PNG", upscale=True
).url

return Response(
{
'errors': errors,
'logo': logo,
}
)
37 changes: 35 additions & 2 deletions akvo/rsr/models/organisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.


from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import Sum, Q
from django.db.models.query import QuerySet as DjangoQuerySet
Expand Down Expand Up @@ -173,7 +173,40 @@ def org_type_from_iati_type(cls, iati_type):

@models.permalink
def get_absolute_url(self):
return ('organisation-main', (), {'organisation_id': self.pk})
return 'organisation-main', (), {'organisation_id': self.pk}

def clean(self):
"""Organisations can only be saved when we're sure that they do not exist already."""
validation_errors = {}

name = self.name.strip()
other_names = Organisation.objects.filter(name__iexact=name)
if name:
if other_names.exists():
validation_errors['name'] = _('Organisation name already exists: %s.' %
other_names[0].name)
else:
validation_errors['name'] = _('Organisation name can not be blank')

long_name = self.long_name.strip()
other_long_names = Organisation.objects.filter(long_name__iexact=long_name)
if long_name:
if other_long_names.exists():
validation_errors['long_name'] = _('Organisation long name already exists: %s.' %
other_long_names[0].long_name)
else:
validation_errors['long_name'] = _('Organisation long name can not be blank')

if self.iati_org_id:
iati_org_id = self.iati_org_id.strip()
other_iati_ids = Organisation.objects.filter(iati_org_id__iexact=iati_org_id)
if iati_org_id and other_iati_ids.exists():
validation_errors['iati_org_id'] = _('IATI organisation identifier already exists '
'for this organisation: %s.' %
other_iati_ids[0].name)

if validation_errors:
raise ValidationError(validation_errors)

class QuerySet(DjangoQuerySet):
def has_location(self):
Expand Down
Loading

0 comments on commit 05f3698

Please sign in to comment.