예제 #1
0
class Races(models.Model):
    """user race: {'futuri':1,'amadeus':2, 'united':3} """
    description = models.CharField(max_length=256, null=True, blank=True)

    def get_img_absolute_urls(self, host_url="", img=None):
        return get_img_absolute_urls(host_url, self.blazon)

    def get_blazon_path(self, filename):
        ext = filename.split('.')[-1]
        filename = "%s.%s" % (self.name, ext)
        directory = 'blazons'
        return os.path.join(directory, filename)

    blazon = imagekit_models.ProcessedImageField(upload_to=get_blazon_path,
                                                 null=False,
                                                 blank=True,
                                                 default=None,
                                                 processors=[
                                                     processors.ResizeToFit(
                                                         150, 150),
                                                 ],
                                                 options={'quality': 85})
    name = models.CharField(max_length=32)

    def __unicode__(self):
        return str(self.pk)
예제 #2
0
class Face(models.Model):
    emotion = models.CharField(max_length=16,
                               verbose_name=_('emotion'),
                               choices=EMOTION_CHOICES)
    race = models.ForeignKey(Races,
                             verbose_name=_('race'),
                             blank=True,
                             null=True)

    def get_img_absolute_urls(self, host_url="", img=None):
        return get_img_absolute_urls(host_url, self.face)

    def get_face_path(self, filename):
        race_name = self.race and self.race.name or 'all'
        ext = filename.split('.')[-1]
        filename = "%s-%s.%s" % (self.emotion, race_name, ext)
        directory = 'faces'
        return os.path.join(directory, filename)

    face = imagekit_models.ProcessedImageField(upload_to=get_face_path,
                                               null=False,
                                               blank=True,
                                               processors=[
                                                   processors.ResizeToFit(
                                                       150, 150),
                                               ],
                                               options={'quality': 85})

    def __unicode__(self):
        return u"%s_%s" % (self.emotion,
                           self.race.name if self.race else 'all')

    class Meta:
        verbose_name = _('face')
        verbose_name_plural = _('face')
예제 #3
0
class AppUser(AbstractUser):
    """
    User of an application, inherited from django user
    Attributes:
        account (Account): FK to account
        role (int): role of user: admin or usual user
        avatar (image): user avatar
    """
    ADMIN_ROLE = 'a'
    USER_ROLE = 'u'
    ROLES = ((ADMIN_ROLE, 'Admin'), (USER_ROLE, 'User'))

    account = models.ForeignKey(Account,
                                related_name='users',
                                blank=True,
                                null=True,
                                db_index=True)
    role = models.CharField(max_length=1, choices=ROLES, blank=True, null=True)

    # avatar
    avatar = imagekitmodels.ProcessedImageField(
        upload_to=upload_user_media_to,
        processors=[ResizeToFill(40, 40)],
        format='PNG',
        options={'quality': 100},
        null=True,
        blank=True)

    class Meta:
        verbose_name = 'User'
        verbose_name_plural = 'Users'

    def __str__(self):
        return self.username

    @property
    def full_name(self):
        return "{} {}".format(self.first_name.capitalize(),
                              self.last_name.capitalize())

    @property
    def is_merchant(self):
        """Is current user is merchant account
        """
        return self.account.type == Account.MERCHANT

    @property
    def is_iso(self):
        """Is current user is ISO account
        """
        return self.account.type == Account.ISO

    @property
    def is_iso_admin(self):
        """Is current user is admin for ISO
        """
        return self.is_iso and self.role == AppUser.ADMIN_ROLE
예제 #4
0
class AppUser(AbstractUser):
    """Custom user model.

    Attributes:
        avatar (file): user's avatar, cropeed to fill 300x300 px
        location (point): latest known GEO coordinates of the user
        location_updated (datetime): latest time user updated coordinates
        notifications (dict): settings for notifications to user
        first_name (str): first name
        last_name (str): last name
        username (str): username (not used)
        email (str): email (should be unique), this is our username field
        is_staff (bool): designates whether the user can log into
            this admin site
        is_active (bool): designates whether this user should be
            treated as active
        date_joined (datetime): when user joined
    """

    avatar = imagekitmodels.ProcessedImageField(
        upload_to=upload_user_media_to,
        processors=[ResizeToFill(300, 300)],
        format='PNG',
        options={'quality': 100},
        editable=False,
        null=True,
        blank=False)

    location = gis_models.PointField(default='POINT(0.0 0.0)',
                                     blank=True,
                                     srid=4326,
                                     editable=False)

    location_updated = models.DateTimeField(null=True,
                                            blank=True,
                                            editable=False)

    notifications = HStoreField(null=True)

    # so authentication happens by email instead of username
    # and username becomes sort of nick
    USERNAME_FIELD = 'email'

    # Make sure to exclude email from required fields if authentication
    # is done by email
    REQUIRED_FIELDS = ['username']

    def __str__(self):
        return self.username

    class Meta:
        verbose_name = 'User'
        verbose_name_plural = 'Users'
