def noticeDetailResource(request, notice_id=1): notice_id = int(notice_id) if request.method == 'GET': #try: # n = Notice.objects.get(id=notice_id) # print n # return toJson(serialize(n)) #except: # raise Http404 n = Notice.objects.filter(id=notice_id) print n #return toJson(serialize(n)) elif request.method == 'POST': post_json_data = json.loads(request.raw_post_data) try: n = Notice( image=post_json_data['image'], ) n.save() return toJson([{'status':'success'}]) except: return toJson([{'status':'fail'}]) elif request.method == 'DELETE': try: print 'deleted' except: raise Http404 return HttpResponse('/noticeDetailResource')
def memberResource(request): if request.method == 'GET': members = Member.objects.all() return toJson(serialize(members)) elif request.method == 'POST': post_json_data = json.loads(request.raw_post_data) print 'memberResource : ', post_json_data try: u = User.objects.create_user( username=post_json_data['username'], password=post_json_data['password'], email=post_json_data['username'] ) u = User.objects.get(username=post_json_data['username']) g = Group.objects.get(id=post_json_data['group_id']) m = Member( user=u, group=g, nickname=post_json_data['nickname'], image=post_json_data['image'], sex=post_json_data['sex'] ) m.save() return toJson([{'status':'success', "message":"member joined success"}]) except: return toJson([{'status':'fail', "message":"member joined fail"}])
def menuResource(request, menu_id=1): menu_id = int(menu_id) if request.method == 'GET': try: menus = Menu.objects.all() print menus return toJson(serialize(menus)) except: raise Http404 elif request.method == 'POST': post_json_data = json.loads(request,raw_post_data) print post_json_data try: i = Menu.objects.get(id=post_json_data['user_id']) m = Menu( id=i, name=post_json_data['name'], category=post_json_data['name'], desc=post_json_data['desc'], cost=post_json_data['cost'], ) m.save() return toJson({'status':'create success'}) except: return toJson({'status':'create fail'}, 400) elif request.method == 'DELETE': print 'deleted' return HttpResponse('Func:menuResource')
def upload_file(request): if request.method == 'POST': form = UploadFileForm(request.FILES) print form.is_valid() if form.is_valid(): print 'valid' HandleUploadedFile(request.FILES['file']) return toJson({'status':'success'}) else: form = UploadFileForm() return toJson({'status':'fail'})
def UserFileHandler(request): if request.method == 'POST': if 'file' in request.FILES: file = request.FILES['file'] filename = file._name dirname = settings.STATICFILES_DIRS[1] #logger.debug(filename + ' upload') fp = open('%s//%s' % (dirname, filename), 'wb') for chunk in file.chunks(): fp.write(chunk) fp.close() return toJson([{'status':'success'}]) return toJson([{'status':'fail'}])
def couponDetailResource(request, coupon_id=1): coupon_id = int(coupon_id) if request.method == 'GET': try: c = Coupon.objects.filter(id=coupon_id) return toJson(serialize(c)) except: raise Http404
def login(request): if request.method == 'POST': post_json_data = json.loads(request.raw_post_data) try: username=post_json_data['username'] password=post_json_data['password'] user = authenticate(username=username, password=password) if user is not None: if user.is_active: m = Member.objects.get(user=user) print '[login] user is logined : %s' % (m) return toJson(serialize(m)) except: return toJson([{'status':'fail', 'message':'login fail'}]) return toJson([{'status':'fail', 'message':'login fail'}])
def groupResource(request): if request.method == "GET": try: groups = Group.objects.all() return toJson(serialize(groups)) except: raise Http404 elif request.method == "POST": post_json_data = json.loads(request.raw_post_data) try: g = Group(name=post_json_data["name"]) g.save() return toJson([{"status": "success"}]) except: return toJson([{"status": "fail"}]) elif request.method == "DELETE": print "DELETE method" return HttpResponse("Func:groupResource")
def memberDetailResource(request, user_id=1): user_id = int(user_id) if request.method == 'GET': try: u = User.objects.get(id=user_id) m = Member.objects.filter(user=u) return toJson(serialize(m)) except: raise Http404
def userCheckinResource(request, user_id=1): user_id = int(user_id) if request.method == 'GET': try: u = User.objects.get(id=user_id) ch = Checkin.objects.filter(user=u) return toJson(serialize(ch)) except: raise Http404
def couponResource(request): if request.method == 'GET': try: coupons = Coupon.objects.all() return toJson(serialize(coupons)) except: raise Http404 elif request.method == 'POST': post_json_data = json.loads(request.raw_post_data) try: c = Coupon( title=post_json_data['title'], content=post_json_data['content'], end=post_json_data['end'], ) c.save() return toJson([{'status':'create success'}]) except: return toJson([{'status':'create fail'}], 400)
def noticeResource(request): #print request if request.method == 'GET': #try: # notices = Notice.objects.all() # return toJson(serialize(notices)) #except: # raise Http404 notices = Notice.objects.all() pages = Paginator(notices, 3) pno = 1 try: pno = request.GET['pno'] if int(pno) > pages.num_pages: return toJson([]) except: pass print pages.page(pno) return toJson(serialize(pages.page(pno).object_list))
def articlePostResource(request): if request.method == "POST": post_json_data = json.loads(request.raw_post_data) print "[articlePostResource]", post_json_data try: u = User.objects.get(id=int(post_json_data["user_id"])) g = Group.objects.get(id=int(post_json_data["group_id"])) a = Article( user=u, group=g, title=post_json_data["title"], content=post_json_data["content"], image=post_json_data["image"], ) a.save() return toJson([{"status": "success", "message": "article upload success"}]) except: return toJson([{"status": "fail", "message": "artile upload fail"}]) else: raise toJson([{"status": "fail"}])
def articleDetailResource(request, group_id=1, article_id=1): group_id = int(group_id) article_id = int(article_id) if request.method == "GET": try: g = Group.objects.get(id=group_id) a = Article.objects.filter(id=article_id) print a return toJson(serialize(a)) except: raise Http404
def articleResource(request, group_id=1): group_id = int(group_id) if request.method == "GET": try: g = Group.objects.get(id=group_id) a = Article.objects.filter(group=g).order_by("-created") pages = Paginator(a, 10) pno = 1 try: pno = request.GET["pno"] if int(pno) > pages.num_pages: return toJson([]) except: pass return toJson(serialize(pages.page(pno).object_list)) except: raise Http404
def checkinResource(request): if request.method == 'GET': try: checkins = Checkin.objects.all() return toJson(serialize(checkins)) except: return Http404 elif request.method == 'POST': post_json_data = json.loads(request.raw_post_data) try: u = User.objects.get(id=post_json_data['user_id']) ch = Checkin( user=u, ) ch.save() return toJson([{'status':'success', 'message':'checkin success'}]) except: return toJson([{'status':'fail'}], 400)
def FileHandler(request): if request.method == 'POST': if 'file' in request.FILES: file = request.FILES['file'] filename = file._name dirname = settings.STATICFILES_DIRS[0] #logger.debug(filename + ' upload') fp = open('%s//%s' % (dirname, filename), 'wb') for chunk in file.chunks(): fp.write(chunk) fp.close() #thumbnail 추출 image = Image.open('%s//%s' % (dirname, filename)) if image.mode not in ("L", "RGB"): image = image.convert("RGB") image.thumbnail((200, 200), Image.ANTIALIAS) image.save('%s//thumbnail_200_200_%s' % (dirname, filename), 'JPEG', quality=75) return toJson([{'status':'success'}]) return toJson([{'status':'fail'}])
def menuDetailResource(request, menu_id=1): menu_id = int(menu_id) if request.method == 'GET': try: m = Menu.objects.filter(id=menu_id) print m return toJson(serialize(m)) except: raise Http404 elif request.method == 'PUT': pass elif request.method == 'DELETE': print 'deleted' return HttpResponse('Func:menuDetailResource')
def logout(request): try: logout(request) except: return toJson({'status':'logout fail'}) return toJson({'status':'logout success'})