Skip to content

Commit

Permalink
Merge branch 'feature/limited_partner_types' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
zzgvh committed Jun 13, 2013
2 parents 7fdb4b8 + 73d80f9 commit df70bf0
Show file tree
Hide file tree
Showing 9 changed files with 1,245 additions and 14 deletions.

This file was deleted.

This file was deleted.

100 changes: 90 additions & 10 deletions akvo/rsr/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.conf import settings
from django.contrib import admin
from django.contrib.admin import helpers, widgets
from django.contrib.admin.util import unquote
from django.contrib.admin.util import unquote, flatten_fieldsets
from django.contrib.auth.admin import GroupAdmin
from django.contrib.auth.models import Group
from django.contrib.contenttypes import generic
Expand Down Expand Up @@ -112,14 +112,19 @@ class InternalOrganisationIDInline(admin.TabularInline):
verbose_name = 'internal organisation ID'
verbose_name_plural = 'internal organisation IDs'


class OrganisationAdmin(admin.ModelAdmin):
# NOTE: The change_form.html template relies on the fieldsets to put the inline forms correctly.
# If the fieldsets are changed, the template may need fixing too
fieldsets = (
(_(u'General information'), {'fields': ('name', 'long_name', 'organisation_type', 'new_organisation_type', 'logo', 'url', 'iati_org_id', 'language',)}),
(_(u'General information'), {'fields': ('name', 'long_name', 'partner_types', 'organisation_type',
'new_organisation_type', 'logo', 'url', 'iati_org_id', 'language',)}),
(_(u'Contact information'), {'fields': ('phone', 'mobile', 'fax', 'contact_person', 'contact_email', ), }),
(_(u'About the organisation'), {'fields': ('description', )}),
)
inlines = (OrganisationLocationInline, InternalOrganisationIDInline,)
exclude = ('internal_org_ids',)
readonly_fields = ('partner_types',)
list_display = ('name', 'long_name', 'website', 'language',)
search_fields = ('name', 'long_name',)

Expand All @@ -140,6 +145,37 @@ def __init__(self, model, admin_site):
self.formfield_overrides = {ImageWithThumbnailsField: {'widget': widgets.AdminFileWidget}, }
super(OrganisationAdmin, self).__init__(model, admin_site)

def allowed_partner_types(self, obj):
return ', '.join([pt.label for pt in obj.partner_types.all()])

def get_list_display(self, request):
# see the notes fields in the change list if you have the right permissions
if request.user.has_perm(self.opts.app_label + '.' + self.opts.get_change_permission()):
return list(self.list_display) + ['allowed_partner_types']
return super(OrganisationAdmin, self).get_list_display(request)

def get_readonly_fields(self, request, obj):
# parter_types is read only unless you have change permission for organisations
if not request.user.has_perm(self.opts.app_label + '.' + self.opts.get_change_permission()):
self.readonly_fields = ('partner_types',)
# hack to set the help text
try:
field = [f for f in obj._meta.local_many_to_many if f.name == 'partner_types']
if len(field) > 0:
field[0].help_text = 'The allowed partner types for this organisation'
except:
pass
else:
self.readonly_fields = ()
# hack to set the help text
try:
field = [f for f in obj._meta.local_many_to_many if f.name == 'partner_types']
if len(field) > 0:
field[0].help_text = 'The allowed partner types for this organisation. Hold down "Control", or "Command" on a Mac, to select more than one.'
except:
pass
return super(OrganisationAdmin, self).get_readonly_fields(request, obj)

def queryset(self, request):
qs = super(OrganisationAdmin, self).queryset(request)
opts = self.opts
Expand Down Expand Up @@ -398,10 +434,8 @@ def clean(self):
def duplicates_in_list(seq):
"return True if the list contains duplicate items"
seq_set = list(set(seq))
# need to sort since set() doesn't preserver order
seq.sort()
seq_set.sort()
return seq != seq_set
# if the set isn't of the same length as the list there must be dupes in the list
return len(seq) != len(seq_set)

user = self.request.user
user_profile = user.get_profile()
Expand Down Expand Up @@ -439,13 +473,32 @@ def duplicates_in_list(seq):
for org, types in partner_types.items():
# are there duplicates in the list of partner_types?
if duplicates_in_list(types):
errors += [_(u'%s has duplicate partner types of the same kind.' % org)]
errors += [_(u'{org} has duplicate partner types of the same kind.'.format(org=org))]

