Esempio n. 1
0
class UserSocialAuth(models.Model):
    """Social Auth association model"""
    user = models.ForeignKey(User, related_name='social_auth')
    provider = models.CharField(max_length=32)
    uid = models.CharField(max_length=255)
    extra_data = JSONField(blank=True)

    class Meta:
        """Meta data"""
        unique_together = ('provider', 'uid')

    def __unicode__(self):
        """Return associated user unicode representation"""
        return unicode(self.user)

    def expiration_delta(self):
        """Return saved session expiration seconds if any. Is retuned in
        the form of a timedelta data type. None is returned if there's no
        value stored or it's malformed.
        """
        if self.extra_data:
            name = getattr(settings, 'SOCIAL_AUTH_EXPIRATION', 'expires')
            try:
                return timedelta(seconds=int(self.extra_data.get(name)))
            except (ValueError, TypeError):
                pass
        return None
class UserSocialAuth(models.Model, UserSocialAuthMixin):
    """Social Auth association model"""
    user = models.ForeignKey(USER_MODEL, related_name='social_auth')
    provider = models.CharField(max_length=32)
    uid = models.CharField(max_length=255)
    extra_data = JSONField(default='{}')

    class Meta:
        """Meta data"""
        unique_together = ('provider', 'uid')
        app_label = 'social_auth'

    @classmethod
    def get_social_auth(cls, provider, uid):
        try:
            return cls.objects.select_related('user').get(provider=provider,
                                                          uid=uid)
        except UserSocialAuth.DoesNotExist:
            return None

    @classmethod
    def username_max_length(cls):
        field = UserSocialAuth.user_model()._meta.get_field('username')
        return field.max_length

    @classmethod
    def user_model(cls):
        return UserSocialAuth._meta.get_field('user').rel.to
Esempio n. 3
0
class UserSocialAuth(models.Model, UserSocialAuthMixin):
    """Social Auth association model"""
    User = UserModel
    user = models.ForeignKey(UserModel, related_name='social_auth')
    provider = models.CharField(max_length=32)
    uid = models.CharField(max_length=255)
    extra_data = JSONField(default='{}')

    class Meta:
        """Meta data"""
        unique_together = ('provider', 'uid')
        app_label = 'social_auth'

    @classmethod
    def create_user(cls, *args, **kwargs):
        return cls.User.objects.create_user(*args, **kwargs)

    @classmethod
    def get_social_auth(cls, provider, uid):
        try:
            return cls.objects.select_related('user').get(provider=provider,
                                                          uid=uid)
        except UserSocialAuth.DoesNotExist:
            return None

    @classmethod
    def username_max_length(cls):
        return cls.User._meta.get_field('username').max_length
Esempio n. 4
0
class Repository(models.Model):
    owner = CharField(max_length=100)
    name = CharField(max_length=100)
    slug = SlugField(max_length=201)
    host_slug = SlugField(max_length=302, unique=True)
    language = CharField(max_length=100, null=True)
    html_url = URLField(null=True, max_length=400)
    homepage = URLField(null=True, max_length=400)
    watchers = PositiveIntegerField(null=True)
    created_at = DateTimeField(null=True)
    pushed_at = DateTimeField(null=True)
    description = TextField(null=True)
    extra_data = JSONField(null=True)
    last_modified = DateTimeField(auto_now=True)
    scm = CharField(max_length=100, choices=SCM_CHOICES, null=True)
    host = CharField(max_length=100, choices=HOST_CHOICES)
    private = BooleanField(default=False)

    class Meta:
        unique_together = ("owner", "name", "host")
        ordering = ['-watchers']

    def save(self, *args, **kwargs):
        self.slug = self.owner.lower() + '/' + self.name.lower()
        self.host_slug = self.host + '/' + self.slug
        if self.html_url == None or self.html_url == '':
            if self.host == 'bitbucket':
                self.html_url = 'https://bitbucket.org/%s/%s' % (self.owner,
                                                                 self.name)
            if self.host == 'github':
                self.html_url = 'https://github.com/%s/%s' % (self.owner,
                                                              self.name)

        super(Repository, self).save(*args, **kwargs)
