def favorite(request): _siteid = request.POST.get('siteid', '') if _siteid: _usr, _host = getUsrHost(request) u = UserInfo.objects.filter(username=_usr) if u.count() == 1: try: Favorite.objects.get(site_id=_siteid, user=u[0], host=_host).delete() siteFavChange(_siteid, -1) Log.objects.create(site_id=_siteid, user=u[0], host=_host, descr="Cancel Favorite") return HttpResponse(json.dumps(getErrorCode('cancel_favorite_success'))) except: Favorite.objects.create(site_id=_siteid, user=u[0], host=_host) Log.objects.create(site_id=_siteid, user=u[0], host=_host, descr="Inset Favorite") else: try: Favorite.objects.get(site_id=_siteid, host=_host).delete() siteFavChange(_siteid, -1) Log.objects.create(site_id=_siteid, host=_host, descr="Cancel Favorite") return HttpResponse(json.dumps(getErrorCode('cancel_favorite_success'))) except: Favorite.objects.create(site_id=_siteid, host=_host) Log.objects.create(site_id=_siteid, host=_host, descr="Insert Favorite") siteFavChange(_siteid, 1) return HttpResponse(json.dumps(getErrorCode('favorite_site_success'))) else: return HttpResponse(json.dumps(getErrorCode('site_id_not_exists')))
def like(request): _flag = 'false' != request.POST.get('flag', '') _siteid = request.POST.get('siteid', '') _usr, _host = getUsrHost(request) try: u = UserInfo.objects.get(username=_usr) try: Like.objects.get(site_id=_siteid, user=u, flag=not _flag) return HttpResponse(json.dumps(getErrorCode('already_like_unlike'))) except: try: Like.objects.get(site_id=_siteid, user=u, flag=_flag).delete() siteLikeChange(_siteid, _flag, -1) Log.objects.create(site_id=_siteid, user=u, host=_host, descr="Cancel like/unlike") return HttpResponse(json.dumps(getErrorCode('cancel_like_unlike_success'))) except: Like.objects.create(site_id=_siteid, user=u, host=_host, flag=_flag) Log.objects.create(site_id=_siteid, user=u, host=_host, descr="Insert like/unlike") except: try: Like.objects.get(site_id=_siteid, host=_host, flag=not _flag) return HttpResponse(json.dumps(getErrorCode('already_like_unlike'))) except: try: Like.objects.get(site_id=_siteid, host=_host, flag=_flag).delete() siteLikeChange(_siteid, _flag, -1) Log.objects.create(site_id=_siteid, host=_host, descr="Cancel like/unlike") return HttpResponse(json.dumps(getErrorCode('cancel_like_unlike_success'))) except: Like.objects.create(site_id=_siteid, host=_host, flag=_flag) Log.objects.create(site_id=_siteid, host=_host, descr="Insert like/unlike") siteLikeChange(_siteid, _flag, 1) return HttpResponse(json.dumps(getErrorCode('like_unlike_success')))
def getFavSite(request): _usr, _host = getUsrHost(request) favSetList = Favorite.objects.filter(user__username=_usr).order_by('-site__siteClickNum') if 'usr' in request.COOKIES else Favorite.objects.filter(host=_host).order_by('-site__siteClickNum') favsite = [] for siteSet in favSetList: siteDict = model_to_dict(siteSet.site) siteDict['like'] = getLikeFlag(request, siteSet.site_id, True) siteDict['unlike'] = getLikeFlag(request, siteSet.site_id, False) siteDict['fav'] = getFavFlag(request, siteSet.site_id) favsite.append(siteDict) return favsite
def visit(request): _siteid = request.POST.get('siteid', '') _usr, _host = getUsrHost(request) try: u = UserInfo.objects.get(username=_usr) Visit.objects.create(site_id=_siteid, user=u, host=_host) except: Visit.objects.create(site_id=_siteid, host=_host) s = Site.objects.get(id=_siteid) s.siteClickNum = s.siteClickNum + 1 s.save() return HttpResponse(json.dumps(getErrorCode('record_visit_success')))
def getFavSite(request): _usr, _host = getUsrHost(request) favSetList = Favorite.objects.filter(user__username=_usr, site__display=0).order_by('-site__siteClickNum') if 'usr' in request.COOKIES else Favorite.objects.filter(host=_host, site__display=0).order_by('-site__siteClickNum') return sitePerfectInfo(request, favSetList, 1)