self._non_form_errors = ErrorList(errors)


class RSR_PartnershipInlineForm(forms.ModelForm):
class Meta:
model = get_model('rsr', 'Partnership')

def clean_partner_type(self):
partner_types = get_model('rsr', 'PartnerType').objects.all()
partner_types_dict = {partner_type.id: partner_type.label for partner_type in partner_types}
allowed = [partner_type.pk for partner_type in self.cleaned_data['organisation'].partner_types.all()]
data = self.cleaned_data['partner_type']
if data not in allowed:
raise forms.ValidationError("{org} is not allowed to be a {partner_type_label}".format(
org=self.cleaned_data['organisation'],
partner_type_label=partner_types_dict[data]
))
return data


class PartnershipInline(admin.TabularInline):
model = get_model('rsr', 'Partnership')
extra = 1
form = RSR_PartnershipInlineForm
formset = RSR_PartnershipInlineFormFormSet

def get_formset(self, request, *args, **kwargs):
Expand Down Expand Up @@ -1103,14 +1156,41 @@ class PaymentGatewaySelectorAdmin(admin.ModelAdmin):

class PartnerSiteAdmin(admin.ModelAdmin):
form = PartnerSiteAdminForm

fieldsets = (
(u'General', dict(fields=('organisation', 'enabled',))),
(u'General', dict(fields=('organisation', 'enabled', 'notes',))),
(u'HTTP', dict(fields=('hostname', 'cname', 'custom_return_url',))),
(u'Style and content', dict(fields=('about_box', 'about_image', 'custom_css', 'custom_logo', 'custom_favicon',))),
(u'Languages and translation', dict(fields=('default_language', 'ui_translation', 'google_translation',)))
)
list_display = '__unicode__', 'full_domain'
# the notes field is not shown to everyone
restricted_fieldsets = (
(u'General', dict(fields=('organisation', 'enabled',))),
(u'HTTP', dict(fields=('hostname', 'cname', 'custom_return_url',))),
(u'Style and content',
dict(fields=('about_box', 'about_image', 'custom_css', 'custom_logo', 'custom_favicon',))),
(u'Languages and translation', dict(fields=('default_language', 'ui_translation', 'google_translation',)))
)
list_display = '__unicode__', 'full_domain', 'enabled',

def get_fieldsets(self, request, obj=None):
# don't show the notes field unless you have "add" permission on the PartnerSite model
# (currently means an Akvo staff user (or superuser))
if request.user.has_perm(self.opts.app_label + '.' + self.opts.get_add_permission()):
return super(PartnerSiteAdmin, self).get_fieldsets(request, obj)
return self.restricted_fieldsets

def get_form(self, request, obj=None, **kwargs):
""" Workaround bug http://code.djangoproject.com/ticket/9360
"""
return super(PartnerSiteAdmin, self).get_form(
request, obj, fields=flatten_fieldsets(self.get_fieldsets(request, obj))
)

def get_list_display(self, request):
# see the notes fields in the change list if you have the right permissions
if request.user.has_perm(self.opts.app_label + '.' + self.opts.get_add_permission()):
return list(self.list_display) + ['notes']
return super(PartnerSiteAdmin, self).get_list_display(request)

def get_actions(self, request):
""" Remove delete admin action for "non certified" users"""
Expand Down
11 changes: 11 additions & 0 deletions akvo/rsr/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,18 @@ def create_limited_change_permissions(sender, **kwargs):
else:
print 'RSR limited change %s Permission already exists in the database' % model_name

def create_partner_types(sender, **kwargs):
print "Adding PartnerType objects"
print
for id, label in rsr.Partnership.PARTNER_TYPES:
partner_type, created = rsr.PartnerType.objects.get_or_create(id=id, label=label)
if created:
print 'Created PartnerType {partner_type}'.format(partner_type=partner_type)
else:
print 'Found existing PartnerType {partner_type}'.format(partner_type=partner_type)

post_syncdb.connect(create_limited_change_permissions, sender=rsr)
post_syncdb.connect(create_partner_types, sender=rsr)



Expand Down
Loading

0 comments on commit df70bf0

Please sign in to comment.