Esempio n. 5
0
class UserSocialAuth(models.Model):
    """Social Auth association model"""
    user = models.ForeignKey(User, related_name='social_auth')
    provider = models.CharField(max_length=32)
    uid = models.CharField(max_length=255)
    extra_data = JSONField(default='{}')

    class Meta:
        """Meta data"""
        unique_together = ('provider', 'uid')

    def __unicode__(self):
        """Return associated user unicode representation"""
        return u'%s - %s' % (unicode(self.user), self.provider.title())

    @property
    def tokens(self):
        """Return access_token stored in extra_data or None"""
        # Make import here to avoid recursive imports :-/
        from social_auth.backends import get_backends
        backend = get_backends().get(self.provider)
        if backend:
            return backend.AUTH_BACKEND.tokens(self)
        else:
            return {}

    def expiration_datetime(self):
        """Return provider session live seconds. Returns a timedelta ready to
        use with session.set_expiry().

        If provider returns a timestamp instead of session seconds to live, the
        timedelta is inferred from current time (using UTC timezone). None is
        returned if there's no value stored or it's invalid.
        """
        name = setting('SOCIAL_AUTH_EXPIRATION', 'expires')
        if self.extra_data and name in self.extra_data:
            try:
                expires = int(self.extra_data.get(name))
            except (ValueError, TypeError):
                return None

            now = datetime.now()
            now_timestamp = time.mktime(now.timetuple())

            # Detect if expires is a timestamp
            if expires > now_timestamp:  # expires is a datetime
                return datetime.utcfromtimestamp(expires) \
                               .replace(tzinfo=utc) - \
                       now.replace(tzinfo=utc)
            else:  # expires is a timedelta
                return timedelta(seconds=expires)
Esempio n. 6
0
class UserSocialAuth(models.Model):
    """Social Auth association model"""
    user = models.ForeignKey(User, related_name='social_auth')
    provider = models.CharField(max_length=32)
    uid = models.CharField(max_length=255)
    extra_data = JSONField(default='{}')

    class Meta:
        """Meta data"""
        unique_together = ('provider', 'uid')

    def __unicode__(self):
        """Return associated user unicode representation"""
        return u'%s - %s' % (unicode(self.user), self.provider.title())

    @property
    def tokens(self):
        """Return access_token stored in extra_data or None"""
        # Make import here to avoid recursive imports :-/
        from social_auth.backends import get_backends
        backend = get_backends().get(self.provider)
        if backend:
            return backend.AUTH_BACKEND.tokens(self)
        else:
            return {}

    def expiration_delta(self):
        """Return saved session expiration seconds if any. Is returned in
        the form of a timedelta data type. None is returned if there's no
        value stored or it's malformed.
        """
        if self.extra_data:
            name = setting('SOCIAL_AUTH_EXPIRATION', 'expires')
            try:
                return timedelta(seconds=int(self.extra_data.get(name)))
            except (ValueError, TypeError):
                pass
        return None
Esempio n. 7
0
class UserSocialAuth(models.Model, UserSocialAuthMixin):
    """Social Auth association model"""
    user = models.ForeignKey(USER_MODEL, related_name='social_auth')
    provider = models.CharField(max_length=32)
    uid = models.CharField(max_length=UID_LENGTH)
    extra_data = JSONField(default='{}')
    created = models.DateTimeField(auto_now_add=True, blank=True, null=True)

    class Meta:
        """Meta data"""
        unique_together = (('provider', 'uid'), ('provider', 'user'))
        app_label = 'social_auth'

    @classmethod
    def get_social_auth(cls, provider, uid):
        try:
            return cls.objects.select_related('user').get(provider=provider,
                                                          uid=uid)
        except UserSocialAuth.DoesNotExist:
            return None

    @classmethod
    def username_max_length(cls):
        return cls._field_length('USERNAME_FIELD', 'username')

    @classmethod
    def email_max_length(cls):
        return cls._field_length('EMAIL_FIELD', 'email')

    @classmethod
    def _field_length(self, setting_name, default_name):
        model = UserSocialAuth.user_model()
        field_name = getattr(model, setting_name, default_name)
        return model._meta.get_field(field_name).max_length

    @classmethod
    def user_model(cls):
        return get_model(*USER_MODEL.split('.'))
Esempio n. 8
0
class RepositoryUser(models.Model):
    login = CharField(max_length=100, db_index=True)
    name = CharField(max_length=100, null=True)
    slug = SlugField(max_length=201, unique=True)
    email = EmailField(max_length=254, null=True)
    blog = URLField(null=True)
    followers = PositiveIntegerField(null=True)
    following = PositiveIntegerField(null=True)
    public_repos = PositiveIntegerField(null=True)
    created_at = DateTimeField(null=True)
    extra_data = JSONField(null=True)
    last_modified = DateTimeField(auto_now=True)
    repositories = models.ManyToManyField(
        Repository, through='RepositoryUserRepositoryLink')
    starred = PositiveIntegerField(null=True)
    watched = PositiveIntegerField(null=True)
    host = CharField(max_length=100, choices=HOST_CHOICES, db_index=True)

    class Meta:
        unique_together = ("login", "host")

    def save(self, *args, **kwargs):
        self.slug = self.host + '/' + self.login.lower()
        super(RepositoryUser, self).save(*args, **kwargs)
Esempio n. 9
0
class GaProfile(m.Model):
    ''' A google analytics profile. '''
    account_id = m.IntegerField()
    profile_id = m.IntegerField()
    user = m.ForeignKey(User)
    info = JSONField()