def get_object(self, pk=None): if pk: obj = read_from_cache(self.redis_key + '_%s' % pk) if not obj: try: obj = self.get_queryset().get(id=pk) except organization.DoesNotExist: raise InvestError(code=5002) else: write_to_cache(self.redis_key + '_%s' % pk, obj) else: lookup_url_kwarg = 'pk' obj = read_from_cache(self.redis_key + '_%s' % self.kwargs[lookup_url_kwarg]) if not obj: try: obj = self.get_queryset().get( id=self.kwargs[lookup_url_kwarg]) except organization.DoesNotExist: raise InvestError(code=5002) else: write_to_cache( self.redis_key + '_%s' % self.kwargs[lookup_url_kwarg], obj) if obj.datasource != self.request.user.datasource: raise InvestError(code=8888, msg='资源非同源') return obj
def get_object(self, pk=None): if pk: obj = read_from_cache(self.redis_key + '_%s' % pk) if not obj: try: obj = self.Model.objects.get(id=pk, is_deleted=False) except self.Model.DoesNotExist: raise InvestError( code=6002, msg='timeline with this "%s" is not exist' % pk) else: write_to_cache(self.redis_key + '_%s' % pk, obj) else: lookup_url_kwarg = 'pk' obj = read_from_cache(self.redis_key + '_%s' % self.kwargs[lookup_url_kwarg]) if not obj: try: obj = self.Model.objects.get( id=self.kwargs[lookup_url_kwarg], is_deleted=False) except self.Model.DoesNotExist: raise InvestError( code=6002, msg='timeline with this "%s" is not exist' % self.kwargs[lookup_url_kwarg]) else: write_to_cache( self.redis_key + '_%s' % self.kwargs[lookup_url_kwarg], obj) if obj.datasource != self.request.user.datasource: raise InvestError(code=8888) return obj
def eventCountByAddress(self): countDic = read_from_cache('countEventByAddress') if not countDic: countDic = {} for cat in ChinaList: count = self.queryset.filter(com_addr=cat).count() countDic[cat] = count write_to_cache('countEventByAddress', countDic) return countDic
def eventCountByCat(self): allCat = CompanyCatData.objects.all().filter(p_cat_name=None) countDic = read_from_cache('countEventByCat') if not countDic: countDic = {} for cat in allCat: count = self.queryset.filter(com_cat_name=cat.cat_name).count() countDic[cat.cat_name] = count write_to_cache('countEventByCat', countDic) return countDic
def recordUpload(request): try: record = datetime.datetime.now().strftime('%y%m%d%H%M%S') + ''.join( random.sample(string.ascii_lowercase, 6)) write_to_cache(record, {'files': [], 'is_active': True}) return JSONResponse(SuccessResponse({record: read_from_cache(record)})) except InvestError as err: return JSONResponse(InvestErrorResponse(err)) except Exception: return JSONResponse( ExceptionResponse(traceback.format_exc().split('\n')[-2]))
def updateUpload(request): try: data = request.data record = data.get('record') files = data.get('files', []) if read_from_cache(record) is None: raise InvestError(8003, msg='没有这条记录') write_to_cache(record, {'files': files, 'is_active': True}, 3600) return JSONResponse(SuccessResponse({record: read_from_cache(record)})) except InvestError as err: return JSONResponse(InvestErrorResponse(err)) except Exception: return JSONResponse( ExceptionResponse(traceback.format_exc().split('\n')[-2]))
def cancelUpload(request): try: data = request.data record = data.get('record') recordDic = read_from_cache(record) if recordDic is None: raise InvestError(8003, msg='没有这条记录') recordDic['is_active'] = False write_to_cache(record, recordDic, 3600) return JSONResponse(SuccessResponse({record: read_from_cache(record)})) except InvestError as err: return JSONResponse(InvestErrorResponse(err)) except Exception: return JSONResponse( ExceptionResponse(traceback.format_exc().split('\n')[-2]))
def eventCountByRound(self): year = datetime.datetime.now().year timelist = [str(i) for i in range(year - 10, year + 1)] countList = read_from_cache('countEventByRound') if not countList: countList = {} for year in timelist: qs = self.queryset.filter(date__startswith=year) countDic = {} for cat in RoundList: count = qs.filter(round=cat).count() countDic[cat] = count countList[year] = countDic write_to_cache('countEventByRound', countList) return countList
def updateUpload(request): try: data = request.data record = data.get('record') bucket = data.get('bucket') key = data.get('key') filename = data.get('filename', 'mobilefile') if read_from_cache(record) is None: raise InvestError(8003, msg='没有这条记录') write_to_cache(record, { 'bucket': bucket, 'key': key, 'filename': filename, 'is_active': True }, 3600) return JSONResponse(SuccessResponse({record: read_from_cache(record)})) except InvestError as err: return JSONResponse(InvestErrorResponse(err)) except Exception: return JSONResponse( ExceptionResponse(traceback.format_exc().split('\n')[-2]))