def login(request): # log.info(request.META['HTTP_USER_AGENT']) code = 200 user_agent = request.META.get('HTTP_USER_AGENT', '') if 'iPhone' in user_agent or 'iPad' in user_agent: code = 409 if request.method == "POST": _forms = MobileUserSignInForm(request=request, data=request.POST) # log.info(request.POST) if _forms.is_valid(): _forms.login() _user = _forms.get_user() res = {'user': _user.v3_toDict(), 'session': _forms.get_session()} return SuccessJsonResponse(res) log.info(_forms.errors) for k, v in dict(_forms.errors).items(): error_msg = v.as_text().split('*')[1] log.info(error_msg) return ErrorJsonResponse(status=code, data={ 'type': k, 'message': error_msg.lstrip(), }) else: return ErrorJsonResponse(status=400)
def detail(request, user_id): _key = request.GET.get('session') try: _session = Session_Key.objects.get(session_key=_key) visitor = _session.user except Session_Key.DoesNotExist: visitor = None try: _user = GKUser.objects.get(pk=user_id) except GKUser.DoesNotExist: raise ErrorJsonResponse(status=404) _last_like = Entity_Like.objects.filter(user=_user).last() _last_note = Note.objects.filter(user=_user).last() res = dict() res['user'] = _user.v3_toDict(visitor) if _last_like: res['last_like'] = _last_like.entity.v3_toDict() if _last_note: res['last_note'] = _last_note.v3_toDict() return SuccessJsonResponse(res)
def tag_detail(request, user_id, tag): _key = request.GET.get('session') try: user = GKUser.objects.get(pk=user_id) except GKUser.DoesNotExist: return ErrorJsonResponse(status=404) tags = Content_Tags.objects.filter(creator_id=user_id, tag__name=tag, target_content_type_id=24) try: _session = Session_Key.objects.get(session_key = _key) _eid_list = Note.objects.filter(pk__in=tags.values_list('target_object_id', flat=True)).values_list('entity_id', flat=True) # eid_list = Note.objects.filter(pk__in=) el = Entity_Like.objects.user_like_list(user=_session.user, entity_list=list(_eid_list)) except Session_Key.DoesNotExist: el = None res = dict() res['user'] = user.v3_toDict() res['entity_list'] = [] for row in tags: entity = row.target.entity res['entity_list'].append(entity.v3_toDict(user_like_list=el)) return SuccessJsonResponse(res)
def entity_sort(category_id, reverse, offset, count, key): if reverse != 0: entity_list = APIEntity.objects.sort(category_id, like=False) else: entity_list = APIEntity.objects.sort(category_id, like=False) paginator = Paginator(entity_list, count) print entity_list.count() try: entities = paginator.page(offset) # except PageNotAnInteger: # entities = paginator.page(1) except EmptyPage: return ErrorJsonResponse(status=404) try: _session = Session_Key.objects.get(session_key=key) el = Entity_Like.objects.user_like_list( user=_session.user, entity_list=list(entities.object_list.values_list('id', flat=True))) except Session_Key.DoesNotExist: el = None res = [] for row in entities: r = row.v4_toDict(user_like_list=el) r.pop('images', None) r.pop('id', None) res.append(r) return SuccessJsonResponse(res)
def login(request): if request.method == "POST": _forms = MobileUserSignInForm(request=request, data=request.POST) log.info(request.POST) if _forms.is_valid(): _forms.login() _user = _forms.get_user() res = { 'user': _user.v3_toDict(), 'session': _forms.get_session() } return SuccessJsonResponse(res) else: _forms = MobileUserSignInForm(request=request) log.info(_forms.errors) for error in _forms.errors: # log.info("error %s" % error) return ErrorJsonResponse(status=400, data={ 'type': 'email', 'message': 'Error', }) return ErrorJsonResponse(status=400)
def entity(request, category_id): _offset = int(request.GET.get('offset', '0')) _offset = _offset / 30 + 1 _count = int(request.GET.get('count', '30')) _key = request.GET.get('session') # entity_list = Entity.objects.filter(category_id=category_id, status__gte=0) entity_list = Entity.objects.new_or_selection(category_id=category_id) paginator = ExtentPaginator(entity_list, _count) try: entities = paginator.page(_offset) except PageNotAnInteger: entities = paginator.page(1) except EmptyPage: return ErrorJsonResponse(status=404) try: _session = Session_Key.objects.get(session_key=_key) el = Entity_Like.objects.user_like_list( user=_session.user, entity_list=list(entities.object_list.values_list('id', flat=True))) except Session_Key.DoesNotExist: el = None res = [] for row in entities.object_list: r = row.v3_toDict(user_like_list=el) r.pop('images', None) r.pop('id', None) res.append(r) return SuccessJsonResponse(res)
def rest_password(request): if request.method == "POST": _key = request.POST.get('session', None) try: _session = Session_Key.objects.get(session_key=_key) except Session_Key.DoesNotExist: return ErrorJsonResponse(status=403) _forms = MobileRestPassword(user=_session.user, data=request.POST) if _forms.is_valid(): res = _forms.save() return SuccessJsonResponse(res) # log.info(_forms.errors) for k, v in dict(_forms.errors).items(): log.info(v.as_text().split('*')) error_msg = v.as_text().split('*')[1] return ErrorJsonResponse(status=400, data={ 'type': k, 'message': error_msg.lstrip(), }) return ErrorJsonResponse(status=500) else: return ErrorJsonResponse(status=400)
def entity_note(request, category_id): _offset = int(request.GET.get('offset', '0')) _count = int(request.GET.get('count', '30')) _offset = _offset / _count + 1 res = [] note_list = Note.objects.filter(entity__category_id=category_id) paginator = ExtentPaginator(note_list, _count) try: notes = paginator.page(_offset) except PageNotAnInteger: notes = paginator.page(1) except EmptyPage: return ErrorJsonResponse(status=404) for n in notes.object_list: # log.info(n) res.append({ 'note': n.v3_toDict(), 'entity': n.entity.v3_toDict(), }) return SuccessJsonResponse(res)
def user_like(request, category_id, user_id): _key = request.GET.get('session') _offset = int(request.GET.get('offset', '0')) _count = int(request.GET.get('count', '30')) _offset = _offset / _count + 1 innqs = Entity_Like.objects.filter(user_id=user_id).values_list( 'entity_id', flat=True) entity_list = Entity.objects.filter(category_id=category_id, id__in=innqs) paginator = ExtentPaginator(entity_list, _count) try: entities = paginator.page(_offset) except PageNotAnInteger: entities = paginator.page(1) except EmptyPage: return ErrorJsonResponse(status=404) res = [] for entity in entities: res.append(entity.v3_toDict(user_like_list=[entity.id])) return SuccessJsonResponse(res)
def like_action(request, entity_id, target_status): if request.method == "POST": _key = request.POST.get('session', None) try: _session = Session_Key.objects.get(session_key=_key) except Session_Key.DoesNotExist: return ErrorJsonResponse(status=403) res = { 'entity_id': entity_id, } if target_status == "1": like_task.delay(uid=_session.user_id, eid=entity_id) # try: # Entity_Like.objects.get(user_id=_session.user_id, entity_id=entity_id) # except Entity_Like.DoesNotExist: # Entity_Like.objects.create( # user_id = _session.user_id, # entity_id = entity_id # ) res['like_already'] = 1 else: # try: # el = Entity_Like.objects.get(user_id=_session.user_id, entity_id=entity_id) # el.delete() # except Entity_Like.DoesNotExist, e: # log.info("info %s", e.message) unlike_task.delay(uid=_session.user_id, eid=entity_id) res['like_already'] = 0 return SuccessJsonResponse(res) return ErrorJsonResponse(status=400)
def homepage(request): res = dict() # shows = Show_Banner.objects.all().values_list('banner_id', flat=True) # log.info(innqs) # banners = Banner.objects.filter(id__in=innqs) shows = Show_Banner.objects.all() res['banner'] = [] for row in shows: res['banner'].append({ 'url': row.banner.url, 'img': row.banner.image_url }) res['discover'] = [] # from django.db.models import Count # el = Entity_Like.objects.popular(scale='weekly') # category = Entity.objects.filter(pk__in=list(el)).annotate(dcount=Count('category')).values_list('category_id', flat=True) sub_category = Sub_Category.objects.popular_random(12) log.info(sub_category) for c in sub_category: res['discover'].append(c.v3_toDict()) # log.info(res['discover']) res['hottag'] = [] res['config'] = {} res['config']['taobao_ban_count'] = 2 res['config']['url_ban_list'] = [ 'http://m.taobao.com/go/act/mobile/cloud-jump.html' ] return SuccessJsonResponse(data=res)
def entity_note(request, user_id): # log.info(request.GET) try: _user = GKUser.objects.get(pk=user_id) except GKUser.DoesNotExist: return ErrorJsonResponse(status=404) # _offset = int(request.GET.get('offset', '0')) _count = int(request.GET.get('count', '30')) # _offset = _offset / _count + 1 _timestamp = request.GET.get('timestamp', datetime.now()) if _timestamp != None: _timestamp = datetime.fromtimestamp(float(_timestamp)) notes = APINote.objects.filter(user=_user, entity__status__gt=APIEntity.remove, post_time__lt=_timestamp). \ exclude(status=Note.remove).order_by('-post_time')[:_count] res = [] for note in notes: # log.info(note) res.append({ 'note': note.v4_toDict(), 'entity': note.entity.v3_toDict(), }) # log.info(res) return SuccessJsonResponse(res)
def detail(request, note_id): _key = request.GET.get('session', None) try: _session = Session_Key.objects.get(session_key = _key) np = Note_Poke.objects.user_poke_list(user=_session.user, note_list=[note_id]) except Session_Key.DoesNotExist: np = None res = dict() try: note = Note.objects.get(pk=note_id) except Note.DoesNotExist: raise ErrorJsonResponse(status=404) res['note'] = note.v3_toDict(user_note_pokes=np) res['entity'] = note.entity.v3_toDict() res['poker_list'] = [] for poker in note.pokes.all(): res['poker_list'].append( poker.user.v3_toDict() ) res['comment_list'] = [] for comment in note.comments.all(): res['comment_list'].append(comment.v3_toDict()) return SuccessJsonResponse(res)
def logout(request): # _req_uri = request.get_full_path() if request.method == "POST": _forms = MobileUserSignOutForm(request.POST) if _forms.is_valid(): res = _forms.logout() return SuccessJsonResponse({ 'success': '1' }) return ErrorJsonResponse(status=400)
def login_by_weibo(request): if request.method == "POST": _forms = MobileWeiboLoginForm(request.POST) if _forms.is_valid(): res = _forms.login() return SuccessJsonResponse(res) return ErrorJsonResponse(status=409, data={ 'type':'sina_id', }) return ErrorJsonResponse(status=400)
def refetch_image(request, entity_id): try: _entity = Entity.objects.get(pk=entity_id) except Entity.DoesNotExist: raise Http404 log.info(_entity.images) fetch_image(_entity.images, _entity.pk) return SuccessJsonResponse(data={'status': 'ok'})
def remove_buy_link(request, bid): try: b = Buy_Link.objects.get(pk=bid) except Buy_Link.DoesNotExist: return ErrorJsonResponse(status=404) if b.default: return ErrorJsonResponse(status=403) b.delete() return SuccessJsonResponse()
def tag_list(request, user_id): try: _user = APIUser.objects.get(pk=user_id) except APIUser.DoesNotExist: return ErrorJsonResponse(status=404) res = {} res['user'] = _user.v4_toDict() res['tags'] = Content_Tags.objects.user_tags(user=_user.pk) return SuccessJsonResponse(res)
def delete(request, comment_id): if not request.is_ajax() or not request.method == "POST": return ErrorJsonResponse(status=400) try: comment = Note_Comment.objects.get(pk=comment_id) comment.delete() except Note_Comment.DoesNotExist: return ErrorJsonResponse(status=404) return SuccessJsonResponse(data={'status': 'success'})
def usite_published(request): if request.is_ajax(): entityids_json = request.POST.get('eids', None) # log.info(entityids_json) from apps.core.tasks import usite_published # usite_published(entityids_json) usite_published.delay(entityids_json) # print json.loads(entityids_json) return SuccessJsonResponse(data={'status': 'ok'}) else: return ErrorJsonResponse(status=400)
def guess(request): res = [] _category_id = request.GET.get('cid', None) _count = int(request.GET.get('count', '5')) entities = Entity.objects.guess(category_id=_category_id, count=_count) for entity in entities: res.append(entity.v3_toDict()) return SuccessJsonResponse(res)
def delete(request): if request.method == "POST": mid = request.POST.get('mid', None) try: medium = Media.objects.get(pk = mid) medium.delete() except Media.DoesNotExist: raise Http404 return SuccessJsonResponse(data={'msg':'success'}) else: return ErrorJsonResponse(status=400)
def unlink_by_weibo(request): if request.method == "POST": _form = MobileWeiboUnLinkForm(request.POST) if _form.is_valid(): _form.unlink() return SuccessJsonResponse(data={'status':'success'}) for k, v in _form.errors.items(): _message = _form.error_class.as_text(v) return ErrorJsonResponse(status=403, data={ 'type': k, 'message': _message.replace('*', ''), }) return ErrorJsonResponse(status=400)
def update_account(request): if request.method == "POST": _key = request.POST.get('session', None) try: _session = Session_Key.objects.get(session_key=_key) except Session_Key.DoesNotExist: return ErrorJsonResponse(status=403) _forms = MobileUserRestPassword(user=_session.user, data=request.POST) if _forms.is_valid(): res = _forms.save() return SuccessJsonResponse(res) log.info(_forms.errors) return ErrorJsonResponse(status=400)
def forget_password(request): # if request.method == "POST": if request.method == 'POST': _forms = APIUserPasswordResetForm(request.POST) if _forms.is_valid(): _forms.save(template_invoke_name=settings.RESET_PASSWORD_TEMPLATE, domain_override=settings.SITE_DOMAIN) return SuccessJsonResponse(data={'success': '1'}) return ErrorJsonResponse(data={ 'type': 'email', 'message': 'email does not exist', }, status=200) return ErrorJsonResponse(status=400)
def remove(request, note_id): if request.method == "POST": _key = request.POST.get('session') try: _session = Session_Key.objects.get(session_key=_key) except Session_Key.DoesNotExist: return ErrorJsonResponse(status=403) try: note = APINote.objects.get(pk=note_id, user=_session.user) except Note.DoesNotExist: return ErrorJsonResponse(status=404) note.delete() return SuccessJsonResponse(data={'delete_already': 1})
def del_comment(request, note_id, comment_id): if request.method == "POST": _key = request.POST.get('session') try: _session = Session_Key.objects.get(session_key = _key) except Session_Key.DoesNotExist: return ErrorJsonResponse(status=403) # log.info("comment id %s" % comment_id) try: nc = Note_Comment.objects.get(user=_session.user, pk=comment_id) nc.delete() return SuccessJsonResponse(data={'delete_already':1}) except Note_Comment.DoesNotExist: return ErrorJsonResponse(status=404) return ErrorJsonResponse(status=400)
def update(request): if request.method == "POST": # log.info(request.POST) # log.info(request.FILES) _key = request.POST.get('session', None) try: _session = Session_Key.objects.get(session_key=_key) except Session_Key.DoesNotExist: return ErrorJsonResponse(status=403) _forms = MobileUserProfileForm(user=_session.user, data=request.POST, files=request.FILES) if _forms.is_valid(): res = _forms.save() return SuccessJsonResponse(res) return ErrorJsonResponse(status=400)
def post_note(request, entity_id): if request.method == "POST": # _key = request.POST.get('session') try: entity = Entity.objects.get(pk = entity_id) except Entity.DoesNotExist: return ErrorJsonResponse(status=404) _forms = PostNoteForms(entity=entity, data=request.POST) if _forms.is_valid(): res = _forms.save() return SuccessJsonResponse(res) return ErrorJsonResponse(status=403) return ErrorJsonResponse(status=400)
def link_by_weibo(request): if request.method == "POST": _forms = MobileWeiboLinkForm(request.POST) if _forms.is_valid(): res = _forms.save() return SuccessJsonResponse(res) # log.info(dict(_forms.errors)) for k, v in _forms.errors.items(): _message = _forms.error_class.as_text(v) return ErrorJsonResponse(status=403, data={ 'type': k, 'message': _message.replace('*', ''), }) return ErrorJsonResponse(status=400)