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 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 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')))