def form_valid(self, form, form_media): media = form_media.save(commit=False) media.author = self.request.user.person if media.file: media.file.name = self.get_filename(media.file.name) media.save() form_media.save_m2m() agreement = form.save(commit=False) agreement.partnership = self.partnership agreement.media = media agreement.save() form.save_m2m() self.check_dates(agreement) messages.success(self.request, _('partnership_agreement_success')) # Send notification e-mail when not ADRI if not is_linked_to_adri_entity(self.request.user): title = '{} - {}'.format( _('partnership_agreement_created'), self.partnership.ucl_entity.most_recent_acronym ) self.notify_admin_mail(title, 'agreement_creation.html', { 'partnership': self.partnership, }) return redirect(self.partnership)
def __init__(self, partnership_type=None, *args, **kwargs): super().__init__(partnership_type, *args, **kwargs) config = PartnershipConfiguration.get_configuration() current_academic_year = config.partnership_creation_update_min_year is_adri = is_linked_to_adri_entity(self.user) if self.instance.partnership_id: # Update partnership = self.instance.partnership if (current_academic_year is not None and current_academic_year.year > partnership.end_academic_year.year): self.fields[ 'end_academic_year'].initial = current_academic_year else: self.fields[ 'end_academic_year'].initial = partnership.end_academic_year if is_adri or partnership_type != PartnershipType.MOBILITY.name: self.fields[ 'start_academic_year'].initial = partnership.start_academic_year else: del self.fields['start_academic_year'] self.fields['from_academic_year'].initial = current_academic_year else: # Create self.fields['start_academic_year'].initial = current_academic_year del self.fields['from_academic_year'] self.fields['end_academic_year'].initial = current_academic_year
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['funding'].help_text = _('help_text_funding_type') self.fields['funding'].required = False self.fields['eligible'].widget = forms.CheckboxInput() is_adri = is_linked_to_adri_entity(self.user) config = PartnershipConfiguration.get_configuration() current_academic_year = config.partnership_creation_update_min_year if not is_adri: del self.fields['eligible'] if current_academic_year is not None: future_academic_years = AcademicYear.objects.filter( year__gte=current_academic_year.year) if 'start_academic_year' in self.fields: self.fields[ 'start_academic_year'].queryset = future_academic_years if 'from_academic_year' in self.fields: self.fields[ 'from_academic_year'].queryset = future_academic_years self.fields[ 'end_academic_year'].queryset = future_academic_years
def __init__(self, partnership_type=None, *args, **kwargs): super().__init__(partnership_type, *args, **kwargs) self.fields['partner_entities'].widget.attrs = { 'data-maximum-selection-length': 1, } field = self.fields['ucl_entity'] field.queryset = field.queryset.filter( self.get_partnership_entities_managed(self.user)).distinct() if not is_linked_to_adri_entity(self.user): # Restrict fields for GF and GS if field.queryset.count() == 1: field.initial = field.queryset.first().pk field.disabled = True if self.instance.pk is not None: # TODO This is a guess, need to check with Bart self.fields['partner_entities'].disabled = True field.disabled = True del self.fields['is_public'] self.fields['supervisor'].required = False self.fields['supervisor'].widget.attrs = { 'data-placeholder': _('same_supervisor_than_management_entity') }
def __init__(self, *args, **kwargs): user = kwargs.pop('user') super().__init__(*args, **kwargs) if not is_linked_to_adri_entity(user): del self.fields['is_valid'] if self.instance.pk is not None: self.fields['now_known_as'].queryset = self.fields[ 'now_known_as'].queryset.exclude(pk=self.instance.pk) self.fields['now_known_as'].queryset = self.fields[ 'now_known_as'].queryset.order_by('organization__name')
def get_queryset(self): cte = EntityVersion.objects.with_children() qs = cte.join(EntityVersion, id=cte.col.id).with_cte(cte).annotate( children=cte.col.children, ).filter( children__contains_any=OuterRef('entity__pk'), ) queryset = ( UCLManagementEntity.objects .annotate( faculty_most_recent_acronym=Subquery( qs.filter(entity_type=FACULTY).values('acronym')[:1] ), entity_most_recent_acronym=Subquery( EntityVersion.objects .filter(entity=OuterRef('entity__pk')) .order_by('-start_date') .values('acronym')[:1] ), ) .order_by('faculty_most_recent_acronym', 'entity_most_recent_acronym') .select_related( 'academic_responsible', 'administrative_responsible', 'contact_in_person', 'contact_out_person', 'entity', ) .prefetch_related('entity__partnerships') ) if not is_linked_to_adri_entity(self.request.user): # get what the user manages person = self.request.user.person entities_managed_by_user = list(PartnershipEntityManager.get_person_related_entities(person)) # get the children qs = cte.queryset().with_cte(cte).filter( entity_id__in=entities_managed_by_user ).annotate( child_entity_id=Func(cte.col.children, function='unnest'), ).distinct('child_entity_id').values('child_entity_id') # check if entity is part of entity the user manages queryset = queryset.filter( entity__in=qs, ) return queryset.distinct()
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) if "application/json" in self.request.headers.get("Accept", ""): return context user = self.request.user context['can_change_configuration'] = is_linked_to_adri_entity(user) context['can_add_partnership'] = any([ t for t in PartnershipType if user.has_perm('partnership.add_partnership', t) ]) context['url'] = reverse('partnerships:list') context['search_button_label'] = _('search_partnership') context['export_years'] = AcademicYear.objects.annotate( current_year=Subquery( AcademicYear.objects.currents().values('year')[:1]), ).filter( year__gte=F('current_year'), ).order_by('year')[:3] return context
def form_valid(self, form, form_year): partnership = form.save(commit=False) partnership.author = self.request.user.person if partnership.partnership_type in PartnershipType.with_synced_dates(): start_academic_year = form_year.cleaned_data['start_academic_year'] end_academic_year = form_year.cleaned_data['end_academic_year'] partnership.start_date = start_academic_year.start_date partnership.end_date = end_academic_year.end_date else: years = find_academic_years( # We need academic years surrounding this time range start_date=partnership.end_date, end_date=partnership.start_date, ) start_academic_year = years.first() end_academic_year = years.last() # Resume saving partnership.save() form.save_m2m() # Create years start_year = start_academic_year.year end_year = end_academic_year.year academic_years = find_academic_years(start_year=start_year, end_year=end_year) for academic_year in academic_years: partnership_year = form_year.save(commit=False) partnership_year.id = None # Force the creation of a new PartnershipYear partnership_year.partnership = partnership partnership_year.academic_year = academic_year partnership_year.save() form_year.save_m2m() messages.success(self.request, _('partnership_success')) if not is_linked_to_adri_entity(self.request.user): title = '{} - {}'.format( _('partnership_created'), partnership.ucl_entity.most_recent_acronym ) self.notify_admin_mail(title, 'partnership_creation.html', { 'partnership': partnership, }) return redirect(partnership)
def __init__(self, *args, **kwargs): user = kwargs.pop('user') super().__init__(*args, **kwargs) self.fields['name'].label = _('partner_name') self.fields['type'].label = _('partner_type') self.fields['type'].required = True self.fields['type'].choices = [ c for c in self.fields['type'].choices if c[0] != MAIN ] self.fields['code'].label = _('partner_code') if not self.instance.pk: self.fields['start_date'].help_text = '' else: self.fields['start_date'].initial = self.instance.start_date self.fields['end_date'].initial = self.instance.end_date self.fields['website'].initial = self.instance.website if not is_linked_to_adri_entity(user): del self.fields['code']
def form_valid(self, form, form_address, organization_form): changed = (form.has_changed() or form_address.has_changed() or organization_form.has_changed()) # Save organization organization = organization_form.save() # Save related entity entity, created = EntityProxy.objects.only_roots().update_or_create( defaults=dict(website=organization_form.cleaned_data['website'], ), organization_id=organization.pk, ) last_version = self.save_entity_version( changed, entity, organization_form, ) partner = form.save(commit=False) is_creation = partner.pk is None if is_creation: partner.author = self.request.user.person partner.organization = organization partner.save() form.save_m2m() # Save address self.save_address(last_version, form_address) messages.success(self.request, _('partner_saved')) if is_creation and not is_linked_to_adri_entity(self.request.user): self.notify_admin_mail( '{} - {}'.format(_('partner_created'), organization.name), 'partner_creation.html', { 'user': self.request.user, 'partner': partner, }, ) return redirect(partner)
def get_partnership_entities_managed(cls, user): """ Filter entities : - must have UCLManagementEntity associated - if user is not adri, only entities the user is allowed to manage :param user: :return: Q object """ # Must have UCL management entity conditions = Q(uclmanagement_entity__isnull=False) if not is_linked_to_adri_entity(user): # Get children entities which user has a PartnershipEntityManager cte = EntityVersion.objects.with_children(entity_id=OuterRef('pk')) qs = cte.join(PartnershipEntityManager, entity_id=cte.col.entity_id).with_cte(cte).filter( person=user.person).annotate( child_entity_id=Func(cte.col.children, function='unnest'), ) # Restrict fields for GF and GS conditions &= Q(pk__in=CTESubquery(qs.values('child_entity_id'))) return conditions
def form_valid(self, form, form_year): partnership = form.save() year_data = form_year.cleaned_data new_end_year = year_data.get('end_academic_year') if (new_end_year and new_end_year != self.object.end_academic_year and not is_linked_to_adri_entity(self.request.user)): title = _( 'partnership_end_year_updated_{partner}_{faculty}').format( partner=partnership.first_partner_name, faculty=partnership.ucl_entity.most_recent_acronym, ) self.notify_admin_mail(title, 'partnership_update.html', { 'partnership': partnership, }) start_year = None start_academic_year = year_data.get('start_academic_year', None) # Create missing start year if needed if start_academic_year is not None: start_year = start_academic_year.year first_year = partnership.years.order_by( 'academic_year__year').select_related('academic_year').first() if first_year is not None: first_year_education_fields = first_year.education_fields.all() first_year_education_levels = first_year.education_levels.all() first_year_entities = first_year.entities.all() first_year_offers = first_year.offers.all() academic_years = find_academic_years( start_year=start_year, end_year=first_year.academic_year.year - 1) for academic_year in academic_years: first_year.id = None first_year.academic_year = academic_year first_year.save() first_year.education_fields.set( first_year_education_fields) first_year.education_levels.set( first_year_education_levels) first_year.entities.set(first_year_entities) first_year.offers.set(first_year_offers) # Update years if partnership.partnership_type in PartnershipType.with_synced_dates(): from_year = year_data['from_academic_year'].year end_year = year_data['end_academic_year'].year academic_years = find_academic_years( start_year=from_year, end_year=end_year, ) else: academic_years = find_academic_years( # We need academic years surrounding this time range start_date=partnership.end_date, end_date=partnership.start_date, ) end_year = academic_years.last().year for academic_year in academic_years: partnership_year = form_year.save(commit=False) existing_year = PartnershipYear.objects.filter( partnership=partnership, academic_year=academic_year).first() partnership_year.pk = existing_year.pk if existing_year else None partnership_year.partnership = partnership partnership_year.academic_year = academic_year partnership_year.save() form_year.save_m2m() # Delete no longer used years if partnership.partnership_type in PartnershipType.with_synced_dates(): query = Q(academic_year__year__gt=end_year) if start_academic_year is not None: query |= Q(academic_year__year__lt=start_year) else: query = Q(academic_year__start_date__gt=partnership.end_date) query |= Q(academic_year__end_date__lt=partnership.start_date) partnership.years.filter(query).delete() # Sync dates if not form.cleaned_data.get('start_date') and start_academic_year: partnership.start_date = start_academic_year.start_date partnership.save() if not form.cleaned_data.get('end_date'): partnership.end_date = year_data['end_academic_year'].end_date partnership.save() messages.success(self.request, _('partnership_success')) return redirect(partnership)
def __init__(self, user=None, *args, **kwargs): super().__init__(*args, **kwargs) if not is_linked_to_adri_entity(user): del self.fields['status']