|
@@ -5,7 +5,8 @@ from django.shortcuts import render, get_object_or_404
|
|
|
from django.contrib import admin
|
|
|
from django.contrib import messages
|
|
|
from django.contrib.auth.admin import UserAdmin
|
|
|
-from django.contrib.auth.models import Group
|
|
|
+from django.contrib.auth.models import Group, Permission
|
|
|
+from django.contrib.contenttypes.models import ContentType
|
|
|
from django.http import HttpResponseRedirect
|
|
|
from django.conf.urls import url
|
|
|
from django.db.models.query import QuerySet
|
|
@@ -13,7 +14,7 @@ from django.core.urlresolvers import reverse
|
|
|
from django.utils.html import format_html
|
|
|
|
|
|
from coin.members.models import (
|
|
|
- Member, CryptoKey, LdapUser, MembershipFee, OfferSubscription)
|
|
|
+ Member, CryptoKey, LdapUser, MembershipFee, Offer, OfferSubscription, RowLevelPermission)
|
|
|
from coin.members.membershipfee_filter import MembershipFeeFilter
|
|
|
from coin.members.forms import AdminMemberChangeForm, MemberCreationForm
|
|
|
from coin.utils import delete_selected
|
|
@@ -35,17 +36,50 @@ class MembershipFeeInline(admin.TabularInline):
|
|
|
class OfferSubscriptionInline(admin.TabularInline):
|
|
|
model = OfferSubscription
|
|
|
extra = 0
|
|
|
- exclude = ('comments',)
|
|
|
- readonly_fields = ('get_subscription_reference', 'subscription_date', 'resign_date',
|
|
|
- 'commitment', 'offer')
|
|
|
|
|
|
- show_change_link = True
|
|
|
+ writable_fields = ('subscription_date', 'resign_date', 'commitment', 'offer')
|
|
|
+ all_fields = ('get_subscription_reference',) + writable_fields
|
|
|
+
|
|
|
+ def get_fields(self, request, obj=None):
|
|
|
+ if obj:
|
|
|
+ return self.all_fields
|
|
|
+ else:
|
|
|
+ return self.writable_fields
|
|
|
+
|
|
|
+ def get_readonly_fields(self, request, obj=None):
|
|
|
+ # création ou superuser : lecture écriture
|
|
|
+ if not obj or request.user.is_superuser:
|
|
|
+ return ('get_subscription_reference',)
|
|
|
+ # modification : lecture seule seulement
|
|
|
+ else:
|
|
|
+ return self.all_fields
|
|
|
|
|
|
- def has_add_permission(self, request, obj=None):
|
|
|
- return False
|
|
|
+ show_change_link = True
|
|
|
|
|
|
+ def formfield_for_foreignkey(self, db_field, request, **kwargs):
|
|
|
+ if request.user.is_superuser:
|
|
|
+ return super(OfferSubscriptionInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
|
|
|
+ else:
|
|
|
+ if db_field.name == "offer":
|
|
|
+ kwargs["queryset"] = Offer.objects.manageable_by(request.user)
|
|
|
+ return super(OfferSubscriptionInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
|
|
|
+
|
|
|
+ def has_add_permission(self, request):
|
|
|
+ # - Quand on *crée* un membre on autorise à ajouter un abonnement
|
|
|
+ # - Quand on *édite* un membre, on interdit l'ajout d'abonnements (sauf
|
|
|
+ # par le bureau) car cela permettrait de gagner à loisir accès à
|
|
|
+ # toute fiche adhérent en lui ajoutant un abonnement à une offre dont
|
|
|
+ # on a la gestion).
|
|
|
+ return (
|
|
|
+ request.resolver_match.view_name == 'admin:members_member_add'
|
|
|
+ or
|
|
|
+ request.user.is_superuser
|
|
|
+ )
|
|
|
+
|
|
|
+ # sinon on pourrait supprimer les abo qu'on ne peut pas gérer
|
|
|
+ # pourrait peut-être être plus fin, obj réfère ici au member de la page
|
|
|
def has_delete_permission(self, request, obj=None):
|
|
|
- return False
|
|
|
+ return request.user.is_superuser
|
|
|
|
|
|
|
|
|
class MemberAdmin(UserAdmin):
|
|
@@ -62,45 +96,46 @@ class MemberAdmin(UserAdmin):
|
|
|
form = AdminMemberChangeForm
|
|
|
add_form = MemberCreationForm
|
|
|
|
|
|
- fieldsets = (
|
|
|
- ('Adhérent', {'fields': (
|
|
|
- ('status', 'resign_date'),
|
|
|
- 'type',
|
|
|
- ('first_name', 'last_name', 'nickname'),
|
|
|
- 'organization_name',
|
|
|
- 'comments',
|
|
|
- 'balance')}),
|
|
|
- ('Coordonnées', {'fields': (
|
|
|
+ def get_fieldsets(self, request, obj=None):
|
|
|
+ coord_fieldset = ('Coordonnées', {'fields': (
|
|
|
('email', 'send_membership_fees_email'),
|
|
|
('home_phone_number', 'mobile_phone_number'),
|
|
|
'address',
|
|
|
- ('postal_code', 'city', 'country'))}),
|
|
|
- ('Authentification', {'fields': (
|
|
|
- ('username', 'password'))}),
|
|
|
- ('Permissions', {'fields': (
|
|
|
- ('is_active', 'is_staff', 'is_superuser'))}),
|
|
|
- (None, {'fields': ('date_last_call_for_membership_fees_email',)})
|
|
|
- )
|
|
|
-
|
|
|
- add_fieldsets = (
|
|
|
- ('Adhérent', {'fields': (
|
|
|
- 'status',
|
|
|
- 'type',
|
|
|
- ('first_name', 'last_name', 'nickname'),
|
|
|
- 'organization_name',
|
|
|
- 'comments',
|
|
|
- 'balance' # XXX we shouldn't need this, the default value should be used
|
|
|
- )}),
|
|
|
- ('Coordonnées', {'fields': (
|
|
|
- 'email',
|
|
|
- ('home_phone_number', 'mobile_phone_number'),
|
|
|
- 'address',
|
|
|
- ('postal_code', 'city', 'country'))}),
|
|
|
- ('Authentification', {'fields': (
|
|
|
- ('username', 'password'),)}),
|
|
|
- ('Permissions', {'fields': (
|
|
|
- ('is_active', 'is_staff', 'is_superuser', 'date_joined'))})
|
|
|
- )
|
|
|
+ ('postal_code', 'city', 'country'))})
|
|
|
+ auth_fieldset = ('Authentification', {'fields': (
|
|
|
+ ('username', 'password'))})
|
|
|
+ perm_fieldset = ('Permissions', {'fields': (
|
|
|
+ ('is_active', 'is_staff', 'is_superuser', 'groups'))})
|
|
|
+
|
|
|
+ # if obj is null then it is a creation, otherwise it is a modification
|
|
|
+ if obj:
|
|
|
+ return (
|
|
|
+ ('Adhérent', {'fields': (
|
|
|
+ ('status', 'date_joined', 'resign_date'),
|
|
|
+ 'type',
|
|
|
+ ('first_name', 'last_name', 'nickname'),
|
|
|
+ 'organization_name',
|
|
|
+ 'comments',
|
|
|
+ 'balance' # XXX we shouldn't need this, the default value should be used
|
|
|
+ )}),
|
|
|
+ coord_fieldset,
|
|
|
+ auth_fieldset,
|
|
|
+ perm_fieldset,
|
|
|
+ (None, {'fields': ('date_last_call_for_membership_fees_email',)})
|
|
|
+ )
|
|
|
+ else:
|
|
|
+ return (
|
|
|
+ ('Adhérent', {'fields': (
|
|
|
+ ('status', 'date_joined'),
|
|
|
+ 'type',
|
|
|
+ ('first_name', 'last_name', 'nickname'),
|
|
|
+ 'organization_name',
|
|
|
+ 'comments',
|
|
|
+ 'balance')}),
|
|
|
+ coord_fieldset,
|
|
|
+ auth_fieldset,
|
|
|
+ perm_fieldset
|
|
|
+ )
|
|
|
|
|
|
radio_fields = {"type": admin.HORIZONTAL}
|
|
|
|
|
@@ -108,16 +143,27 @@ class MemberAdmin(UserAdmin):
|
|
|
|
|
|
inlines = [CryptoKeyInline, MembershipFeeInline, OfferSubscriptionInline]
|
|
|
|
|
|
+ def get_queryset(self, request):
|
|
|
+ qs = super(MemberAdmin, self).get_queryset(request)
|
|
|
+ if request.user.is_superuser:
|
|
|
+ return qs
|
|
|
+ else:
|
|
|
+ offers = Offer.objects.manageable_by(request.user)
|
|
|
+ return qs.filter(offersubscription__offer__in=offers).distinct()
|
|
|
+
|
|
|
def get_readonly_fields(self, request, obj=None):
|
|
|
+ readonly_fields = []
|
|
|
if obj:
|
|
|
# Remove help_text for readonly field (can't do that in the Form
|
|
|
# django seems to user help_text from model for readonly fields)
|
|
|
username_field = [
|
|
|
f for f in obj._meta.fields if f.name == 'username']
|
|
|
username_field[0].help_text = ''
|
|
|
- return ['username', ]
|
|
|
- else:
|
|
|
- return []
|
|
|
+
|
|
|
+ readonly_fields.append('username')
|
|
|
+ if not request.user.is_superuser:
|
|
|
+ readonly_fields += ['is_active', 'is_staff', 'is_superuser', 'groups', 'date_last_call_for_membership_fees_email']
|
|
|
+ return readonly_fields
|
|
|
|
|
|
def set_as_member(self, request, queryset):
|
|
|
rows_updated = queryset.update(status='member')
|
|
@@ -215,7 +261,14 @@ class MembershipFeeAdmin(admin.ModelAdmin):
|
|
|
'payment_date')
|
|
|
form = autocomplete_light.modelform_factory(MembershipFee, fields='__all__')
|
|
|
|
|
|
+class RowLevelPermissionAdmin(admin.ModelAdmin):
|
|
|
+ def get_changeform_initial_data(self, request):
|
|
|
+ return {'content_type': ContentType.objects.get_for_model(OfferSubscription)}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
admin.site.register(Member, MemberAdmin)
|
|
|
admin.site.register(MembershipFee, MembershipFeeAdmin)
|
|
|
-admin.site.unregister(Group)
|
|
|
+# admin.site.unregister(Group)
|
|
|
# admin.site.register(LdapUser, LdapUserAdmin)
|
|
|
+admin.site.register(RowLevelPermission, RowLevelPermissionAdmin)
|