Ejemplo n.º 1
0
 def get_conditions(self, measure):
     tx = WorkBasket.get_current_transaction(self.request)
     return (
         measure.conditions.with_reference_price_string().approved_up_to_transaction(
             tx,
         )
     )
Ejemplo n.º 2
0
 def get_queryset(self):
     tx = WorkBasket.get_current_transaction(self.request)
     return (
         models.Footnote.objects.approved_up_to_transaction(tx)
         .select_related("footnote_type")
         .prefetch_related("descriptions")
     )
Ejemplo n.º 3
0
    def save(self, commit=True):
        instance = super(CertificateCreateForm, self).save(commit=False)

        self.cleaned_data[
            "certificate_description"] = models.CertificateDescription(
                description=self.cleaned_data["description"],
                validity_start=self.cleaned_data["valid_between"].lower,
            )

        if not instance.sid:
            current_transaction = WorkBasket.get_current_transaction(
                self.request)
            # Filter certificate by type and find the highest sid, using regex to ignore legacy, non-numeric identifiers
            instance.sid = get_next_id(
                models.Certificate.objects.filter(
                    sid__regex=r"^[0-9]*$",
                    certificate_type__sid=instance.certificate_type.sid,
                ).approved_up_to_transaction(current_transaction),
                instance._meta.get_field("sid"),
                max_len=3,
            )

        if commit:
            instance.save()
        return instance
Ejemplo n.º 4
0
    def __call__(self, request):
        from workbaskets.models import WorkBasket

        with override_current_transaction(
                WorkBasket.get_current_transaction(request), ):
            response = self.get_response(request)
        # No post-view processing required.
        return response
Ejemplo n.º 5
0
    def get_footnotes(self, measure):
        tx = WorkBasket.get_current_transaction(self.request)
        associations = FootnoteAssociationMeasure.objects.approved_up_to_transaction(
            tx,
        ).filter(
            footnoted_measure__sid=measure.sid,
        )

        return [a.associated_footnote for a in associations]
Ejemplo n.º 6
0
def test_current_transaction_returns_last_approved_transaction(
    session_request,
    approved_transaction,
):
    """Check that when no workbasket is saved on the request session
    get_current_transaction returns the latest approved transaction instead."""
    current = WorkBasket.get_current_transaction(session_request)

    assert current == approved_transaction
Ejemplo n.º 7
0
    def get_form(self, form_class=None):
        form = super().get_form(form_class=form_class)
        tx = WorkBasket.get_current_transaction(self.request)

        if hasattr(form, "field"):
            for field in form.fields.values():
                if hasattr(field, "queryset"):
                    field.queryset = field.queryset.approved_up_to_transaction(tx)

        return form
Ejemplo n.º 8
0
    def get_queryset(self):
        """
        API endpoint for autocomplete as used by the MeasureCreationWizard.

        Only return valid names that are products (suffix=80)
        """
        tx = WorkBasket.get_current_transaction(self.request)
        return (GoodsNomenclature.objects.approved_up_to_transaction(
            tx, ).prefetch_related("descriptions").as_at(
                date.today()).filter(suffix=80))
Ejemplo n.º 9
0
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request", None)
        super().__init__(*args, **kwargs)

        tx = WorkBasket.get_current_transaction(self.request)

        if not hasattr(self.instance, "duty_sentence"):
            raise AttributeError(
                "Measure instance is missing `duty_sentence` attribute. Try calling `with_duty_sentence` queryset method",
            )

        self.initial["duty_sentence"] = self.instance.duty_sentence
        self.request.session[
            f"instance_duty_sentence_{self.instance.sid}"] = self.instance.duty_sentence

        if self.instance.geographical_area.is_all_countries():
            self.initial["geo_area"] = GeoAreaType.ERGA_OMNES.value

        elif self.instance.geographical_area.is_group():
            self.initial["geo_area"] = GeoAreaType.GROUP.value

        else:
            self.initial["geo_area"] = GeoAreaType.COUNTRY.value

        # If no footnote keys are stored in the session for a measure,
        # store all the pks of a measure's footnotes on the session, using the measure sid as key
        if f"instance_footnotes_{self.instance.sid}" not in self.request.session.keys(
        ):
            tx = WorkBasket.get_current_transaction(self.request)
            associations = (models.FootnoteAssociationMeasure.objects.
                            approved_up_to_transaction(tx, ).filter(
                                footnoted_measure=self.instance, ))
            self.request.session[f"instance_footnotes_{self.instance.sid}"] = [
                a.associated_footnote.pk for a in associations
            ]

        nested_forms_initial = {**self.initial}
        nested_forms_initial[
            "geographical_area"] = self.instance.geographical_area
        kwargs.pop("initial")
        self.bind_nested_forms(*args, initial=nested_forms_initial, **kwargs)
