def get_context_data(self, *args, **kwargs): default_language_code = 'en' # default_language = aims.language.objects.get(code=default_language_code) exists = True while exists: id_number = str(random.randint(0, 10000)).zfill(4) iati_identifier = 'MM-FERD-ID%s' % id_number exists = activity.objects.filter(iati_identifier=iati_identifier).exists() def prepare_language_fields(formset): if not formset[0].fields['language'].initial: formset[0].fields['language'].initial = default_language_code if not formset[1].fields['language'].initial: formset[1].fields['language'].initial = 'my' placeholder = {'placeholder': _('Myanmar language translation')} if 'title' in formset[1].fields: formset[1].fields['title'].widget.attrs.update(placeholder) else: formset[1].fields['description'].widget.attrs.update(placeholder) return formset context = super(CreateLocalActivity, self).get_context_data() context['editor'] = True context['form'] = kwargs['form'] context['form'].fields['remote_data'].initial = '' context['form'].fields['iati_identifier'].initial = iati_identifier context['form'].fields['iati_identifier'].widget.attrs['readonly'] = True context['form'].fields['id'].initial = iati_identifier context['form_helper'] = forms.FormHelper() request_data = None if self.request.method == "POST": request_data = self.request.POST context['sections'] = aims_sections("CPU") title = forms.TitleFormset(request_data) description = forms.DescriptionFormset(request_data) objective = forms.ObjectivesFormset(request_data, prefix="objectives") target_groups = forms.TargetGroupsFormset(request_data, prefix="target_groups") context['title'] = prepare_language_fields(title) context['description'] = prepare_language_fields(description) context['objective'] = prepare_language_fields(objective) context['target_groups'] = prepare_language_fields(target_groups) context['activity_sector'] = forms.ActivitySectorFormset(request_data, queryset=aims.activity_sector.objects.exclude(sector__code__startswith=550), prefix="sector") context['activity_national_sector'] = forms.ActivityNationalSectorFormset(request_data, queryset=aims.activity_sector.objects.filter(sector__code__startswith=550), prefix="national_sector") context['ministry'] = forms.MinistryFormset(request_data, prefix="ministry", queryset=aims.activity_participating_organisation.objects.filter(role='Accountable')) context['participating_organisation'] = forms.ParticipatingOrganisationFormset(request_data, prefix="participating_organisation", queryset=aims.activity_participating_organisation.objects.exclude(role__in=("Accountable", "Funding"))) context['location'] = forms.LocationFormset(request_data, prefix="location", ) # queryset=self.object.location_set.all()) context['result'] = forms.ResultFormset(request_data, prefix="result") context['contact_info'] = forms.ContactFormset(request_data, prefix="contact_info") context['transactions'] = aims.transaction.aims.none() context['blank_transaction_form'] = forms.TransactionForm() return context
def get_context_data(self, *args, **kwargs): context = super(CreateLocalTransaction, self).get_context_data() editor = hasattr(self.object, 'activity') and base_utils.check_editor_privilege(self.request.user, self.object.activity.reporting_organisation) context['editor'] = editor context['form'] = kwargs['form'] context['form_helper'] = forms.FormHelper() return context
def get_context_data(self, *args, **kwargs): context = super(EditTransaction, self).get_context_data() editor = base_utils.check_editor_privilege(self.request.user, self.object.activity.reporting_organisation) context['editor'] = editor context['form'] = kwargs['form'] context['form'].fields['remote_data'].initial = self.kwargs['pk'] context['form_helper'] = forms.FormHelper() return context
def get_context_data(self, *args, **kwargs): context = super(EditOrganisation, self).get_context_data() editor = base_utils.check_editor_privilege(self.request.user, self.object) #editor = True if self.request.user.is_staff or self.request.user.userorganisation.organisations.filter(pk=self.object.pk) else False context['editor'] = editor context['form'] = kwargs['form'] context['form'].fields['remote_data'].initial = self.kwargs['pk'] context['form'] = disable_fields_in_form(context['form'], editor) context['form_helper'] = forms.FormHelper() return context
def get_context_data(self, *args, **kwargs): default_language_code = 'en' default_language = aims.language.objects.get(code=default_language_code) def prepare_language_fields(formset): if not formset[0].fields['language'].initial: formset[0].fields['language'].initial = default_language_code if not formset[1].fields['language'].initial: formset[1].fields['language'].initial = 'my' placeholder = {'placeholder': _('Myanmar language translation')} if 'title' in formset[1].fields: formset[1].fields['title'].widget.attrs.update(placeholder) else: formset[1].fields['description'].widget.attrs.update(placeholder) return formset context = super(EditActivity, self).get_context_data() context['form'] = kwargs['form'] context['form'].fields['remote_data'].initial = self.object.pk context['form_helper'] = forms.FormHelper() context['sections'] = aims_sections("CPU") # disable form fields for those who do not have permission editor = base_utils.check_editor_privilege(self.request.user, self.object.reporting_organisation) context['editor'] = editor context['form '] = disable_fields_in_form(context['form'], editor) request_data = None if self.request.method == "POST": request_data = self.request.POST for title in self.object.title_set.filter(language=None): title.language = default_language title.save() for description in self.object.description_set.filter(language=None): description.language = default_language description.save() title = forms.TitleFormset(request_data, instance=self.object, queryset=self.object.title_set.all().order_by('language')) title = disable_fields_in_formset(title, editor) description = forms.DescriptionFormset(request_data, instance=self.object, queryset=self.object.description_set.filter(type=1).order_by('language')) description = disable_fields_in_formset(description, editor) objective = forms.ObjectivesFormset(request_data, instance=self.object, prefix="objectives", queryset=self.object.description_set.filter(type=2).order_by('language')) objective = disable_fields_in_formset(objective, editor) target_groups = forms.TargetGroupsFormset(request_data, instance=self.object, prefix="target_groups", queryset=self.object.description_set.filter(type=3).order_by('language')) target_groups = disable_fields_in_formset(target_groups, editor) context['title'] = prepare_language_fields(title) context['description'] = prepare_language_fields(description) context['objective'] = prepare_language_fields(objective) context['target_groups'] = prepare_language_fields(target_groups) context['activity_sector'] = disable_fields_in_formset(forms.ActivitySectorFormset(request_data, instance=self.object, queryset=self.object.sectors, prefix="sector"), editor) context['activity_national_sector'] = disable_fields_in_formset(forms.ActivityNationalSectorFormset(request_data, instance=self.object, queryset=self.object.national_sectors, prefix="national_sector"), editor) context['ministry'] = disable_fields_in_formset(forms.MinistryFormset(request_data, instance=self.object, prefix="ministry", queryset=self.object.activity_participating_organisation_set.filter(role='Accountable')), editor) context['participating_organisation'] = disable_fields_in_formset(forms.ParticipatingOrganisationFormset(request_data, instance=self.object, prefix="participating_organisation", queryset=aims.activity_participating_organisation.objects.exclude(role="Accountable")), editor) context['location'] = disable_fields_in_formset(forms.LocationFormset(request_data, instance=self.object, prefix="location", ), editor) # queryset=self.object.location_set.all()) context['result'] = disable_fields_in_formset(forms.ResultFormset(request_data, instance=self.object, prefix="result", queryset=self.object.result_set), editor) context['contact_info'] = disable_fields_in_formset(forms.ContactFormset(request_data, instance=self.object, prefix="contact_info", queryset=self.object.contact_info_set.all()), editor) transactions = aims.transaction.aims.with_drafts().filter(activity=self.object.pk).order_by('-transaction_date') for transaction in transactions: auto_id = 'transaction_' + str(transaction.pk) + '_%s' transaction.modal_form = disable_fields_in_form(forms.TransactionForm(instance=transaction, auto_id=auto_id), editor) context['transactions'] = transactions context['blank_transaction_form'] = disable_fields_in_form(forms.TransactionForm(), editor) return context