def favourite_kwip(request,quip_id): quip= get_object_or_404(Quip, id=int(quip_id)) if request.user.is_authenticated: cache_key = '%s_favcount%d' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX,request.user.id,) cache.delete(cache_key) cache_key = '%s_fav%d-%d' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX,request.user.id,int(quip_id),) cache.delete(cache_key) if not Favourite.objects.filter(quip=quip,user=request.user): fav = Favourite(user=request.user,quip=quip) fav.save() if quip.account.user != request.user: timestamp = quip.created_at.strftime("%Y/%b/%d/%H%M%S") link = str(quip.account.user.username)+'/kwips/'+timestamp.lower() params_for_mail = {'#_1':str(quip.account.user.username),'#_2':str(request.user.username), '#_3':link} send_mail(str(quip.account.user.email),'kwippy <*****@*****.**>','favorite_kwip',params_for_mail) send_im(quip.account.user,'favorite_kwip',params_for_mail) up = get_object_or_404(User_Profile, user=request.user) up.fav_count = up.fav_count+1 up.save() return HttpResponse('Favourited') else: Favourite.objects.get(quip=quip,user=request.user).delete() up = get_object_or_404(User_Profile, user=request.user) up.fav_count = up.fav_count-1 up.save() return HttpResponse('UnFavourited') return HttpResponse('oops')
def save_model(self, request, obj, form, change): super().save_model(request, obj, form, change) # 发出任务 让celery发出任务 重新生成首页静态文件 from celery_tasks.tasks import create_index_static_html create_index_static_html.delay() # 删除缓存 cache.delete('goods_index_data')
def w(*args, **kwargs): try: key = namespace + _namespace_sep + key_func(*args, **kwargs) except TypeError: key = None if _force_set or not key: data = None else: data = _cache.get(key) if data is None: data = func(*args, **kwargs) if key: if data is None: _cache.delete(key) else: _cache.set(key, data, timeout) return data
def update (request, create_leaf = None): try: id = UUID (request.POST['leafId']) except: id = None if id is not None: return create_on_update (request, create_leaf) (type, ids) = json.loads (base64.b32decode (request.POST['leafId'])) if type == 'leaf': leaf = LEAF.objects.get (pk = ids[1]) if os.path.islink (leaf.file): ## copy-on-write source = os.readlink (leaf.file) os.remove (leaf.file) shutil.copy (source, leaf.file) with open (leaf.file, 'w') as uuid_file: uuid_file.write (request.POST['data'].encode ("utf-8")) leaf.name = request.POST['name'] leaf.save () while leaf.node.node: leaf.node = leaf.node.node object_key = hex (hash ((request.session.session_key, translator.processToReport, leaf.node.id))) cache.delete (object_key) ## invalidate cache object_key = hex (hash ((request.session.session_key, translator.processToText, leaf.node.id))) cache.delete (object_key) ## invalidate cache object_key = hex (hash ((request.session.session_key, translator.processToLatex, leaf.node.id))) cache.delete (object_key) ## invalidate cache object_key = hex (hash ((request.session.session_key, translator.processToHtml, leaf.node.id))) cache.delete (object_key) ## invalidate cache object_key = hex (hash ((request.session.session_key, translator.processToPdf, leaf.node.id))) cache.delete (object_key) ## invalidate cache response = success (request) else: response = failure (request) return response
def follow_user(request,followee_username): followee_user = get_object_or_404(User, username=followee_username) if request.META.has_key('HTTP_REFERER'): referer = request.META.get('HTTP_REFERER', '') else: referer = str(followee_username) followee_user = get_object_or_404(User, username=followee_username) follower_user = get_object_or_404(User,id=request.user.id) if follower_user!=followee_user: if request.GET['to_do']=="follow": if not Follower.objects.filter(followee=followee_user,follower=follower_user): if not follower_user.is_active == 4: default_notification_flag = get_object_or_404(User_Profile, user=follower_user).default_notification_on if default_notification_flag: f=Follower(follower=follower_user,followee=followee_user,im_notification=1) else: f=Follower(follower=follower_user,followee=followee_user) f.save() # need to ensure mail type matches the corresponding entry in databases' email table params_for_mail = {'#_1':get_display_name(follower_user),'#_2':get_display_name(followee_user), '#_3':follower_user.username} send_mail(str(followee_user.email),'kwippy <*****@*****.**>','follower',params_for_mail) send_im(followee_user,'follower',params_for_mail) request.session['flash'] = "You can also set IM notification of kwips by changing the settings." else: request.session['flash'] = "You are temporarily not allowed to follow someone. Please contact [email protected]" else: if Follower.objects.filter(followee=followee_user,follower=follower_user): #f=Follower.objects.get(followee=followee_user,follower=follower_user) f = get_object_or_404(Follower, followee=followee_user, follower=follower_user) f.delete() cache_key = '%s_follow%dto%d' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX,follower_user.id,followee_user.id,) cache.delete(cache_key) cache_key = '%s_userfollowerquery%d' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX,followee_user.id,) cache.delete(cache_key) cache_key = '%s_userfollowerqueryfull%d' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX,followee_user.id,) cache.delete(cache_key) cache_key = '%s_userfolloweequery%d' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX,follower_user.id,) cache.delete(cache_key) cache_key = '%s_followercount%d' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX,followee_user.id,) cache.delete(cache_key) cache_key = '%s_followeecount%d' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX,follower_user.id,) cache.delete(cache_key) return HttpResponseRedirect(referer)
def invalidate_item_cache(sender, instance, **kwargs): cache.delete(ITEM_CACHE_KEY.format(instance.id))
def get(self, request, course_id, format=None): """ Get course chapter list. 注意:如果移动端需要的vertical_types需要video之外的东西,整个方法需要重构 """ if not Course.objects.filter(course_id=course_id).exists(): return Response(status=status.HTTP_404_NOT_FOUND) if not CourseEnrollment.is_enrolled(request.user, course_id): return Response(status=status.HTTP_403_FORBIDDEN) user_agent = request.META.get('HTTP_USER_AGENT', '').lower() if 'androidtv' in user_agent: is_tv = True else: is_tv = False show_sequentials = request.GET.get('show_sequentials') if show_sequentials: if show_sequentials == '1' or show_sequentials.lower() == 'true': show_sequentials = True else: show_sequentials = False # 首先取一下缓存 if show_sequentials: # 手动清除缓存的后面 invalite_cache = request.GET.get('cache', None) if invalite_cache: cache_key_tv = 'api.course.{}.chapters_with_seq.{}'.format(course_id, True) cache_key = 'api.course.{}.chapters_with_seq.{}'.format(course_id, False) cache.delete(cache_key_tv) cache.delete(cache_key) if settings.DEBUG: cache_key = 'api.course.{}.chapters_with_seq.{}'.format(course_id, is_tv) + str(time.time()) else: cache_key = 'api.course.{}.chapters_with_seq.{}'.format(course_id, is_tv) cache_result = cache.get(cache_key) if cache_result: return Response(cache_result) course = get_course(course_id) chapters = [get_item(course_id, "chapter", chapter_id) for chapter_id in _get_item_id_list(course.children, "chapter")] now = datetime.now(UTC) chapters = filter(lambda x: x and x.start < now, chapters) if show_sequentials: # key sequential, value {video:0} seq_type_dict = defaultdict(lambda : defaultdict(int)) # 首先查出vertical需要的block列表 vertical_types = ['video'] vertical_dict = {vt: set() for vt in vertical_types} for vtype in vertical_types: blocks = get_items(course_id, vtype) blocks = filter(lambda x: x and x.start < now, blocks) vtype_set = _get_vertical_set(blocks) vertical_dict[vtype] = vtype_set for chapter in chapters: sequentials = [get_item(course_id, "sequential", sequential_id) for sequential_id in _get_item_id_list(chapter.children, "sequential")] sequentials = filter(lambda x: x and x.start < now, sequentials) chapter.sequentials = sequentials for sequential in sequentials: verticals = [get_item(course_id, "vertical", vertical_id) for vertical_id in _get_item_id_list(sequential.children, "vertical")] verticals = filter(lambda x: x and x.start < now, verticals) sequential.verticals = verticals # 通过之前查出的block集合 for vertical in verticals: blocks = vertical.children for block in blocks: category = _get_location_category(block) block_location_id = _get_location_id(block) if category in vertical_dict and block_location_id in vertical_dict[category]: seq_type_dict[sequential][category] += 1 for sequential, types in seq_type_dict.iteritems(): sequential.type = dict(types) chapters_array = ChapterWithSequentialSerializer(chapters, many=True).data if is_tv: cp_array = [] for chapters_temp in chapters_array: sq_array = [] for sq_temp in chapters_temp['sequentials']: if sq_temp['type'].get('video', None): # tv端过滤非video sq_array.append(sq_temp) chapters_temp['sequentials'] = sq_array if chapters_temp['sequentials'] !=[]: cp_array.append(chapters_temp) chapters_array = cp_array result = { "chapters": chapters_array, } if is_tv: cache.set(cache_key, result, 60 * 60 * 24 * 7) else: cache.set(cache_key, result, 60 * 60) else: result = { "chapters": ChapterSerializer(chapters, many=True).data } return Response(result)