Ejemplo n.º 10
0
    def clean_sid(self):
        sid = self.cleaned_data["sid"]
        if sid:
            certificate_type = self.cleaned_data["certificate_type"]
            tx = WorkBasket.get_current_transaction(self.request)
            if (models.Certificate.objects.approved_up_to_transaction(
                    tx).filter(sid=sid,
                               certificate_type=certificate_type).exists()):
                raise ValidationError(
                    f"Certificate with sid {sid} and type {certificate_type} already exists.",
                )

        return sid
Ejemplo n.º 11
0
    def save(self, commit=True):
        instance = super().save(commit=False)

        tx = WorkBasket.get_current_transaction(self.request)

        highest_sid = (models.AdditionalCode.
                       objects.approved_up_to_transaction(tx).aggregate(
                           Max("sid"), )["sid__max"]) or 0
        instance.sid = highest_sid + 1

        if commit:
            instance.save(commit)
        return instance
Ejemplo n.º 12
0
 def _get_next_part_value(self, partial_regulation_id):
     """Get the next available part value that can be appended to a partial
     regulation_id (see
     RegulationCreateForm._make_partial_regulation_id())."""
     tx = WorkBasket.get_current_transaction(self.request)
     last_matching_regulation = (Regulation.objects.filter(
         regulation_id__startswith=partial_regulation_id,
         role_type=FIXED_ROLE_TYPE,
     ).approved_up_to_transaction(tx).order_by("-regulation_id").first())
     if last_matching_regulation:
         highest_part_value = last_matching_regulation.regulation_id[-1]
         alphanum = string.digits + string.ascii_uppercase
         return alphanum[int(highest_part_value, 36) + 1]
     return 0
Ejemplo n.º 13
0
    def get_form(self, step=None, data=None, files=None):
        form = super().get_form(step, data, files)
        tx = WorkBasket.get_current_transaction(self.request)
        forms = [form]
        if hasattr(form, "forms"):
            forms = form.forms
        for f in forms:
            if hasattr(f, "fields"):
                for field in f.fields.values():
                    if hasattr(field, "queryset"):
                        field.queryset = field.queryset.approved_up_to_transaction(tx)

        form.is_valid()
        if hasattr(form, "cleaned_data"):
            form.initial = form.cleaned_data

        return form
Ejemplo n.º 14
0
 def get_queryset(self):
     tx = WorkBasket.get_current_transaction(self.request)
     return MeasureType.objects.approved_up_to_transaction(tx).order_by(
         "description", )
Ejemplo n.º 15
0
 def get_queryset(self):
     tx = WorkBasket.get_current_transaction(self.request)
     return Regulation.objects.approved_up_to_transaction(tx)
Ejemplo n.º 16
0
 def get_queryset(self):
     tx = WorkBasket.get_current_transaction(self.request)
     return AdditionalCodeDescription.objects.approved_up_to_transaction(tx)
Ejemplo n.º 17
0
 def get_queryset(self):
     tx = WorkBasket.get_current_transaction(self.request)
     return AdditionalCode.objects.approved_up_to_transaction(
         tx).select_related("type", )
Ejemplo n.º 18
0
 def get_queryset(self):
     tx = WorkBasket.get_current_transaction(self.request)
     return GeographicalAreaDescription.objects.approved_up_to_transaction(
         tx)
Ejemplo n.º 19
0
 def get_queryset(self):
     tx = WorkBasket.get_current_transaction(self.request)
     return models.QuotaOrderNumber.objects.approved_up_to_transaction(tx)
Ejemplo n.º 20
0
 def get_queryset(self):
     tx = WorkBasket.get_current_transaction(self.request)
     return models.Certificate.objects.approved_up_to_transaction(
         tx).select_related("certificate_type", )
Ejemplo n.º 21
0
    def get_queryset(self):
        tx = WorkBasket.get_current_transaction(self.request)

        return Measure.objects.with_duty_sentence().approved_up_to_transaction(
            tx)
Ejemplo n.º 22
0
 def get_queryset(self):
     tx = WorkBasket.get_current_transaction(self.request)
     return models.CertificateDescription.objects.approved_up_to_transaction(
         tx)