예제 #5
0
class ImgModels(models.Model):
    DIRECTORY = ''

    def get_image_path(self, filename):
        return os.path.join(os.path.join('media', self.DIRECTORY), filename)

    class Meta:
        abstract = True

    name = models.CharField(
        max_length=16, unique=True)
    img = imagekit_models.ProcessedImageField(
        upload_to=get_image_path,
        options={"quality": 85}
    )

    def __unicode__(self):
        return self.name
예제 #6
0
class CustomUser(AbstractUser):

    first_name = models.CharField(_('first name'),
                                  max_length=30,
                                  blank=True,
                                  null=True)
    last_name = models.CharField(_('last name'),
                                 max_length=30,
                                 blank=True,
                                 null=True)
    email = models.EmailField(
        verbose_name=_('email address'),
        unique=True,
        blank=True,
        error_messages={
            'unique': _("A user with this email already exists."),
        },
        help_text=_('Email is used as internal username'),
    )
    username = models.CharField(
        verbose_name=_('Username'),
        blank=True,
        null=True,
        help_text=_('Display username'),
        max_length=255,
    )
    avatar = imagekitmodels.ProcessedImageField(
        upload_to=upload_user_media_to,
        processors=[ResizeToFill(300, 300)],
        format='JPEG',
        options={'quality': 100},
        editable=False,
        null=True,
        blank=True)
    contact = models.CharField(validators=[phone_regex],
                               max_length=17,
                               null=True,
                               blank=True,
                               verbose_name=_('Contact'))
    user_role = models.CharField(max_length=100,
                                 verbose_name=_('Role of User'),
                                 blank=True,
                                 null=True)
    status = models.CharField(
        max_length=100,
        verbose_name=_('User Status'),
        help_text=_('Any User is Blocked/Unblocked by Buyer'),
        blank=True,
        null=True)
    warehouse_exchange_id = models.CharField(
        max_length=100,
        verbose_name=_('ID of Warehouse Exchange'),
        blank=True,
        null=True)
    last_login = models.DateTimeField(blank=True, null=True)
    created = models.DateTimeField(auto_now_add=True)

    objects = CustomUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    class Meta:
        verbose_name = 'CustomUser'
        verbose_name_plural = 'Users'

    def __str__(self):
        if self.first_name:
            return '{self.first_name} {self.last_name}'.format(
                self=self).strip()
        return self.email
예제 #7
0
class MessageBase(models.Model):

    date = models.DateTimeField(auto_now_add=True,
                                null=True,
                                blank=True,
                                editable=False,
                                verbose_name=u"дата добавления")

    text = models.TextField(max_length=1024,
                            null=False,
                            blank=True,
                            verbose_name=u"сообщение")

    user = models.ForeignKey(User, verbose_name=_('user'))

    place = models.ForeignKey(Place, verbose_name=u"место")

    def get_photo_path(self, filename):
        ext = filename.split('.')[-1]
        filename = "%s.%s" % (uuid.uuid4(), ext)
        directory = time.strftime('photos/%Y/%m/%d')
        return os.path.join(directory, filename)

    photo = imagekit_models.ProcessedImageField(upload_to=get_photo_path,
                                                verbose_name=u"фотография",
                                                null=False,
                                                blank=True,
                                                processors=[
                                                    processors.ResizeToFit(
                                                        1350, 1200),
                                                ],
                                                options={'quality': 85})

    reduced_photo = imagekit_models.ImageSpecField([
        processors.ResizeToFit(435),
    ],
                                                   source='photo',
                                                   options={'quality': 85})

    thumbnail = imagekit_models.ImageSpecField([
        processors.ResizeToFill(90, 90),
    ],
                                               source='photo',
                                               options={'quality': 85})

    def get_photo_absolute_urls_and_size(self, photo_host_url=""):
        urls = self.get_photo_absolute_urls(photo_host_url)
        urls_and_size = {}
        if urls:
            for (name, url) in urls.items():
                photo = \
                    name=='full' and self.photo \
                    or name=='reduced' and self.reduced_photo \
                    or self.thumbnail
                urls_and_size[name] = {
                    'url': url,
                    'width': photo.width,
                    'height': photo.height
                }
        return urls_and_size

    def get_photo_absolute_urls(self, photo_host_url=""):
        # photo_host_url = photo_host_url.rstrip('/')
        photo_host_url = photo_host_url + 'media/'
        if self.photo:
            return dict(full=photo_host_url + self.photo.url,
                        reduced=photo_host_url + self.reduced_photo.url,
                        thumbnail=photo_host_url + self.thumbnail.url)
        else:
            return None

    categories = models.ManyToManyField(Category, null=True, blank=True)

    class Meta:
        abstract = True
        verbose_name = _('message')
        verbose_name_plural = _('messages')

    def __unicode__(self):
        return u"%s" % self.text

    def save(self, force_insert=False, force_update=False, using=None):
        if self.text is None:
            self.text = ''
        self.text = self.text.strip()
        models.Model.save(self,
                          force_insert=force_insert,
                          force_update=force_update,
                          using=using)

    def is_photo(self):
        return self.photo and True or False

    def get_string_date(self):
        return get_string_date(self.date)