Beispiel #1
0
class PrimordialModel(BaseModel):
    """Base model for CommonModel and CommonModelNameNotUnique."""

    class Meta:
        abstract = True

    description = fields.TruncatingCharField(
        max_length=255, blank=True, default='')
    active = models.BooleanField(default=True, db_index=True)
Beispiel #2
0
class Notification(PrimordialModel):
    class Meta:
        ordering = ('-id',)

    owner = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        related_name='notifications',
        db_index=True,
        editable=False,
        on_delete=models.CASCADE,
    )
    source = models.CharField(
        max_length=20,
        verbose_name="Source",
        editable=False
    )
    github_branch = models.CharField(
        max_length=256,
        verbose_name="GitHub Branch",
        blank=True,
        editable=False
    )
    travis_build_url = models.CharField(
        max_length=256,
        blank=True
    )
    travis_status = models.CharField(
        max_length=256,
        blank=True
    )
    commit = models.CharField(
        max_length=256,
        blank=True
    )
    committed_at = models.DateTimeField(
        auto_now=False,
        null=True
    )
    commit_message = fields.TruncatingCharField(
        max_length=256,
        blank=True,
        default=''
    )
    repository = models.ForeignKey(
        'Repository',
        related_name='notifications',
        editable=False,
        on_delete=models.CASCADE,
    )
    import_task = models.ForeignKey(
        ImportTask,
        related_name='notifications',
        verbose_name='Tasks',
        editable=False,
        on_delete=models.CASCADE,
    )
Beispiel #3
0
class ContentType(BaseModel):
    """A model that represents content type (e.g. role, module, etc.)."""
    name = models.CharField(max_length=512, unique=True, db_index=True,
                            choices=constants.ContentType.choices())
    description = fields.TruncatingCharField(
        max_length=255, blank=True, default='')

    @classmethod
    def get(cls, content_type):
        if isinstance(content_type, constants.ContentType):
            content_type = content_type.value
        return cls.objects.get(name=content_type)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('api:content_type_detail', args=(self.pk,))
Beispiel #4
0
class Repository(BaseModel):
    class Meta:
        unique_together = [
            ('provider_namespace', 'name'),
            ('provider_namespace', 'original_name'),
        ]
        ordering = ('provider_namespace', 'name')

    # Foreign keys
    owners = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        related_name='repositories'
    )
    provider_namespace = models.ForeignKey(
        ProviderNamespace,
        related_name='repositories',
        on_delete=models.CASCADE,
    )
    readme = models.ForeignKey(
        'Readme',
        null=True,
        on_delete=models.SET_NULL,
        related_name='+',
    )

    # Fields
    name = models.CharField(max_length=256)
    original_name = models.CharField(max_length=256, null=False)
    format = models.CharField(max_length=16, null=True,
                              choices=constants.RepositoryFormat.choices())
    description = fields.TruncatingCharField(
        max_length=255, blank=True, default='')
    import_branch = models.CharField(max_length=256, null=True)
    is_enabled = models.BooleanField(default=False)

    # Repository attributes
    commit = models.CharField(max_length=256, blank=True, default='')
    commit_message = fields.TruncatingCharField(
        max_length=256, blank=True, default='')
    commit_url = models.CharField(max_length=256, blank=True, default='')
    commit_created = models.DateTimeField(
        null=True, verbose_name="Last Commit DateTime")
    stargazers_count = models.IntegerField(default=0)
    watchers_count = models.IntegerField(default=0)
    forks_count = models.IntegerField(default=0)
    open_issues_count = models.IntegerField(default=0)
    travis_status_url = models.CharField(
        max_length=256,
        blank=True,
        default='',
        verbose_name="Travis Build Status"
    )
    travis_build_url = models.CharField(
        max_length=256,
        blank=True,
        default='',
        verbose_name="Travis Build URL"
    )
    issue_tracker_url = models.CharField(
        max_length=256,
        blank=True,
        null=True,
        verbose_name="Issue Tracker URL",
    )
    download_count = models.IntegerField(
        default=0
    )
    deprecated = models.BooleanField(
        default=False,
    )
    community_score = models.FloatField(
        null=True
    )
    quality_score = models.FloatField(
        null=True,
        validators=[MinValueValidator(0.0), MaxValueValidator(5.0)],
    )
    quality_score_date = models.DateTimeField(
        null=True,
        verbose_name="DateTime last scored",
    )

    is_new = models.BooleanField(
        default=False,
    )

    @property
    def clone_url(self):
        return "https://github.com/{user}/{repo}.git".format(
            user=self.provider_namespace.name,
            repo=self.original_name
        )

    @property
    def github_user(self):
        return self.provider_namespace.name

    @property
    def github_repo(self):
        return self.original_name

    @property
    def content_counts(self):
        return Content.objects \
            .filter(repository=self.pk) \
            .values('content_type__name') \
            .annotate(count=models.Count('content_type__name')) \
            .order_by('content_type__name')

    def get_absolute_url(self):
        return reverse('api:repository_detail', args=(self.pk,))

    def get_download_url(self, ref=None):
        download_url = self.provider_namespace.provider.download_url

        if ref is None:
            last_version = self.last_version()
            if last_version:
                ref = last_version.tag
            else:
                ref = self.import_branch

        return download_url.format(
            username=self.provider_namespace.name,
            repository=self.original_name,
            ref=ref,
        )

    def all_versions(self):
        return sorted(self.versions.filter(version__isnull=False).all(),
                      key=operator.attrgetter('version'),
                      reverse=True)

    def last_version(self):
        versions = self.all_versions()
        if versions:
            return versions[0]
        return None