def expenses(request, expense_id=None): """Display user expenses and expenses that he can validate""" if not request.user.groups.filter(name="expense_requester").exists(): return HttpResponseRedirect(urlresolvers.reverse("forbiden")) try: consultant = Consultant.objects.get(trigramme__iexact=request.user.username) user_team = consultant.userTeam(excludeSelf=False) except Consultant.DoesNotExist: user_team = [] try: if expense_id: expense = Expense.objects.get(id=expense_id) if not (perm.has_permission(expense, request.user, "expense_edit") and (expense.user == request.user or expense.user in user_team)): messages.add_message(request, messages.WARNING, _("You are not allowed to edit that expense")) expense_id = None expense = None except Expense.DoesNotExist: messages.add_message(request, messages.ERROR, _("Expense %s does not exist" % expense_id)) expense_id = None if request.method == "POST": if expense_id: form = ExpenseForm(request.POST, request.FILES, instance=expense) else: form = ExpenseForm(request.POST, request.FILES) if form.is_valid(): expense = form.save(commit=False) if not hasattr(expense, "user"): # Don't update user if defined (case of expense updated by manager or adminstrator) expense.user = request.user expense.creation_date = date.today() expense.save() wf.set_initial_state(expense) return HttpResponseRedirect(urlresolvers.reverse("expense.views.expenses")) else: if expense_id: form = ExpenseForm(instance=expense) # A form that edit current expense else: form = ExpenseForm(initial={"expense_date": date.today()}) # An unbound form # Get user expenses user_expenses = Expense.objects.filter(user=request.user, workflow_in_progress=True).select_related() if user_team: team_expenses = Expense.objects.filter(user__in=user_team, workflow_in_progress=True).select_related() else: team_expenses = [] # Paymaster manage all expenses if perm.has_role(request.user, "expense paymaster"): managed_expenses = Expense.objects.filter(workflow_in_progress=True).exclude(user=request.user).select_related() else: managed_expenses = team_expenses userExpenseTable = UserExpenseWorkflowTable(user_expenses) userExpenseTable.transitionsData = dict([(e.id, []) for e in user_expenses]) # Inject expense allowed transitions. Always empty for own expense userExpenseTable.expenseEditPerm = dict([(e.id, perm.has_permission(e, request.user, "expense_edit")) for e in user_expenses]) # Inject expense edit permissions RequestConfig(request, paginate={"per_page": 50}).configure(userExpenseTable) managedExpenseTable = ManagedExpenseWorkflowTable(managed_expenses) managedExpenseTable.transitionsData = dict([(e.id, e.transitions(request.user)) for e in managed_expenses]) # Inject expense allowed transitions managedExpenseTable.expenseEditPerm = dict([(e.id, perm.has_permission(e, request.user, "expense_edit")) for e in managed_expenses]) # Inject expense edit permissions RequestConfig(request, paginate={"per_page": 50}).configure(managedExpenseTable) return render(request, "expense/expenses.html", {"user_expense_table": userExpenseTable, "managed_expense_table": managedExpenseTable, "modify_expense": bool(expense_id), "form": form, "user": request.user})
def expenses(request, expense_id=None, clone_from=None): """Display user expenses and expenses that he can validate""" expense_administrator, expense_manager, expense_paymaster, expense_requester = user_expense_perm( request.user) if not expense_requester: return HttpResponseRedirect(reverse("core:forbiden")) user_team = user_expense_team(request.user) consultant = Consultant.objects.get( trigramme__iexact=request.user.username) subcontractor = None if consultant.subcontractor: subcontractor = consultant try: if expense_id: expense = Expense.objects.get(id=expense_id) if not can_edit_expense(expense, request.user): messages.add_message( request, messages.WARNING, _("You are not allowed to edit that expense")) expense_id = None expense = None except Expense.DoesNotExist: messages.add_message(request, messages.ERROR, _("Expense %s does not exist" % expense_id)) expense_id = None if request.method == "POST": if expense_id: form = ExpenseForm(request.POST, request.FILES, instance=expense, subcontractor=subcontractor) else: form = ExpenseForm(request.POST, request.FILES, subcontractor=subcontractor) if form.is_valid(): expense = form.save(commit=False) if not hasattr(expense, "user"): # Don't update user if defined (case of expense updated by manager or administrator) expense.user = request.user expense.state = "REQUESTED" expense.workflow_in_progress = True expense.save() return HttpResponseRedirect(reverse("expense:expenses")) else: if expense_id: form = ExpenseForm(instance=expense, subcontractor=subcontractor ) # A form that edit current expense elif clone_from: try: expense = Expense.objects.get(id=clone_from) expense.pk = None # Null pk so it will generate a new fresh object during form submit expense.receipt = None # Never duplicate the receipt, a new one need to be provided form = ExpenseForm( instance=expense, subcontractor=subcontractor ) # A form with the new cloned expense (not saved) except Expense.DoesNotExist: form = ExpenseForm(initial={"expense_date": date.today()}) # An unbound form else: form = ExpenseForm(initial={"expense_date": date.today()}, subcontractor=subcontractor) # An unbound form # Get user expenses user_expenses = Expense.objects.filter( user=request.user, workflow_in_progress=True).select_related() if user_team: team_expenses = Expense.objects.filter( user__in=user_team, workflow_in_progress=True).select_related() else: team_expenses = [] if expense_administrator: # Admin manage all expenses managed_expenses = Expense.objects.filter( workflow_in_progress=True).select_related() elif expense_paymaster: # Paymaster manage all expenses except his own managed_expenses = Expense.objects.filter( workflow_in_progress=True).exclude( user=request.user).select_related() else: managed_expenses = team_expenses userExpenseTable = UserExpenseWorkflowTable(user_expenses) userExpenseTable.transitionsData = dict([ (e.id, []) for e in user_expenses ]) # Inject expense allowed transitions. Always empty for own expense userExpenseTable.expenseEditPerm = dict([ (e.id, can_edit_expense(e, request.user)) for e in user_expenses ]) # Inject expense edit permissions RequestConfig(request, paginate={ "per_page": 50 }).configure(userExpenseTable) managedExpenseTable = ManagedExpenseWorkflowTable(managed_expenses) managedExpenseTable.transitionsData = dict([ (e.id, expense_next_states(e, request.user)) for e in managed_expenses ]) # Inject expense allowed transitions managedExpenseTable.expenseEditPerm = dict([ (e.id, can_edit_expense(e, request.user)) for e in managed_expenses ]) # Inject expense edit permissions RequestConfig(request, paginate={ "per_page": 100 }).configure(managedExpenseTable) return render( request, "expense/expenses.html", { "user_expense_table": userExpenseTable, "managed_expense_table": managedExpenseTable, "modify_expense": bool(expense_id), "form": form, "user": request.user })
def expenses(request, expense_id=None): """Display user expenses and expenses that he can validate""" if not request.user.groups.filter(name="expense_requester").exists(): return HttpResponseRedirect(urlresolvers.reverse("forbiden")) try: consultant = Consultant.objects.get(trigramme__iexact=request.user.username) user_team = consultant.userTeam(excludeSelf=False) except Consultant.DoesNotExist: user_team = [] try: if expense_id: expense = Expense.objects.get(id=expense_id) if not (perm.has_permission(expense, request.user, "expense_edit") and (expense.user == request.user or expense.user in user_team)): messages.add_message(request, messages.WARNING, _("You are not allowed to edit that expense")) expense_id = None expense = None except Expense.DoesNotExist: messages.add_message(request, messages.ERROR, _("Expense %s does not exist" % expense_id)) expense_id = None if request.method == "POST": if expense_id: form = ExpenseForm(request.POST, request.FILES, instance=expense) else: form = ExpenseForm(request.POST, request.FILES) if form.is_valid(): expense = form.save(commit=False) if not hasattr(expense, "user"): # Don't update user if defined (case of expense updated by manager or adminstrator) expense.user = request.user expense.creation_date = date.today() expense.save() wf.set_initial_state(expense) return HttpResponseRedirect(urlresolvers.reverse("expense.views.expenses")) else: if expense_id: form = ExpenseForm(instance=expense) # A form that edit current expense else: form = ExpenseForm(initial={"expense_date": date.today()}) # An unbound form # Get user expenses user_expenses = Expense.objects.filter(user=request.user, workflow_in_progress=True).select_related() if user_team: team_expenses = Expense.objects.filter(user__in=user_team, workflow_in_progress=True).select_related() else: team_expenses = [] # Paymaster manage all expenses if utils.has_role(request.user, "expense paymaster"): managed_expenses = Expense.objects.filter(workflow_in_progress=True).exclude(user=request.user).select_related() else: managed_expenses = team_expenses userExpenseTable = UserExpenseWorkflowTable(user_expenses) userExpenseTable.transitionsData = dict([(e.id, []) for e in user_expenses]) # Inject expense allowed transitions. Always empty for own expense userExpenseTable.expenseEditPerm = dict([(e.id, perm.has_permission(e, request.user, "expense_edit")) for e in user_expenses]) # Inject expense edit permissions RequestConfig(request, paginate={"per_page": 50}).configure(userExpenseTable) managedExpenseTable = ManagedExpenseWorkflowTable(managed_expenses) managedExpenseTable.transitionsData = dict([(e.id, e.transitions(request.user)) for e in managed_expenses]) # Inject expense allowed transitions managedExpenseTable.expenseEditPerm = dict([(e.id, perm.has_permission(e, request.user, "expense_edit")) for e in managed_expenses]) # Inject expense edit permissions RequestConfig(request, paginate={"per_page": 100}).configure(managedExpenseTable) # Prune every expense not updated since 60 days. For instance, rejected expense. for expense in Expense.objects.filter(workflow_in_progress=True, update_date__lt=(date.today() - timedelta(60))): if wf.get_state(expense).transitions.count() == 0: expense.workflow_in_progress = False expense.save() return render(request, "expense/expenses.html", {"user_expense_table": userExpenseTable, "managed_expense_table": managedExpenseTable, "modify_expense": bool(expense_id), "form": form, "user": request.user})
def expenses(request, expense_id=None, clone_from=None): """Display user expenses and expenses that he can validate""" expense_administrator, expense_manager, expense_paymaster, expense_requester = user_expense_perm(request.user) if not expense_requester: return HttpResponseRedirect(reverse("core:forbiden")) user_team = user_expense_team(request.user) try: if expense_id: expense = Expense.objects.get(id=expense_id) if not can_edit_expense(expense, request.user): messages.add_message(request, messages.WARNING, _("You are not allowed to edit that expense")) expense_id = None expense = None except Expense.DoesNotExist: messages.add_message(request, messages.ERROR, _("Expense %s does not exist" % expense_id)) expense_id = None if request.method == "POST": if expense_id: form = ExpenseForm(request.POST, request.FILES, instance=expense) else: form = ExpenseForm(request.POST, request.FILES) if form.is_valid(): expense = form.save(commit=False) if not hasattr(expense, "user"): # Don't update user if defined (case of expense updated by manager or administrator) expense.user = request.user expense.state = "REQUESTED" expense.workflow_in_progress = True expense.save() return HttpResponseRedirect(reverse("expense:expenses")) else: if expense_id: form = ExpenseForm(instance=expense) # A form that edit current expense elif clone_from: try: expense = Expense.objects.get(id=clone_from) expense.pk = None # Null pk so it will generate a new fresh object during form submit expense.receipt = None # Never duplicate the receipt, a new one need to be provided form = ExpenseForm(instance=expense) # A form with the new cloned expense (not saved) except Expense.DoesNotExist: form = ExpenseForm(initial={"expense_date": date.today()}) # An unbound form else: form = ExpenseForm(initial={"expense_date": date.today()}) # An unbound form # Get user expenses user_expenses = Expense.objects.filter(user=request.user, workflow_in_progress=True).select_related() if user_team: team_expenses = Expense.objects.filter(user__in=user_team, workflow_in_progress=True).select_related() else: team_expenses = [] if expense_administrator: # Admin manage all expenses managed_expenses = Expense.objects.filter(workflow_in_progress=True).select_related() elif expense_paymaster: # Paymaster manage all expenses except his own managed_expenses = Expense.objects.filter(workflow_in_progress=True).exclude(user=request.user).select_related() else: managed_expenses = team_expenses userExpenseTable = UserExpenseWorkflowTable(user_expenses) userExpenseTable.transitionsData = dict([(e.id, []) for e in user_expenses]) # Inject expense allowed transitions. Always empty for own expense userExpenseTable.expenseEditPerm = dict([(e.id, can_edit_expense(e, request.user)) for e in user_expenses]) # Inject expense edit permissions RequestConfig(request, paginate={"per_page": 50}).configure(userExpenseTable) managedExpenseTable = ManagedExpenseWorkflowTable(managed_expenses) managedExpenseTable.transitionsData = dict([(e.id, expense_next_states(e, request.user)) for e in managed_expenses]) # Inject expense allowed transitions managedExpenseTable.expenseEditPerm = dict([(e.id, can_edit_expense(e, request.user)) for e in managed_expenses]) # Inject expense edit permissions RequestConfig(request, paginate={"per_page": 100}).configure(managedExpenseTable) return render(request, "expense/expenses.html", {"user_expense_table": userExpenseTable, "managed_expense_table": managedExpenseTable, "modify_expense": bool(expense_id), "form": form, "user": request.user})