def delete_content_by_id(id): """统一的删除内容工具函数,没有加类型校验""" data = Content.get_by_id(id) if data: data.delete() else: raise Http404
def get_pre_next_data(current_id, type): """史上最龌龊的实现,哈哈""" pre_data = None pre_id = current_id while not pre_data: pre_id -= 1 if pre_id > 0: _pre_data = Content.get_by_id(pre_id) if _pre_data and _pre_data.type == type: pre_data = _pre_data if pre_id < current_id - 10: break next_data = None next_id = current_id while not next_data: next_id += 1 if next_id > 0: _next_data = Content.get_by_id(next_id) if _next_data and _next_data.type == type: next_data = _next_data if next_id > current_id + 10: break return (pre_data, next_data)
def view(request, iq_id): if not iq_id: return HttpResponseRedirect('/') try: id = int(iq_id) except ValueError: raise Http404 iq = Content.get_by_id(id) if not iq or iq.type < content_type.IQ or iq.type > content_type.IQ_MAX: raise Http404 iq.view_count += 1 iq.put() #pre next (iq_pre, iq_next) = get_pre_next_data(id, iq.type) return render_to_response('iq/view.html', {'iq':iq, 'iq_pre':iq_pre, 'iq_next':iq_next}, context_instance=RequestContext(request))
def view(request, humor_id): if not humor_id: return HttpResponseRedirect('/') try: id = int(humor_id) except ValueError: raise Http404 humor = Content.get_by_id(id) if not humor or humor.type < content_type.HUMOR or humor.type > content_type.HUMOR_MAX: raise Http404 humor.view_count += 1 humor.put() #pre next (humor_pre, humor_next) = get_pre_next_data(id, humor.type) return render_to_response('humor/view.html', {'humor':humor, 'humor_pre':humor_pre, 'humor_next':humor_next}, context_instance=RequestContext(request))
def latest(request, page_id): riddle_set = ( Content.all() .filter("type >=", content_type.RIDDLE) .filter("type <=", content_type.RIDDLE_MAX) .order("type") .order("-create_time") ) return object_list_page( request, queryset=riddle_set, per_page=size.DEFAULT_LIST, template_name="riddle/latest.html", page=page_id, extra_context={"url_prefix": r"/riddle/latest/"}, )
def humor_data(request, page_id): humor_set = ( Content.all() .filter("type >=", content_type.HUMOR) .filter("type <=", content_type.HUMOR_MAX) .order("type") .order("-create_time") ) return object_list_page( request, queryset=humor_set, per_page=size.DEFAULT_MANAGE_LIST, template_name="manage/content_list.html", page=page_id, extra_context={"url_prefix": r"/manage/humor_data/", "type": "humor"}, )
def view(request, riddle_id): if not riddle_id: return HttpResponseRedirect("/") try: id = int(riddle_id) except ValueError: raise Http404 riddle = Content.get_by_id(id) if not riddle or riddle.type < content_type.RIDDLE or riddle.type > content_type.RIDDLE_MAX: raise Http404 riddle.view_count += 1 riddle.put() # pre next (riddle_pre, riddle_next) = get_pre_next_data(id, riddle.type) return render_to_response( "riddle/view.html", {"riddle": riddle, "riddle_pre": riddle_pre, "riddle_next": riddle_next}, context_instance=RequestContext(request), )
def content_edit(request, content_id): if not content_id: return HttpResponseRedirect("/") try: id = int(content_id) except ValueError: raise Http404 data = Content.get_by_id(id) if not data: raise Http404 if request.method == "POST": form = ContentManageForm(request.POST) if form.is_valid(): edit_data = form.save(commit=False) data.subject = edit_data.subject data.content = edit_data.content data.content_a = edit_data.content_a data.source_type = edit_data.source_type data.source_link = edit_data.source_link data.property = edit_data.property data.type = edit_data.type data.status = edit_data.status data.good_score = edit_data.good_score data.bad_score = edit_data.bad_score data.favor_score = edit_data.favor_score data.view_count = edit_data.view_count data.put() return render_to_response( "manage/content_edit.html", {"form": form, "content_id": content_id, "success_msg": u"成功修改"}, context_instance=RequestContext(request), ) return render_to_response( "manage/content_edit.html", {"form": form, "content_id": content_id}, context_instance=RequestContext(request), ) form = ContentManageForm(instance=data) return render_to_response( "manage/content_edit.html", {"form": form, "content_id": content_id}, context_instance=RequestContext(request) )
def bury(request, type, id): """type暂时没检测""" if not type: return HttpResponse() try: type = int(type) except ValueError: raise Http404 if not id: return HttpResponse() try: id = int(id) except ValueError: raise Http404 # data = Content.gql('WHERE type=:1 and key.id=:2', type, id) data = Content.get_by_id(id) if not data: raise Http404 data.bad_score += 1 data.put() return HttpResponse()
def get_cached_promotion_data(promotion_subject, time=6000): """ 使用规则的名称获得缓存的数据list。 TODO 应该做类型检查! """ cached_data = memcache.get(promotion_subject) if not cached_data: promotion = Promotion.all().filter('subject', promotion_subject) cached_data_ids = [] for item in promotion: cached_data_string_ids_str = item.content.split(',') for id_str in cached_data_string_ids_str: try: id = int(id_str) except Exception: continue if id > 0: cached_data_ids.append(id) temp_cached_data = Content.get_by_id(cached_data_ids) cached_data = [data for data in temp_cached_data if data is not None] memcache.set(promotion_subject, cached_data, time) return cached_data
def latest(request, page_id): iq_set = Content.all().filter('type >=', content_type.IQ).filter('type <=', content_type.IQ_MAX).order('type').order('-create_time') return object_list_page(request, queryset=iq_set, per_page=size.DEFAULT_LIST, template_name='iq/latest.html', page=page_id, extra_context={'url_prefix':r'/iq/latest/'})