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 _create_plan(self, **kwargs): opts = { 'name': 'test-plan', 'available': True, 'visible': True, 'created': datetime.now() } opts.update(kwargs) p = Plan(**opts) p.full_clean() p.save() return p
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))