mirror of
https://github.com/ae-utbm/sith.git
synced 2024-12-23 00:01:16 +00:00
Update CanCreateView and fix accounting views in consequence
This commit is contained in:
parent
1396f2ca84
commit
e9544f2581
@ -88,34 +88,12 @@ class GeneralJournal(models.Model):
|
||||
amount = CurrencyField(_('amount'), default=0)
|
||||
effective_amount = CurrencyField(_('effective_amount'), default=0)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(GeneralJournal, self).__init__(*args, **kwargs)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.id == None:
|
||||
amount = 0
|
||||
super(GeneralJournal, self).save(*args, **kwargs)
|
||||
|
||||
def can_be_created_by(user):
|
||||
"""
|
||||
Method to see if an object can be created by the given user
|
||||
"""
|
||||
if user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']): # TODO: add the treasurer of the club
|
||||
return True
|
||||
return False
|
||||
|
||||
def is_owned_by(self, user):
|
||||
"""
|
||||
Method to see if that object can be edited by the given user
|
||||
"""
|
||||
if user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']):
|
||||
return True
|
||||
return False
|
||||
|
||||
def can_be_edited_by(self, user):
|
||||
"""
|
||||
Method to see if that object can be edited by the given user
|
||||
"""
|
||||
if self.club_account.can_be_edited_by(user):
|
||||
return True
|
||||
return False
|
||||
@ -165,7 +143,7 @@ class Operation(models.Model):
|
||||
"""
|
||||
if user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']):
|
||||
return True
|
||||
m = self.journal.club_account.get_membership_for(user)
|
||||
m = self.journal.club_account.club.get_membership_for(user)
|
||||
if m is not None and m.role >= 7:
|
||||
return True
|
||||
return False
|
||||
@ -182,8 +160,8 @@ class Operation(models.Model):
|
||||
return reverse('accounting:journal_details', kwargs={'j_id': self.journal.id})
|
||||
|
||||
def __str__(self):
|
||||
return "%d | %d € | %s | %s | %s" % (
|
||||
self.id, self.amount, self.date, self.accounting_type, self.done,
|
||||
return "%d € | %s | %s | %s" % (
|
||||
self.amount, self.date, self.accounting_type, self.done,
|
||||
)
|
||||
|
||||
class AccountingType(models.Model):
|
||||
|
@ -26,7 +26,7 @@ class AccountingTypeEditView(CanViewMixin, UpdateView):
|
||||
fields = ['code', 'label', 'movement_type']
|
||||
template_name = 'core/edit.jinja'
|
||||
|
||||
class AccountingTypeCreateView(CanEditPropMixin, CreateView): # TODO: move to CanCreateMixin
|
||||
class AccountingTypeCreateView(CanCreateMixin, CreateView):
|
||||
"""
|
||||
Create an accounting type (for the admins)
|
||||
"""
|
||||
@ -60,7 +60,7 @@ class BankAccountDetailView(CanViewMixin, DetailView):
|
||||
pk_url_kwarg = "b_account_id"
|
||||
template_name = 'accounting/bank_account_details.jinja'
|
||||
|
||||
class BankAccountCreateView(CanEditPropMixin, CreateView): # TODO: move to CanCreateMixin
|
||||
class BankAccountCreateView(CanCreateMixin, CreateView):
|
||||
"""
|
||||
Create a bank account (for the admins)
|
||||
"""
|
||||
@ -96,7 +96,7 @@ class ClubAccountDetailView(CanViewMixin, DetailView):
|
||||
pk_url_kwarg = "c_account_id"
|
||||
template_name = 'accounting/club_account_details.jinja'
|
||||
|
||||
class ClubAccountCreateView(CanEditPropMixin, CreateView): # TODO: move to CanCreateMixin
|
||||
class ClubAccountCreateView(CanCreateMixin, CreateView):
|
||||
"""
|
||||
Create a club account (for the admins)
|
||||
"""
|
||||
@ -158,7 +158,7 @@ class JournalEditView(CanEditMixin, UpdateView):
|
||||
|
||||
# Operation views
|
||||
|
||||
class OperationCreateView(CanEditMixin, CreateView): # TODO: move to CanCreateMixin
|
||||
class OperationCreateView(CanCreateMixin, CreateView):
|
||||
"""
|
||||
Create an operation
|
||||
"""
|
||||
|
@ -12,11 +12,6 @@ def forbidden(request):
|
||||
def not_found(request):
|
||||
return HttpResponseNotFound(render(request, "core/404.jinja"))
|
||||
|
||||
def can_create(mod, user):
|
||||
if mod.can_be_created_by(user):
|
||||
return True
|
||||
return False
|
||||
|
||||
def can_edit_prop(obj, user):
|
||||
if obj is None or user.is_owner(obj):
|
||||
return True
|
||||
@ -37,12 +32,10 @@ class CanCreateMixin(View):
|
||||
This view is made to protect any child view that would create an object, and thus, that can not be protected by any
|
||||
of the following mixin
|
||||
"""
|
||||
def dispatch(self, request, *arg, **kwargs):
|
||||
res = super(CanCreateMixin, self).dispatch(request, *arg, **kwargs)
|
||||
if hasattr(self, 'model'):
|
||||
mod = self.model
|
||||
if can_create(mod, self.request.user):
|
||||
return res
|
||||
def form_valid(self, form):
|
||||
obj = form.instance
|
||||
if can_edit_prop(obj, self.request.user):
|
||||
return super(CanCreateMixin, self).form_valid(form)
|
||||
raise PermissionDenied
|
||||
|
||||
class CanEditPropMixin(View):
|
||||
|
Loading…
Reference in New Issue
Block a user