import os
from urllib import parse
from django.db import models
from django.contrib.auth.models import AbstractUser
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFill
from imagekit.exceptions import MissingSource


def avatar_path(instance, filename):
    return os.path.join('avatar', instance.username, filename)


class User(AbstractUser):
    ROLES = (
        ('admin', 'admin'),
        ('editor', 'editor')
    )
    phone = models.CharField(max_length=11, null=True, blank=True, default='', verbose_name='电话')
    department = models.CharField(max_length=20, null=True, blank=True, default='', verbose_name='部门')
    position = models.CharField(max_length=50, null=True, blank=True, default='', verbose_name='职位')
    introduction = models.TextField(verbose_name='简介')
    role = models.CharField(max_length=20, default='admin', choices=ROLES, verbose_name='角色')
    avatar = models.ImageField(upload_to=avatar_path,
                               default='https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', null=True, blank=True)
    avatar_thumbnail = ImageSpecField(source='avatar',
                                      processors=[ResizeToFill(100, 100)],
                                      format='JPEG',
                                      options={'quality': 100})

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = verbose_name

    def avatar_url(self):
        try:
            avatar_thumbnail_url = self.avatar_thumbnail.url
        except MissingSource:
            avatar_thumbnail_url = ''
        return avatar_thumbnail_url

    def get_image_url(self):
        return parse.unquote(self.avatar.url)

    def __str__(self):
        return self.username
