Exemple #1
0
class Organization(models.Model):
    class Meta:
        ordering = ["slug"]

    slug = models.SlugField(primary_key=True)
    name = models.CharField(max_length=100)
    channel = models.CharField(max_length=50,
                               choices=get_channel_choices(),
                               default="kiwix")
    warehouse = models.CharField(max_length=50,
                                 choices=get_warehouse_choices(),
                                 default="kiwix")
    public_warehouse = models.CharField(
        max_length=50,
        choices=get_warehouse_choices(),
        default="download",
        verbose_name="Pub WH",
    )
    email = models.EmailField()
    units = models.IntegerField(null=True, blank=True, default=0)

    @property
    def is_limited(self):
        return self.units is not None

    @classmethod
    def get_or_none(cls, slug):
        try:
            return cls.objects.get(slug=slug)
        except cls.DoesNotExist:
            return None

    @classmethod
    def create_kiwix(cls):
        if cls.objects.filter(slug="kiwix").count():
            return cls.objects.get(slug="kiwix")
        return cls.objects.create(slug="kiwix",
                                  name="Kiwix",
                                  email="*****@*****.**",
                                  units=256000)

    def get_warehouse_details(self, use_public=False):
        success, warehouse = get_warehouse_from(
            self.public_warehouse if use_public else self.warehouse)
        if not success:
            raise SchedulerAPIError(warehouse)
        return warehouse

    def __str__(self):
        return self.name
Exemple #2
0
class ImageForm(SchedulerForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["config"].choices = Configuration.get_choices(
            self._client.organization
        )

    slug = forms.CharField(label="Image ID/slug")
    config = forms.ChoiceField(choices=[], label="Configuration")
    contact_email = forms.EmailField(label="Contact Email")
    warehouse = forms.ChoiceField(choices=get_warehouse_choices())
    channel = forms.ChoiceField(choices=get_channel_choices())
    private = forms.BooleanField(initial=True, required=True)
    active = forms.BooleanField(initial=True, required=False)

    @staticmethod
    def success_message(result):
        return _("Successfuly created Auto Image <em>%(img)s</em>") % {"img": result}

    def clean_slug(self):
        if not re.match(r"^[a-zA-Z0-9_.+-]+$", self.cleaned_data.get("slug")):
            raise forms.ValidationError(_("Prohibited Characters"), code="invalid")
        return self.cleaned_data.get("slug")

    def clean_config(self):
        config = Configuration.get_or_none(self.cleaned_data.get("config"))
        if config is None or config.organization != self._client.organization:
            raise forms.ValidationError(_("Not your configuration"), code="invalid")
        return config

    def save(self):
        if not self.is_valid():
            raise ValueError(_("%(class)s is not valid") % {"class": type(self)})

        success, autoimage_slug = add_autoimage(
            slug=self.cleaned_data.get("slug"),
            config=self.cleaned_data.get("config").to_dict(),
            contact_email=self.cleaned_data.get("contact_email"),
            periodicity="monthly",
            warehouse=self.cleaned_data.get("warehouse"),
            channel=self.cleaned_data.get("channel"),
            private=self.cleaned_data.get("private"),
        )
        if not success:
            raise SchedulerAPIError(autoimage_slug)
        return autoimage_slug