def test_image(self): import requests rsp = requests.get( 'https://www.python.org/static/img/[email protected]') imagepath = os.path.join(settings.BASE_DIR, 'python.png') with open(imagepath, 'wb') as file: file.write(rsp.content) rsp = self.client.post('/upload') self.assertEqual(rsp.status_code, 403) sign = get_md5(get_md5(settings.SECRET_KEY)) with open(imagepath, 'rb') as file: imgfile = SimpleUploadedFile('python.png', file.read(), content_type='image/jpg') form_data = {'python.png': imgfile} rsp = self.client.post('/upload?sign=' + sign, form_data, follow=True) self.assertEqual(rsp.status_code, 200) from DjangoBlog.utils import save_user_avatar, send_email send_email(['*****@*****.**'], 'testTitle', 'testContent') save_user_avatar( 'https://www.python.org/static/img/[email protected]') """
def oauth_user_login_callback(sender, **kwargs): id = kwargs['id'] oauthuser = OAuthUser.objects.get(id=id) setting = get_blog_setting() if oauthuser.picture and not oauthuser.picture.startswith(setting.resource_path): from DjangoBlog.utils import save_user_avatar oauthuser.picture = save_user_avatar(oauthuser.picture) oauthuser.save()
def authorize(request): manager = None type = request.GET.get('type', None) if not type: return HttpResponseRedirect('/') manager = get_manager_by_type(type) if not manager: return HttpResponseRedirect('/') code = request.GET.get('code', None) rsp = manager.get_access_token_by_code(code) nexturl = request.GET.get('next_url', None) if not nexturl: nexturl = '/' if not rsp: return HttpResponseRedirect(manager.get_authorization_url(nexturl)) user = manager.get_oauth_userinfo() logger.info('user:'******'%y%m%d%I%M%S') try: user = OAuthUser.objects.get(type=type, openid=user.openid) except ObjectDoesNotExist: pass # facebook的token过长 if type == 'facebook': user.token = '' email = user.email if email: author = None try: author = get_user_model().objects.get(id=user.author_id) except ObjectDoesNotExist: pass if not author: result = get_user_model().objects.get_or_create( email=user.email) author = result[0] if result[1]: author.username = user.nikename author.save() user.author = author user.save() login(request, author) return HttpResponseRedirect(nexturl) if not email: user.save() url = reverse('oauth:require_email', kwargs={'oauthid': user.id}) return HttpResponseRedirect(url) else: return HttpResponseRedirect(nexturl)
def oauth_user_login_signal_handler(sender, **kwargs): id = kwargs['id'] oauthuser = OAuthUser.objects.get(id=id) site = Site.objects.get_current().domain if oauthuser.picture and not oauthuser.picture.find(site) >= 0: from DjangoBlog.utils import save_user_avatar oauthuser.picture = save_user_avatar(oauthuser.picture) oauthuser.save() delete_view_cache(oauthuser.author.username) cache.clear()
def handle(self, *args, **options): users = OAuthUser.objects.filter(picture__isnull=False).exclude( picture__istartswith='https://resource.lylinux.net').all() self.stdout.write('开始同步{count}个用户头像'.format(count=len(users))) for u in users: self.stdout.write('开始同步:{id}'.format(id=u.nikename)) url = u.picture url = save_user_avatar(url) if url: self.stdout.write('结束同步:{id}.url:{url}'.format(id=u.nikename, url=url)) u.picture = url u.save() self.stdout.write('结束同步')
def oauth_user_login_signal_handler(sender, **kwargs): id = kwargs['id'] oauthuser = OAuthUser.objects.get(id=id) site = get_current_site().domain if oauthuser.picture and not oauthuser.picture.find(site) >= 0: from DjangoBlog.utils import save_user_avatar oauthuser.picture = save_user_avatar(oauthuser.picture) oauthuser.save() delete_sidebar_cache( oauthuser.author.username ) #用户认证通过后,发送信号过来,信号里删除用户的缓存碎片,.author.username通过外键关联到用户模型 cache.clear()
def handle(self, *args, **options): users = OAuthUser.objects.filter(picture__isnull=False).exclude( picture__istartswith='https://resource.mtuktarov.com').all() self.stdout.write( 'Начинаем синхронизацию{count}аватаров'.format(count=len(users))) for u in users: self.stdout.write( 'Начать синхронизацию:{id}'.format(id=u.nikename)) url = u.picture url = save_user_avatar(url) if url: self.stdout.write( 'Завершить синхронизацию:{id}.url:{url}'.format( id=u.nikename, url=url)) u.picture = url u.save() self.stdout.write('Завершить синхронизацию')