def plan_detail(request, access_token, planid): result = login_auth(access_token) if result['err']['code'] != 0: return HttpResponse(json.dumps(result)) try: plan = Plan.objects.get(id__exact=planid) userid = result['data']['id'] if plan is None: result = format(404, 'no plan found') elif not get_viewable(userid, plan): result = format(403, 'not viewable') else: joinable = get_joinable(userid, plan) editable = get_editable(userid, plan) joined_list = get_joiners(plan) joined = False joined_id_list = [joiner['id'] for joiner in joined_list] if userid in joined_id_list: joined = True data = { 'plan_id': planid, 'joinable': joinable, 'editable': editable, 'joined': joined, 'joined_list': joined_list } result = format(0, 'success', data) return HttpResponse(json.dumps(result)) except Exception as e: result = format(400, str(e)) return HttpResponse(json.dumps(result))
def edit_plan(request, access_token, planid): result = login_auth(access_token) if result['err']['code'] != 0: return HttpResponse(json.dumps(result)) userid = result['data']['id'] try: plan = Plan.objects.get(id__exact=planid) user = FBUser.objects.get(fbid=userid) PrivatePlan.objects.filter(accessible_plan=plan).delete() plan.holder = user plan.title = request.POST.get('title', plan.title) plan.destination = request.POST.get('destination', plan.destination) plan.description = request.POST.get('description', plan.description) plan.depart_time = request.POST.get('depart_time', plan.depart_time) plan.length = request.POST.get('length', plan.length) plan.limit = request.POST.get('limit', plan.limit) visible_type = request.POST.get('visible_type', plan.visible_type) plan.visible_type = int(visible_type) friend_list = request.POST.getlist('friendlist', []) plan.full_clean() plan.save() if plan.visible_type == 3: for friendid in friend_list: friend = FBUser.objects.get(fbid=friendid) private = PrivatePlan() private.accessible_user = friend private.accessible_plan = plan private.full_clean() private.save() result = format(0, 'edit success') return HttpResponse(json.dumps(result)) except Exception as e: result = format(400, str(e)) return HttpResponse(json.dumps(result))
def add_plan(request, access_token): result = login_auth(access_token) if result['err']['code'] != 0: return HttpResponse(json.dumps(result)) userid = result['data']['id'] try: new_plan = Plan() user = FBUser.objects.get(fbid=userid) new_plan.holder = user new_plan.title = request.POST.get('title', "testtitle") new_plan.destination = request.POST.get('destination', "testdestination") new_plan.description = request.POST.get('description', "testdescription") new_plan.depart_time = request.POST.get('depart_time', datetime.today()) new_plan.length = request.POST.get('length', 2) new_plan.limit = request.POST.get('limit', 2) visible_type = request.POST.get('visible_type', 1) new_plan.visible_type = int(visible_type) friend_list = request.POST.getlist('friendlist',[]) new_plan.full_clean() new_plan.save() if new_plan.visible_type == 3: for friendid in friend_list: friend = FBUser.objects.get(fbid=friendid) private = PrivatePlan() private.accessible_user = friend private.accessible_plan = new_plan private.full_clean() private.save() result = format(0, 'create success') return HttpResponse(json.dumps(result)) except Exception as e: result = format(400, str(e)) return HttpResponse(json.dumps(result))
def plan_detail(request, access_token, planid): result = login_auth(access_token) if result['err']['code'] != 0: return HttpResponse(json.dumps(result)) try: plan = Plan.objects.get(id__exact=planid) userid = result['data']['id'] if plan is None: result = format(404, 'no plan found') elif not get_viewable(userid, plan): result = format(403, 'not viewable') else: joinable = get_joinable(userid, plan) editable = get_editable(userid, plan) joined_list = get_joiners(plan) joined = False joined_id_list = [joiner['id'] for joiner in joined_list] if userid in joined_id_list: joined = True data = { 'plan_id': planid, 'joinable': joinable, 'editable': editable, 'joined': joined, 'joined_list': joined_list } result = format(0, 'success', data) return HttpResponse(json.dumps(result)) except Exception as e: result = format(400, str(e)) return HttpResponse(json.dumps(result))
def edit_plan(request, access_token, planid): result = login_auth(access_token) if result['err']['code'] != 0: return HttpResponse(json.dumps(result)) userid = result['data']['id'] try: plan = Plan.objects.get(id__exact=planid) user = FBUser.objects.get(fbid=userid) PrivatePlan.objects.filter(accessible_plan=plan).delete() plan.holder = user plan.title = request.POST.get('title', plan.title) plan.destination = request.POST.get('destination', plan.destination) plan.description = request.POST.get('description', plan.description) plan.depart_time = request.POST.get('depart_time', plan.depart_time) plan.length = request.POST.get('length', plan.length) plan.limit = request.POST.get('limit', plan.limit) visible_type = request.POST.get('visible_type', plan.visible_type) plan.visible_type = int(visible_type) friend_list = request.POST.getlist('friendlist', []) plan.full_clean() plan.save() if plan.visible_type == 3: for friendid in friend_list: friend = FBUser.objects.get(fbid=friendid) private = PrivatePlan() private.accessible_user = friend private.accessible_plan = plan private.full_clean() private.save() result = format(0, 'edit success') return HttpResponse(json.dumps(result)) except Exception as e: result = format(400, str(e)) return HttpResponse(json.dumps(result))
def delete_plan(request, access_token, planid): result = login_auth(access_token) if result['err']['code'] != 0: return HttpResponse(json.dumps(result)) userid = result['data']['id'] try: plan = Plan.objects.get(id__exact=planid) if get_editable(userid, plan): plan.delete() result = format(0, 'delete success') else: result = format(403, 'delete permission denied') return HttpResponse(json.dumps(result)) except Exception as e: result = format(400, str(e)) return HttpResponse(json.dumps(result))
def delete_plan(request, access_token, planid): result = login_auth(access_token) if result['err']['code'] != 0: return HttpResponse(json.dumps(result)) userid = result['data']['id'] try: plan = Plan.objects.get(id__exact=planid) if get_editable(userid, plan): plan.delete() result = format(0, 'delete success') else: result = format(403, 'delete permission denied') return HttpResponse(json.dumps(result)) except Exception as e: result = format(400, str(e)) return HttpResponse(json.dumps(result))
def list_plans(request, access_token, visible_type=""): result = login_auth(access_token) if result['err']['code'] != 0: return HttpResponse(json.dumps(result)) userid = result['data']['id'] try: if visible_type == 'all' or visible_type == "": planlist = get_available_plans(userid) elif visible_type == 'mine': planlist = get_my_plans(userid) elif visible_type == 'joined': planlist = get_joined_plans(userid) else: result = format(400, 'invalid parameter') return HttpResponse(json.dumps(result)) result = format(0, 'success', planlist) return HttpResponse(json.dumps(result)) except Exception as e: result = format(400, str(e)) return HttpResponse(json.dumps(result))
def list_plans(request, access_token, visible_type=""): result = login_auth(access_token) if result['err']['code'] != 0: return HttpResponse(json.dumps(result)) userid = result['data']['id'] try: if visible_type == 'all' or visible_type == "": planlist = get_available_plans(userid) elif visible_type == 'mine': planlist = get_my_plans(userid) elif visible_type == 'joined': planlist = get_joined_plans(userid) else: result = format(400, 'invalid parameter') return HttpResponse(json.dumps(result)) result = format(0, 'success', planlist) return HttpResponse(json.dumps(result)) except Exception as e: result = format(400, str(e)) return HttpResponse(json.dumps(result))
def join_plan(request, access_token, planid): result = login_auth(access_token) if result['err']['code'] != 0: return HttpResponse(json.dumps(result)) userid = result['data']['id'] try: plan = Plan.objects.get(id__exact=planid) if get_joinable(userid, plan): newjoin = JoinedPlan() user = FBUser.objects.get(fbid=userid) newjoin.joined_user = user newjoin.joined_plan = plan newjoin.full_clean() newjoin.save() result = format(0, 'join success') else: result = format(403, 'join permission denied') return HttpResponse(json.dumps(result)) except Exception as e: result = format(400, str(e)) return HttpResponse(json.dumps(result))
def join_plan(request, access_token, planid): result = login_auth(access_token) if result['err']['code'] != 0: return HttpResponse(json.dumps(result)) userid = result['data']['id'] try: plan = Plan.objects.get(id__exact=planid) if get_joinable(userid, plan): newjoin = JoinedPlan() user = FBUser.objects.get(fbid=userid) newjoin.joined_user = user newjoin.joined_plan = plan newjoin.full_clean() newjoin.save() result = format(0, 'join success') else: result = format(403, 'join permission denied') return HttpResponse(json.dumps(result)) except Exception as e: result = format(400, str(e)) return HttpResponse(json.dumps(result))
def unjoin_plan(request, access_token, planid): result = login_auth(access_token) if result['err']['code'] != 0: return HttpResponse(json.dumps(result)) userid = result['data']['id'] try: plan = Plan.objects.get(id__exact=planid) print plan print userid r = JoinedPlan.objects.filter(joined_user__exact=userid, joined_plan=plan) if len(r) == 0: result = format(403, 'unjoin permission denied') elif len(r) > 1: result = format(400, 'internal realtionship error') else: rel = r[0] rel.delete() result = format(0, 'unjoin success') return HttpResponse(json.dumps(result)) except Exception as e: result = format(400, str(e)) return HttpResponse(json.dumps(result))
def unjoin_plan(request, access_token, planid): result = login_auth(access_token) if result['err']['code'] != 0: return HttpResponse(json.dumps(result)) userid = result['data']['id'] try: plan = Plan.objects.get(id__exact=planid) print plan print userid r = JoinedPlan.objects.filter(joined_user__exact=userid, joined_plan=plan) if len(r) == 0: result = format(403, 'unjoin permission denied') elif len(r) > 1: result = format(400, 'internal realtionship error') else: rel = r[0] rel.delete() result = format(0, 'unjoin success') return HttpResponse(json.dumps(result)) except Exception as e: result = format(400, str(e)) return HttpResponse(json.dumps(result))
def add_plan(request, access_token): result = login_auth(access_token) if result['err']['code'] != 0: return HttpResponse(json.dumps(result)) userid = result['data']['id'] try: new_plan = Plan() user = FBUser.objects.get(fbid=userid) new_plan.holder = user new_plan.title = request.POST.get('title', "testtitle") new_plan.destination = request.POST.get('destination', "testdestination") new_plan.description = request.POST.get('description', "testdescription") new_plan.depart_time = request.POST.get('depart_time', datetime.today()) new_plan.length = request.POST.get('length', 2) new_plan.limit = request.POST.get('limit', 2) visible_type = request.POST.get('visible_type', 1) new_plan.visible_type = int(visible_type) friend_list = request.POST.getlist('friendlist', []) new_plan.full_clean() new_plan.save() if new_plan.visible_type == 3: for friendid in friend_list: friend = FBUser.objects.get(fbid=friendid) private = PrivatePlan() private.accessible_user = friend private.accessible_plan = new_plan private.full_clean() private.save() result = format(0, 'create success') return HttpResponse(json.dumps(result)) except Exception as e: result = format(400, str(e)) return HttpResponse(json.dumps(result))
def auth(request, access_token): avatar = request.POST.get('avatar', "") result = login_auth(access_token, avatar) return HttpResponse(json.dumps(result))
def auth(request, access_token): avatar = request.POST.get('avatar', "") result = login_auth(access_token, avatar) return HttpResponse(json.dumps(result))