def render_to_response(self, context, **response_kwargs): ''' 验证view_response :param context: :param response_kwargs: :return: ''' try: if not self.request.GET.get('code') or not self.request.GET.get( 'state'): return HttpResponseError(self.request).HttpResponse_or_403() RegisterState = models.RegisterState.objects.get( state=self.request.GET.get('state')) except models.RegisterState.DoesNotExist: return HttpResponseError(self.request).HttpResponse_or_403() pass from managestage.utli.datetimenow import datetime_unix RegisterState.state_code = str(datetime_unix()).split('.')[0] RegisterState.code = self.request.GET.get('code') RegisterState.save() return redirect( reverse('auth:oauth2:TokenViewSet', args=[RegisterState.state_code]))
def AuthToken(self, code): data = { 'type': 'cgr', 'grant_type': 'authorization_code', 'code': code, 'redirect_uri': self.redirect_uri, } RegisterState.objects.get(code=code) html = self.service.get_raw_access_token(data=data) log.i(globals(), 'AuthToken_POST', html.text) if 'refresh' in html.text: html = self.service.get_raw_access_token(data=data) log.i(globals(), 'AuthToken_POST', html.text, '刷新页面') pass jso = json.loads(html.text) try: self.access_token = jso['access_token'] except: from managestage.utli.datetimenow import datetime_unix error = jso['error'] if error == 'invalid_grant': state = str(datetime_unix()).split('.')[0] params = { 'redirect_uri': self.redirect_uri, 'response_type': 'code', 'scope': 'read', 'state': str(state), 'type': 'cgr', } response = requests.post(url=self.userinfo_url, data=params) print(response.text) pass print(jso) print( '-------------------------------------------------------------' ) print('KeyError 错误,重新登录') return None print('GET获取') return self.access_token
def dispatch(self, request, *args, **kwargs): try: self.app_id = self.kwargs['app_id'] self.app_unix = self.kwargs['app_unix'] except KeyError: self.app_id = None self.app_unix = None if self.app_id and self.app_unix: self.article_editor = True if not self.app_unix: self.app_unix = int(datetimenow.datetime_unix()) return super(ArticleRelease, self).dispatch(request, *args, **kwargs)
def _add_broadcast(request): ''' 参加添加轮播图 :param request: :return: ''' action = request.POST.get('action') id = request.POST.get('id') url = request.POST.get('url') name = request.POST.get('name') images = request.FILES.get('image') str_images = '{}.{}'.format(datetime_unix(), str(images.name).split('.')[1]) if action == 'WareApp': url = '/app/details/{}/?m=1F'.format( models.WareApp.objects.get(id=id).id) models.Carousel(name=name, image=str_images, time_add=TIMEDATES(), time_now=TIMEDATES(), url=url).save() print('WareApp') pass if action == 'custom': models.Carousel(name=name, image=str_images, time_add=TIMEDATES(), time_now=TIMEDATES(), url=url).save() print('custom') pass from PIL import Image from lhwill import settings print('images.name', settings.MEDIA_ROOT + '/' + str_images) if images: img = Image.open(images) img.save(settings.MEDIA_ROOT + '/carousel/' + str_images) # return HttpResponsePermanentRedirect('/admin/sector/broadcast/') return redirect(reverse('admins:broadcast')) pass
def AuthLogin(self): ''' 国采登录 :return: ''' from managestage.utli.datetimenow import datetime_unix state = str(datetime_unix()).split('.')[0] models.RegisterState(state=state).save() params = { 'redirect_uri': self.redirect_uri, 'response_type': 'code', 'scope': 'read', 'state': str(state), 'type': 'cgr', } return self.service.get_authorize_url(**params) pass
def _set_broadcast(request): ''' 修改轮播图 :param request: :return: ''' action = request.POST.get('action') id = request.POST.get('id') car_id = request.POST.get('car_id') url = request.POST.get('url') name = request.POST.get('name') images = request.FILES.get('image') if action == 'WareApp' and id != '-1': url = '/app/details/{}/'.format(models.WareApp.objects.get(id=id).id) pass car = models.Carousel.objects.get(id=car_id) if name: car.name = name if images: str_images = '{}.{}'.format(datetime_unix(), str(images.name).split('.')[1]) car.image = str_images print('images.name', settings.MEDIA_ROOT + '/' + str_images) img = Image.open(images) img.save(settings.MEDIA_ROOT + '/carousel/' + str_images) if url and id != '-1': car.url = url car.save() # return HttpResponsePermanentRedirect('/admin/sector/broadcast/') return redirect(reverse('admins:broadcast')) pass
def importfile(request): files = request.FILES.get('upfile') path = os.path.join(settings.BASE_DIR, 'import') try: imfile = ImportFile.objects.filter()[:1].get() except: imfile = ImportFile(stype=0) imfile.save() if len(os.listdir(path)) < 1: imfile.stype = 0 imfile.save() pass if imfile.stype == 0 or imfile.stype == '0': if os.path.isdir(path): shutil.rmtree(path) imfile.stype = 1 imfile.save() os.makedirs(path) pass f = open(os.path.join(path, files.name), 'wb') for chunk in files.chunks(): f.write(chunk) f.close() import zipfile fz = zipfile.ZipFile(os.path.join(path, files.name), 'r') for file in fz.namelist(): fz.extract(file, path) fz.close() os.remove(os.path.join(path, files.name)) else: logger.i(os.path.join(settings.BASE_DIR, '有用户正在执行任务,擒等待任务完成', )) content = { 'state': 'error', 'error': '有用户正在执行任务,擒等待任务完成', 'code': '403' } return HttpResponse(json.dumps(content)) pass Im = None url = [] if os.path.exists(path): dirs = os.listdir(path) for dirc in dirs: print(dirc) url.append(dirc) unix = datetimenow.datetime_unix() unix = '{}{}'.format(unix, random.randint(1000000000, 1520000000)) # 1527735195 Im = ImportWareApp(os.path.join(path, dirc), unix) Im.dirList() Im.addModelsObjects() pass pass try: imfile = ImportFile.objects.filter()[:1].get() imfile.stype = 0 imfile.save() except: imfile = ImportFile(stype=0) imfile.save() pass if os.path.isdir(path): shutil.rmtree(path) os.makedirs(path) pass content = { 'state': 'success', 'code': '200' } return HttpResponse(json.dumps(content)) pass
def AuthVerifyToLogin(self, objectModels): OA = OAuth2() token = OA.AuthToken(objectModels.code) userInfo = OA.get_UserInfo() uitInfo = OA.get_UnitInfo() if userInfo['success']: try: username = models.User.objects.get( usercode=userInfo['rows']['usercode']) self.register_auth = False except User.DoesNotExist: self.register_auth = True password = '******'.format(datetime_unix(), range(20000, 90000)) username = models.User.objects.create_user( username=userInfo['rows']['usercode'], email=userInfo['rows']['email'], usercode=userInfo['rows']['usercode'], state=1, password=password) logger.i('create_user', username) UserInfo(usercode=userInfo['rows']['usercode'], email=userInfo['rows']['email'], name=userInfo['rows']['name'], qq=userInfo['rows']['qq'], sex=userInfo['rows']['sex'], phone=userInfo['rows']['phone'], birthday=userInfo['rows']['birthday'], fax=userInfo['rows']['fax'], job=userInfo['rows']['job'], userlevel=userInfo['rows']['userlevel'], key=username).save() UnitInfo(usercode=userInfo['rows']['usercode'], name=uitInfo['rows']['name'], shortname=uitInfo['rows']['shortname'], orgcode=uitInfo['rows']['orgcode'], detailaddress=uitInfo['rows']['detailaddress'], postalcode=uitInfo['rows']['postalcode'], website=uitInfo['rows']['website'], telephone=uitInfo['rows']['telephone'], fax=uitInfo['rows']['fax'], description=uitInfo['rows']['description'], istopunit=uitInfo['rows']['istopunit'], province=uitInfo['rows']['province'], city=uitInfo['rows']['city'], remark=uitInfo['rows']['remark'], topunitname=uitInfo['rows']['topunitname'], topunitcode=uitInfo['rows']['topunitcode'], unitcode=uitInfo['rows']['unitcode'], hyxt1=uitInfo['rows']['hyxt1'], hyxt2=uitInfo['rows']['hyxt2'], key=username).save() Address(defaule=True, consigneeName=userInfo['rows']['name'], province=uitInfo['rows']['province'], city=uitInfo['rows']['city'], consigneeAddress=uitInfo['rows']['detailaddress'], consigneeMobile=userInfo['rows']['phone'], email=userInfo['rows']['email'], key=username).save() pass try: UserToken.objects.get(usercode=userInfo['rows']['usercode']) except UserToken.DoesNotExist: UserToken(usercode=userInfo['rows']['usercode'], username=username, token=token).save() self.set_logig(username) return True return False pass