コード例 #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
コード例 #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
コード例 #3
0
class UserForm(SchedulerForm):
    username = forms.CharField()
    role = forms.ChoiceField(choices=ROLES.items())
    channel = forms.ChoiceField(choices=get_channel_choices())
    email = forms.EmailField()
    password = forms.CharField()

    @staticmethod
    def success_message(result):
        return "Successfuly created User <em>{user}</em>".format(user=result)

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

    def clean_email(self):
        if not re.match(
                r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$",
                self.cleaned_data.get("email"),
        ):
            raise forms.ValidationError("Invalid format", code="invalid")
        return self.cleaned_data.get("email")

    def save(self):
        if not self.is_valid():
            raise ValueError("{cls} is not valid".format(cls=type(self)))

        success, user_id = add_user(
            username=self.cleaned_data.get("username"),
            email=self.cleaned_data.get("email"),
            password=self.cleaned_data.get("password"),
            role=self.cleaned_data.get("role"),
            channel=self.cleaned_data.get("channel"),
            is_admin=self.cleaned_data.get("is_admin"),
        )

        if not success:
            raise SchedulerAPIError(user_id)

        return user_id