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_renew(request, pk): """Renew an accreds""" 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('EDIT', request.user): raise Http404 if request.method == 'POST': for accred in accreds: accred.renewal_date = now() accred.save() AccreditationLog(accreditation=accred, who=request.user, type='renewed').save() from notifications.utils import unotify_people unotify_people('Accreds.Warning', accred) if multi_obj: messages.success(request, _(u'Accréditations renouvelées !')) else: messages.success(request, _(u'Accréditation renouvelée !')) return redirect('units-views-accreds_list') return render(request, 'units/accreds/renew.html', {'accreds': accreds, 'multi_obj': multi_obj})
def handle(self, *args, **options): """Rules: All users with role AUTO_RLC_COMS_ROLES in commissions or AUTO_RLC_ROOT_ROLES in root unit are given AUTO_RLC_GIVEN_ROLE""" rlc_unit = Unit.objects.get(pk=settings.AUTO_RLC_UNIT_PK) rlc_role = Role.objects.get(pk=settings.AUTO_RLC_GIVEN_ROLE) system_user = TruffeUser.objects.get(pk=settings.SYSTEM_USER_PK) valids_accreds = [] def _do(unit, roles): for accred in unit.accreditation_set.filter(end_date=None, role__pk__in=roles): rlc_accred, created = Accreditation.objects.get_or_create(unit=rlc_unit, user=accred.user, end_date=None, role=rlc_role, display_name=u'{} {} ({})'.format(settings.AUTO_RLC_TAG, accred.get_role_or_display_name(), unit), no_epfl_sync=False, hidden_in_epfl=True, hidden_in_truffe=True, need_validation=False) rlc_accred.renewal_date = accred.renewal_date rlc_accred.save() if created: AccreditationLog(accreditation=rlc_accred, who=system_user, what='autocreated').save() valids_accreds.append(rlc_accred) _do(Unit.objects.get(pk=settings.ROOT_UNIT_PK), settings.AUTO_RLC_ROOT_ROLES) for unit in Unit.objects.filter(is_commission=True): _do(unit, settings.AUTO_RLC_COMS_ROLES) for old_accred in Accreditation.objects.filter(end_date=None, unit=rlc_unit, display_name__startswith=settings.AUTO_RLC_TAG).exclude(pk__in=map(lambda u: u.pk, valids_accreds)): old_accred.end_date = now() old_accred.save() AccreditationLog(accreditation=old_accred, who=system_user, what='autodeleted').save()
def accreds_delete(request, pk): """Delete 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 cannot_last_prez = False cannot_last_prez_accred = None for accred in accreds: if not accred.rights_can('DELETE', request.user): raise Http404 if accred.role.pk == settings.PRESIDENT_ROLE_PK and not accred.rights_can( 'INGORE_PREZ', request.user) and accred.unit.accreditation_set.filter( role__pk=settings.PRESIDENT_ROLE_PK, end_date=None).count() <= 1: cannot_last_prez = True cannot_last_prez_accred = accred if not cannot_last_prez and request.method == 'POST': for accred in accreds: accred.end_date = now() accred.save() from notifications.utils import unotify_people unotify_people('Accreds.ToValidate', accred) AccreditationLog(accreditation=accred, who=request.user, type='deleted').save() accred.user.clear_rights_cache() if multi_obj: messages.success(request, _(u'Accréditations supprimées !')) else: messages.success(request, _(u'Accréditation supprimée !')) return redirect('units-views-accreds_list') return render( request, 'units/accreds/delete.html', { 'accreds': accreds, 'cannot_last_prez': cannot_last_prez, 'multi_obj': multi_obj, 'cannot_last_prez_accred': cannot_last_prez_accred })
def accreds_add(request): update_current_unit(request, request.GET.get('upk')) unit = get_current_unit(request) if not Accreditation.static_rights_can('CREATE', request.user, unit): raise Http404 from units.forms2 import AccreditationAddForm from units.models import Role done = False if request.method == 'POST': # If the form has been submitted... form = AccreditationAddForm(request.user, request.POST) if form.is_valid(): # If the form is valid accred = form.save(commit=False) accred.unit = unit # Try to find the user. If he dosen't exists, create it. try: user = TruffeUser.objects.get(username=form.cleaned_data['user'].strip()) except TruffeUser.DoesNotExist: user = TruffeUser() user.username = form.cleaned_data['user'].strip() user.last_name, user.first_name, user.email = get_attrs_of_sciper(user.username) user.is_active = True user.save() accred.user = user accred.save() AccreditationLog(accreditation=accred, who=request.user, type='created').save() # Check if validation is needed accred.check_if_validation_needed(request) accred.save() accred.user.clear_rights_cache() messages.success(request, _(u'Accréditation sauvegardée !')) done = True else: form = AccreditationAddForm(request.user) validables = Role.objects.filter(deleted=False, need_validation=True) return render(request, 'units/accreds/add.html', {'form': form, 'done': done, 'unit': unit, 'validables': validables})
def _do(unit, roles): for accred in unit.accreditation_set.filter(end_date=None, role__pk__in=roles): rlc_accred, created = Accreditation.objects.get_or_create(unit=rlc_unit, user=accred.user, end_date=None, role=rlc_role, display_name=u'{} {} ({})'.format(settings.AUTO_RLC_TAG, accred.get_role_or_display_name(), unit), no_epfl_sync=False, hidden_in_epfl=True, hidden_in_truffe=True, need_validation=False) rlc_accred.renewal_date = accred.renewal_date rlc_accred.save() if created: AccreditationLog(accreditation=rlc_accred, who=system_user, what='autocreated').save() valids_accreds.append(rlc_accred)
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 accreds_edit(request, pk): accred = get_object_or_404(Accreditation, pk=pk) base_role = accred.role old_accred_data = accred.__dict__.copy() if not accred.rights_can('EDIT', request.user): raise Http404 from units.forms2 import AccreditationEditForm from units.models import Role done = False cannot_last_prez = False if request.method == 'POST': # If the form has been submitted... form = AccreditationEditForm(request.POST, instance=accred) if form.is_valid(): # If the form is valid if base_role.pk == settings.PRESIDENT_ROLE_PK and not accred.rights_can( 'INGORE_PREZ', request.user) and accred.unit.accreditation_set.filter( role__pk=settings.PRESIDENT_ROLE_PK, end_date=None).count() <= 1 and form.cleaned_data[ 'role'].pk != settings.PRESIDENT_ROLE_PK: cannot_last_prez = True else: accred = form.save(commit=False) if accred.role.pk != base_role.pk: # On termine l'accrédiation old_accred = get_object_or_404(Accreditation, pk=pk) old_accred.end_date = now() old_accred.need_validation = False old_accred.save() AccreditationLog(accreditation=old_accred, who=request.user, type='deleted').save() # Et on clone la nouvelle accred.pk = None accred.end_date = None accred.need_validation = False accred.save() accred.check_if_validation_needed(request) accred.user.clear_rights_cache() AccreditationLog(accreditation=accred, who=request.user, type='created').save() else: changes = "" # NB: Pas traduit, au cas ou besion de parsing if accred.display_name != old_accred_data['display_name']: changes += "DisplayName \"%s\" -> \"%s\"\n" % ( old_accred_data['display_name'], accred.display_name, ) if accred.no_epfl_sync != old_accred_data['no_epfl_sync']: if accred.no_epfl_sync: changes += 'Now NoEpflSync\n' else: changes += 'Now EpflSync\n' if accred.hidden_in_epfl != old_accred_data[ 'hidden_in_epfl']: if accred.hidden_in_epfl: changes += 'Now EpflHidden\n' else: changes += 'Now EpflShown\n' if accred.hidden_in_truffe != old_accred_data[ 'hidden_in_truffe']: if accred.hidden_in_truffe: changes += 'Now TruffeHidden\n' else: changes += 'Now TruffeShown\n' AccreditationLog(accreditation=accred, who=request.user, type='edited', what=changes).save() accred.save() accred.user.clear_rights_cache() messages.success(request, _(u'Accréditation sauvegardée !')) done = True else: form = AccreditationEditForm(instance=accred) validables = Role.objects.filter(deleted=False, need_validation=True) return render( request, 'units/accreds/edit.html', { 'form': form, 'done': done, 'cannot_last_prez': cannot_last_prez, 'validables': validables })