# Path: djangoblog/utils.py
# def save_user_avatar(url):
#     '''
#     保存用户头像
#     :param url:头像url
#     :return: 本地路径
#     '''
#     setting = get_blog_setting()
#     logger.info(url)
# 
#     try:
#         imgname = url.split('/')[-1]
#         if imgname:
#             path = r'{basedir}/avatar/{img}'.format(
#                 basedir=setting.resource_path, img=imgname)
#             if os.path.exists(path):
#                 os.remove(path)
#         rsp = requests.get(url, timeout=2)
#         if rsp.status_code == 200:
#             basepath = r'{basedir}/avatar/'.format(
#                 basedir=setting.resource_path)
#             if not os.path.exists(basepath):
#                 os.makedirs(basepath)
# 
#             imgextensions = ['.jpg', '.png', 'jpeg', '.gif']
#             isimage = len([i for i in imgextensions if url.endswith(i)]) > 0
#             ext = os.path.splitext(url)[1] if isimage else '.jpg'
#             savefilename = str(uuid.uuid4().hex) + ext
#             logger.info('保存用户头像:' + basepath + savefilename)
#             with open(basepath + savefilename, 'wb+') as file:
#                 file.write(rsp.content)
#             return 'https://resource.lylinux.net/avatar/' + savefilename
#     except Exception as e:
#         logger.error(e)
#         return url
# 
# Path: oauth/models.py
# class OAuthUser(models.Model):
#     author = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         verbose_name='用户',
#         blank=True,
#         null=True,
#         on_delete=models.CASCADE)
#     openid = models.CharField(max_length=50)
#     nikename = models.CharField(max_length=50, verbose_name='昵称')
#     token = models.CharField(max_length=150, null=True, blank=True)
#     picture = models.CharField(max_length=350, blank=True, null=True)
#     type = models.CharField(blank=False, null=False, max_length=50)
#     email = models.CharField(max_length=50, null=True, blank=True)
#     matedata = models.TextField(null=True, blank=True)
#     created_time = models.DateTimeField('创建时间', default=now)
#     last_mod_time = models.DateTimeField('修改时间', default=now)
# 
#     def __str__(self):
#         return self.nikename
# 
#     class Meta:
#         verbose_name = 'oauth用户'
#         verbose_name_plural = verbose_name
#         ordering = ['-created_time']


from django.core.management.base import BaseCommand
from djangoblog.utils import save_user_avatar
from oauth.models import OAuthUser



class Command(BaseCommand):
    help = 'sync user avatar'

    def handle(self, *args, **options):
        users = OAuthUser.objects.filter(picture__isnull=False).exclude(

# Path: djangoblog/utils.py
# def save_user_avatar(url):
#     '''
#     保存用户头像
#     :param url:头像url
#     :return: 本地路径
#     '''
#     setting = get_blog_setting()
#     logger.info(url)
# 
#     try:
#         imgname = url.split('/')[-1]
#         if imgname:
#             path = r'{basedir}/avatar/{img}'.format(
#                 basedir=setting.resource_path, img=imgname)
#             if os.path.exists(path):
#                 os.remove(path)
#         rsp = requests.get(url, timeout=2)
#         if rsp.status_code == 200:
#             basepath = r'{basedir}/avatar/'.format(
#                 basedir=setting.resource_path)
#             if not os.path.exists(basepath):
#                 os.makedirs(basepath)
# 
#             imgextensions = ['.jpg', '.png', 'jpeg', '.gif']
#             isimage = len([i for i in imgextensions if url.endswith(i)]) > 0
#             ext = os.path.splitext(url)[1] if isimage else '.jpg'
#             savefilename = str(uuid.uuid4().hex) + ext
#             logger.info('保存用户头像:' + basepath + savefilename)
#             with open(basepath + savefilename, 'wb+') as file:
#                 file.write(rsp.content)
#             return 'https://resource.lylinux.net/avatar/' + savefilename
#     except Exception as e:
#         logger.error(e)
#         return url
# 
# Path: oauth/models.py
# class OAuthUser(models.Model):
#     author = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         verbose_name='用户',
#         blank=True,
#         null=True,
#         on_delete=models.CASCADE)
#     openid = models.CharField(max_length=50)
#     nikename = models.CharField(max_length=50, verbose_name='昵称')
#     token = models.CharField(max_length=150, null=True, blank=True)
#     picture = models.CharField(max_length=350, blank=True, null=True)
#     type = models.CharField(blank=False, null=False, max_length=50)
#     email = models.CharField(max_length=50, null=True, blank=True)
#     matedata = models.TextField(null=True, blank=True)
#     created_time = models.DateTimeField('创建时间', default=now)
#     last_mod_time = models.DateTimeField('修改时间', default=now)
# 
#     def __str__(self):
#         return self.nikename
# 
#     class Meta:
#         verbose_name = 'oauth用户'
#         verbose_name_plural = verbose_name
#         ordering = ['-created_time']


# Path: blog/management/commands/sync_user_avatar.py
from django.core.management.base import BaseCommand
from djangoblog.utils import save_user_avatar
from oauth.models import OAuthUser




class Command(BaseCommand):
    help = 'sync user avatar'

    def handle(self, *args, **options):
        users = OAuthUser.objects.filter(picture__isnull=False).exclude(

            picture__istartswith='https://resource.lylinux.net').all()