def ajax_Book_asearch(request): # We support POST only . res = None #import pdb #pdb.set_trace() # This is basically a search by a tag or list items with given arguments if request.method == 'GET': return AutoHttpResponse(501) # This is basically a append to a list with given arguments elif request.method == 'POST': id = request.POST.get('id', None) try: #name = parseTriple(request.POST.get('name',None));icbn = parseTriple(request.POST.get('icbn',None));toc = parseTriple(request.POST.get('toc',None));author = parseTriple(request.POST.get('author',None));publication = parseTriple(request.POST.get('publication',None)); non_field_params = ['orderBy', 'include', 'exclude'] orderBy = request.POST.get('orderBy', None) if orderBy: orderBy = orderBy.split(',') include = request.POST.get('include', None) if include: include = include.split(',') exclude = request.POST.get('exclude', None) if exclude: exclude = exclude.split(',') #Define Query Strings. queryDict = dict(request.POST) for _x in non_field_params: if queryDict.has_key(_x): del queryDict[_x] #Now we should only have Database field. Qstr = '' for key, value in queryDict.iteritems(): if isinstance(value, str): v = parseTriple(value) if v: Qstr += query_str_builder(key, v) else: for v in value: v = parseTriple(v) if v: Qstr += query_str_builder(key, v) Qstr = Qstr[2:] except: D_LOG() return AutoHttpResponse(400, 'Wrong Pentameter format.') try: res = BookManager.advSearchBook(id=id, query_str=Qstr, orderBy=orderBy, include=include, exclude=exclude) except: D_LOG() return AutoHttpResponse( 400, 'list item is not speared properly! Is your list field looks like: tags = [1,2,3] or tag1=%5B1%2C2%2C3%5D ?' ) return AutoHttpResponse(res=res)
def searchListBook(author=[], publication=[], page=None, limit=None): try: Query = {} for x in author: Query['author__contains'] = x for x in publication: Query['publication__contains'] = x # Autogen d = Book.objects.filter(**Query) if page is not None: # doing pagination if enable. if limit is None: limit = 10 paginator = Paginator(d, limit) d = paginator.page(page) res = [model_to_dict(u) for u in d] return { 'res': res, 'status': 'info', 'msg': 'Book search returned' } except Exception, e: D_LOG() return { 'res': None, 'status': 'error', 'msg': 'Not able to search Book!', 'sys_error': str(e) }
def removeListBook( id, author=[], publication=[], ): try: res = BookManager.getBookObj(id) if res['res'] is None: return res t = res['res'] t.author = sorted( list(set(t.author) - set(author))) if author is not None else t.author t.publication = sorted( list(set(t.publication) - set(publication) )) if publication is not None else t.publication t.save() res = model_to_dict(t) return {'res': res, 'status': 'info', 'msg': 'tag added'} except Exception, e: D_LOG() return { 'res': None, 'status': 'error', 'msg': 'Not able to add tags ', 'sys_error': str(e) }
def createBook( name, icbn, toc, author, publication, ): #Crete an Obj try: t = Book( name=name, icbn=icbn, toc=toc, author=author, publication=publication, ) t.log_history = [{ 'type': 'CREATE', 'msg': 'Created new entry !', 'ts': datetime.now().strftime('%Y-%m-%d %H:%M:%S') }] t.save() return { 'res': model_to_dict(t), 'status': 'info', 'msg': 'New Book got created.' } except Exception, e: D_LOG() return { 'res': None, 'status': 'error', 'msg': 'Not able to create Book', 'sys_error': str(e) }
def getBookObj(id): #get Obj try: t = Book.objects.get(pk=id) return {'res': t, 'status': 'info', 'msg': 'Book Object returned'} except Exception, e: D_LOG() return { 'res': None, 'status': 'error', 'msg': 'Not able to retrive object Book', 'sys_error': str(e) }
def str2List(s): try: if '[' in s: return eval(s) if ',' in s: return s.split(',') else: return s.split(' ') except: D_LOG() print 'Error: eval Error: We support "[1,2,3]" or "aa,bb,cc" or "aa bb cc" to [1,2,3] Split Over , space or eval ' return []
def ajax_Book_list( request, id=None, ): res = None # This is basically a search by a tag or list items with given arguments if request.method == 'GET': return AutoHttpResponse(501) # This is basically a append to a list with given arguments elif request.method == 'POST': action = request.POST.get('action', None) if action not in ['APPEND', 'REMOVE', 'SEARCH']: return AutoHttpResponse( 400, 'id missing ! your post data must have action = APPEND or REMOVE or SEARCH ?' ) if not id and action != 'SEARCH': return AutoHttpResponse( 400, 'id missing ! is your urls looks like http://192.168.56.101:7777/api/Author/1/list/ ?' ) try: author = eval(request.POST.get('author', '[]')) publication = eval(request.POST.get('publication', '[]')) if action == 'APPEND': res = BookManager.appendListBook( id, author=author, publication=publication, ) elif action == 'REMOVE': res = BookManager.removeListBook( id, author=author, publication=publication, ) elif action == 'SEARCH': res = BookManager.searchListBook( author=author, publication=publication, ) except: D_LOG() return AutoHttpResponse( 400, 'list item is not speared properly! Is your list field looks like: tags = [1,2,3] or tag1=%5B1%2C2%2C3%5D ?' ) #Return the result after converting into json return HttpResponse(json.dumps(res, default=json_util.default), content_type='application/json')
def deleteBook(id): #Delete Obj try: d = Book.objects.get(pk=id) d.delete() return {'res': d, 'status': 'info', 'msg': 'one Book deleted!'} except Exception, e: D_LOG() return { 'res': None, 'status': 'error', 'msg': 'Not able to delete Book!', 'sys_error': str(e) }
def updateBook( id, name, icbn, toc, author, publication, ): #Update Obj try: res = BookManager.getBookObj(id) if res['res'] is None: return res t = res['res'] changes = '' changes += str('update name:' + str(t.name) + ' to ' + str(name) + ' ;') if name is not None else '' changes += str('update icbn:' + str(t.icbn) + ' to ' + str(icbn) + ' ;') if icbn is not None else '' changes += str('update toc:' + str(t.toc) + ' to ' + str(toc) + ' ;') if toc is not None else '' changes += str('update author:' + str(t.author) + ' to ' + str(author) + ' ;') if author is not None else '' changes += str('update publication:' + str(t.publication) + ' to ' + str(publication) + ' ;') if publication is not None else '' t.log_history.append({ 'type': 'UPDATE', 'msg': changes, 'ts': datetime.now().strftime('%Y-%m-%d %H:%M:%S') }) t.name = name if name is not None else t.name t.icbn = icbn if icbn is not None else t.icbn t.toc = toc if toc is not None else t.toc t.author = author if author is not None else t.author t.publication = publication if publication is not None else t.publication t.save() return { 'res': model_to_dict(t), 'status': 'info', 'msg': 'Book Updated' } except Exception, e: D_LOG() return { 'res': None, 'status': 'error', 'msg': 'Not able to update Book', 'sys_error': str(e) }
def getBook(id): # get Json try: t = Book.objects.get(pk=id) res = model_to_dict(t) if res is not None: pass return {'res': res, 'status': 'info', 'msg': 'Book returned'} except Exception, e: D_LOG() return { 'res': None, 'status': 'error', 'msg': 'Not Able to retrive Book', 'sys_error': str(e) }
def advSearchBook(id, query_str, page=None, limit=None, orderBy=None, include=None, exclude=None): try: Qstr = query_str print "===>ADVANCE QUERY EXECUTED AS :", Qstr if Qstr: try: Qstr = eval(Qstr) except Exception, e: D_LOG() return { 'res': None, 'status': 'error', 'msg': 'Book Opps!, The Query is not valid as you made some syntax error ', 'sys_error': str(e) } if Qstr: d = Book.objects.filter(Qstr) else: d = Book.objects.filter() #Oder_by Here. if orderBy: d = d.order_by(*orderBy) if page is not None: # doing pagination if enable. if limit is None: limit = 10 paginator = Paginator(d, limit) d = paginator.page(page) #Selecting fields. if include: res = list(d.values(*include)) else: res = [model_to_dict(u) for u in d] #res = d.values() # Dont RUN this . return { 'res': res, 'status': 'info', 'msg': 'Book search returned' }
def searchBook(name, icbn, toc, author, publication, page=None, limit=None, id=None): # Simple Serach try: Query = {} if id is not None: Query['id'] = id if name is not None: Query['name__contains'] = name if icbn is not None: Query['icbn'] = icbn if toc is not None: Query['toc'] = toc if author is not None: Query['author'] = author if publication is not None: Query[ 'publication'] = publication #if state is not None: Query['state_contains']=state d = Book.objects.filter(**Query) if page is not None: # doing pagination if enable. if limit is None: limit = 10 paginator = Paginator(d, limit) d = paginator.page(page) res = [model_to_dict(u) for u in d] return { 'res': res, 'status': 'info', 'msg': 'Book search returned' } except Exception, e: D_LOG() return { 'res': None, 'status': 'error', 'msg': 'Not able to search Book!', 'sys_error': str(e) }
else: d = Book.objects.filter() #Oder_by Here. if orderBy: d = d.order_by(*orderBy) if page is not None: # doing pagination if enable. if limit is None: limit = 10 paginator = Paginator(d, limit) d = paginator.page(page) #Selecting fields. if include: res = list(d.values(*include)) else: res = [model_to_dict(u) for u in d] #res = d.values() # Dont RUN this . return { 'res': res, 'status': 'info', 'msg': 'Book search returned' } except Exception, e: D_LOG() return { 'res': None, 'status': 'error', 'msg': 'Not able to search Book!', 'sys_error': str(e) }
def ajax_Book(request, id=None): res = None #If the request is coming for get .. if request.method == 'GET': page = request.GET.get('page', None) limit = request.GET.get('limit', None) name = request.GET.get('name', None) icbn = request.GET.get('icbn', None) toc = request.GET.get('toc', None) author = request.GET.get('author', None) publication = request.GET.get('publication', None) #data Must be Normalized to required DataType.. try: name = str(name) if (name) else name icbn = int(icbn) if (icbn) else icbn toc = dict(toc) if (toc) else toc author = str2List(author) if (author) else author publication = str2List(publication) if ( publication) else publication except: D_LOG() return AutoHttpResponse( 400, 'Type mismatch!you might be trying to enter Wrong datatype') # if Id is null, get the perticular Book or it's a search request if id is not None: res = BookManager.getBook(id) else: # General Search request id = request.GET.get('id', None) # We also support search based on ID. res = BookManager.searchBook( name=name, icbn=icbn, toc=toc, author=author, publication=publication, id=id, page=page, limit=limit, ) #This is the implementation for POST request. elif request.method == 'POST': name = request.POST.get('name', None) icbn = request.POST.get('icbn', None) toc = request.POST.get('toc', None) author = request.POST.get('author', None) publication = request.POST.get('publication', None) #data Must be Normalized to required DataType.. try: name = str(name) if (name) else name icbn = int(icbn) if (icbn) else icbn toc = dict(toc) if (toc) else toc author = str2List(author) if (author) else author publication = str2List(publication) if ( publication) else publication except: D_LOG() return AutoHttpResponse( 400, 'Type mismatch!you might be trying to enter Wrong datatype') # Update request if id is not null. if id is not None: res = BookManager.updateBook( id=id, name=name, icbn=icbn, toc=toc, author=author, publication=publication, ) else: # This is new entry request... res = BookManager.createBook( name=name, icbn=icbn, toc=toc, author=author, publication=publication, ) # This is a Delete Request.. elif request.method == 'DELETE' and id is not None: res = BookManager.deleteBook(id) #Return the result after converting into json return HttpResponse(json.dumps(res, default=json_util.default), content_type='application/json')