def switch_status_signal(self, request, old_status, dest_status): s = super(_AccountingError, self) if hasattr(s, 'switch_status_signal'): s.switch_status_signal(request, old_status, dest_status) if dest_status == '2_fixed': if request.POST.get('fix_errors'): if self.linked_line and not self.linked_line.deleted and self.linked_line.status == '2_error': from accounting_main.models import AccountingLineLogging old_status = self.linked_line.status self.linked_line.status = '1_validated' self.linked_line.save() AccountingLineLogging(who=request.user, what='state_changed', object=self.linked_line, extra_data=json.dumps({'old': unicode(self.linked_line.MetaState.states.get(old_status)), 'new': unicode(self.linked_line.MetaState.states.get('1_validated'))})).save() unotify_people(u'AccountingLine.{}.error'.format(self.costcenter.unit), self.linked_line) notify_people(request, u'AccountingLine.{}.fixed'.format(self.costcenter.unit), 'accounting_line_fixed', self.linked_line, self.linked_line.build_group_members_for_compta_everyone()) unotify_people(u'AccountingError.{}.created'.format(self.costcenter.unit), self) notify_people(request, u'AccountingError.{}.fixed'.format(self.costcenter.unit), 'accounting_error_fixed', self, self.build_group_members_for_compta_everyone_with_messages())
def accreds_validate(request, pk): """Validate an accred""" accreds = [get_object_or_404(Accreditation, pk=pk_, end_date=None) for pk_ in filter(lambda x: x, pk.split(','))] multi_obj = len(accreds) > 1 for accred in accreds: if not accred.rights_can('VALIDATE', request.user): raise Http404 if request.method == 'POST': for accred in accreds: accred.need_validation = False accred.save() accred.user.clear_rights_cache() AccreditationLog(accreditation=accred, who=request.user, type='validated').save() from notifications.utils import notify_people, unotify_people dest_users = accred.unit.users_with_access('ACCREDITATION', no_parent=True) notify_people(request, 'Accreds.Validated', 'accreds_validated', accred, dest_users) unotify_people('Accreds.ToValidate', accred) if multi_obj: messages.success(request, _(u'Accréditations validées !')) else: messages.success(request, _(u'Accréditation validée !')) return redirect('units.views.accreds_list') return render(request, 'units/accreds/validate.html', {'accreds': accreds, 'multi_obj': multi_obj})
def accreds_validate(request, pk): """Validate an accred""" accreds = [get_object_or_404(Accreditation, pk=pk_, end_date=None) for pk_ in filter(lambda x: x, pk.split(','))] multi_obj = len(accreds) > 1 for accred in accreds: if not accred.rights_can('VALIDATE', request.user): raise Http404 if request.method == 'POST': for accred in accreds: accred.need_validation = False accred.save() accred.user.clear_rights_cache() AccreditationLog(accreditation=accred, who=request.user, type='validated').save() from notifications.utils import notify_people, unotify_people dest_users = accred.unit.users_with_access('ACCREDITATION', no_parent=True) notify_people(request, 'Accreds.Validated', 'accreds_validated', accred, dest_users) unotify_people('Accreds.ToValidate', accred) if multi_obj: messages.success(request, _(u'Accréditations validées !')) else: messages.success(request, _(u'Accréditation validée !')) return redirect('units-views-accreds_list') return render(request, 'units/accreds/validate.html', {'accreds': accreds, 'multi_obj': multi_obj})
def switch_status_signal(self, request, old_status, dest_status): s = super(_AccountingError, self) if hasattr(s, 'switch_status_signal'): s.switch_status_signal(request, old_status, dest_status) if dest_status == '2_fixed': if request.POST.get('fix_errors'): if self.linked_line and not self.linked_line.deleted and self.linked_line.status == '2_error': from accounting_main.models import AccountingLineLogging old_status = self.linked_line.status self.linked_line.status = '1_validated' self.linked_line.save() AccountingLineLogging(who=request.user, what='state_changed', object=self.linked_line, extra_data=json.dumps({'old': str(self.linked_line.MetaState.states.get(old_status)), 'new': str(self.linked_line.MetaState.states.get('1_validated'))})).save() unotify_people(u'AccountingLine.{}.error'.format(self.costcenter.unit), self.linked_line) notify_people(request, u'AccountingLine.{}.fixed'.format(self.costcenter.unit), 'accounting_line_fixed', self.linked_line, self.linked_line.build_group_members_for_compta_everyone()) unotify_people(u'AccountingError.{}.created'.format(self.costcenter.unit), self) notify_people(request, u'AccountingError.{}.fixed'.format(self.costcenter.unit), 'accounting_error_fixed', self, self.build_group_members_for_compta_everyone_with_messages())
def accreds_validate(request, pk): """Validate an accred""" accreds = [get_object_or_404(Accreditation, pk=pk_, end_date=None) for pk_ in filter(lambda x: x, pk.split(","))] multi_obj = len(accreds) > 1 for accred in accreds: if not accred.rights_can("VALIDATE", request.user): raise Http404 if request.method == "POST": for accred in accreds: accred.need_validation = False accred.save() accred.user.clear_rights_cache() AccreditationLog(accreditation=accred, who=request.user, type="validated").save() from notifications.utils import notify_people dest_users = accred.unit.users_with_access("ACCREDITATION", no_parent=True) notify_people(request, "Accreds.Validated", "accreds_validated", accred, dest_users) if multi_obj: messages.success(request, _(u"Accréditations validées !")) else: messages.success(request, _(u"Accréditation validée !")) return redirect("units.views.accreds_list") return render(request, "units/accreds/validate.html", {"accreds": accreds, "multi_obj": multi_obj})
def accounting_import_step1(request, key): (session_key, session_data) = _get_import_session_data(request, key) if not session_key: return session_data # Not very clean ^^' if session_data['has_data']: return redirect('accounting_main.views.accounting_import_step2', key) from accounting_main.forms2 import ImportForm if request.method == 'POST': if request.GET.get('send') == 'notif': from units.models import Unit people = [] for unit in Unit.objects.filter(deleted=False): for user in unit.people_in_unit(unit, 'TRESORERIE', no_parent=True): if user not in people: people.append(user) notify_people(request, 'Accounting.NewCompta', 'accounting_new_compta', request.user, people, {'notification_force_url': reverse('accounting_main.views.accountingline_list')}) messages.success(request, "Notification envoyée !") form = ImportForm() else: form = ImportForm(request.POST, request.FILES) if form.is_valid(): file_key = '/tmp/truffe_import_{}_data.file'.format(key) with open(file_key, 'wb+') as destination: for chunk in request.FILES['file'].chunks(): destination.write(chunk) if form.cleaned_data['type'] == 'csv_2014': wanted_data = _csv_2014_processor(request, file_key) if wanted_data: diff = _diff_generator(request, form.cleaned_data['year'], wanted_data) if diff: session_data['data'] = diff session_data['year'] = form.cleaned_data['year'].pk session_data['has_data'] = True request.session[session_key] = session_data return redirect('accounting_main.views.accounting_import_step2', key) else: form = ImportForm() return render(request, "accounting_main/import/step1.html", {'key': key, 'form': form})
def errors_send_message(request, pk): from accounting_main.models import AccountingError, AccountingErrorMessage error = get_object_or_404(AccountingError, pk=pk) if not error.rights_can('ADD_COMMENT', request.user): raise Http404 AccountingErrorMessage(author=request.user, message=request.POST.get('message'), error=error).save() notify_people(request, u'AccountingError.{}.message'.format(error.costcenter.unit), 'accounting_error_message', error, error.build_group_members_for_compta_everyone_with_messages(), {'message': request.POST.get('message')}) messages.success(request, _(u'Message ajouté !')) return HttpResponse('')
def handle(self, *args, **options): days_before_warnings = [30, 15, 7, 3, 2, 1] system_user = TruffeUser.objects.get(pk=settings.SYSTEM_USER_PK) # On travaille par unité for u in Unit.objects.filter(deleted=False): # Les destinataires dest_users = u.users_with_access('INFORMATIQUE', no_parent=True) to_warning = {} for d in days_before_warnings: to_warning[d] = [] to_delete = [] # Toutes les accreds encore valides for a in u.accreditation_set.filter(end_date=None): # Nombre de jours avant l'expiration delta = ((a.renewal_date + datetime.timedelta(days=365)) - now()).days # Faut-il supprimer l'accred ? if delta <= 0: a.end_date = now() a.save() AccreditationLog(accreditation=a, who=system_user, what='autodeleted').save() to_delete.append(a) # Faut-il prévenir les responsables ? if delta in days_before_warnings: to_warning[delta].append(a) for d in days_before_warnings: if to_warning[d]: notify_people(None, 'Accreds.Warning', 'accreds_warning', u, dest_users, {'jours': d, 'accreds': map(lambda a: {'pk': a.pk, 'user': str(a.user), 'role': str(a.role)}, to_warning[d])}) if to_delete: notify_people(None, 'Accreds.Deleted', 'accreds_deleted', u, dest_users, {'accreds': map(lambda a: {'pk': a.pk, 'user': str(a.user), 'role': str(a.role)}, to_delete)})
def check_if_validation_needed(self, request): if not self.role.need_validation: return if self.rights_can('VALIDATE', request.user): return if not self.unit.is_commission: # Seulement pour les commisions ! return self.need_validation = True from notifications.utils import notify_people dest_users = self.people_in_root_unit('ACCREDITATION') for user in self.people_in_root_unit('COMMISSIONS'): if user not in dest_users: dest_users.append(user) notify_people(request, 'Accreds.ToValidate', 'accreds_tovalidate', self, dest_users)
def switch_status_signal(self, request, old_status, dest_status): from accounting_main.models import AccountingError, AccountingErrorLogging s = super(_AccountingLine, self) if hasattr(s, 'switch_status_signal'): s.switch_status_signal(request, old_status, dest_status) if dest_status == '2_error': if request.POST.get('error'): ae = AccountingError(initial_remark=request.POST.get('error'), linked_line=self, accounting_year=self.accounting_year, costcenter=self.costcenter) ae.save() AccountingErrorLogging(who=request.user, what='created', object=ae).save() notify_people(request, u'AccountingError.{}.created'.format(self.costcenter.unit), 'accounting_error_created', ae, ae.build_group_members_for_compta_everyone_with_messages()) unotify_people(u'AccountingLine.{}.fixed'.format(self.costcenter.unit), self) notify_people(request, u'AccountingLine.{}.error'.format(self.costcenter.unit), 'accounting_line_error', self, self.build_group_members_for_compta_everyone()) if dest_status == '1_validated': if old_status == '2_error': unotify_people(u'AccountingLine.{}.error'.format(self.costcenter.unit), self) notify_people(request, u'AccountingLine.{}.fixed'.format(self.costcenter.unit), 'accounting_line_fixed', self, self.build_group_members_for_compta_everyone()) if request.POST.get('fix_errors'): for error in self.accountingerror_set.filter(deleted=False).exclude(status='2_fixed'): old_status = error.status error.status = '2_fixed' error.save() AccountingErrorLogging(who=request.user, what='state_changed', object=error, extra_data=json.dumps({'old': unicode(error.MetaState.states.get(old_status)), 'new': unicode(error.MetaState.states.get('2_fixed'))})).save() unotify_people(u'AccountingError.{}.created'.format(self.costcenter.unit), error) notify_people(request, u'AccountingError.{}.fixed'.format(self.costcenter.unit), 'accounting_error_fixed', error, error.build_group_members_for_compta_everyone_with_messages())
def switch_status_signal(self, request, old_status, dest_status): from accounting_main.models import AccountingError, AccountingErrorLogging s = super(_AccountingLine, self) if hasattr(s, 'switch_status_signal'): s.switch_status_signal(request, old_status, dest_status) if dest_status == '2_error': if request.POST.get('error'): ae = AccountingError(initial_remark=request.POST.get('error'), linked_line=self, accounting_year=self.accounting_year, costcenter=self.costcenter) ae.save() AccountingErrorLogging(who=request.user, what='created', object=ae).save() notify_people(request, u'AccountingError.{}.created'.format(self.costcenter.unit), 'accounting_error_created', ae, ae.build_group_members_for_compta_everyone_with_messages()) unotify_people(u'AccountingLine.{}.fixed'.format(self.costcenter.unit), self) notify_people(request, u'AccountingLine.{}.error'.format(self.costcenter.unit), 'accounting_line_error', self, self.build_group_members_for_compta_everyone()) if dest_status == '1_validated': if old_status == '2_error': unotify_people(u'AccountingLine.{}.error'.format(self.costcenter.unit), self) notify_people(request, u'AccountingLine.{}.fixed'.format(self.costcenter.unit), 'accounting_line_fixed', self, self.build_group_members_for_compta_everyone()) if request.POST.get('fix_errors'): for error in self.accountingerror_set.filter(deleted=False).exclude(status='2_fixed'): old_status = error.status error.status = '2_fixed' error.save() AccountingErrorLogging(who=request.user, what='state_changed', object=error, extra_data=json.dumps({'old': str(error.MetaState.states.get(old_status)), 'new': str(error.MetaState.states.get('2_fixed'))})).save() unotify_people(u'AccountingError.{}.created'.format(self.costcenter.unit), error) notify_people(request, u'AccountingError.{}.fixed'.format(self.costcenter.unit), 'accounting_error_fixed', error, error.build_group_members_for_compta_everyone_with_messages())
def accounting_import_step1(request, key): (session_key, session_data) = _get_import_session_data(request, key) if not session_key: return session_data # Not very clean ^^' if session_data['has_data']: return redirect('accounting_main-views-accounting_import_step2', key) from accounting_main.forms2 import ImportForm if request.method == 'POST': if request.GET.get('send') == 'notif': from units.models import Unit people = [] for unit in Unit.objects.filter(deleted=False): for user in unit.people_in_unit(unit, 'TRESORERIE', no_parent=True): if user not in people: people.append(user) notify_people( request, 'Accounting.NewCompta', 'accounting_new_compta', request.user, people, { 'notification_force_url': reverse('accounting_main-views-accountingline_list') }) messages.success(request, "Notification envoyée !") form = ImportForm() else: form = ImportForm(request.POST, request.FILES) if form.is_valid(): file_key = '/tmp/truffe_import_{}_data.file'.format(key) with open(file_key, 'wb+') as destination: for chunk in request.FILES['file'].chunks(): destination.write(chunk) wanted_data = None if form.cleaned_data['type'] == 'csv_2014': wanted_data = _csv_2014_processor(request, file_key) elif form.cleaned_data['type'] == 'tab_2016': wanted_data = _tab_2016_processor(request, file_key) if wanted_data: diff = _diff_generator(request, form.cleaned_data['year'], wanted_data) if diff: session_data['data'] = diff session_data['year'] = form.cleaned_data['year'].pk session_data['has_data'] = True request.session[session_key] = session_data return redirect( 'accounting_main-views-accounting_import_step2', key) else: form = ImportForm() return render(request, "accounting_main/import/step1.html", { 'key': key, 'form': form })
def create_signal(self, request): notify_people(request, u'AccountingError.{}.created'.format(self.costcenter.unit), 'accounting_error_created', self, self.build_group_members_for_compta_everyone_with_messages())