def get(self): def _simple_error(message, code=400): self.error(code) self.response.out.write( simplejson.dumps({ 'status': 'error', 'error': { 'message': message }, 'results': [] })) return None self.response.headers['Content-Type'] = 'application/json' center = geotypes.Point(float(self.request.get('lat')), float(self.request.get('lon'))) max_results = 100 if self.request.get('maxresults'): max_results = int(self.request.get('maxresults')) max_distance = 80000 # 80 km ~ 50 mi if self.request.get('maxdistance'): max_distance = float(self.request.get('maxdistance')) try: # Can't provide an ordering here in case inequality filters are used. base_query = ShopProduct.all() results = ShopProduct.proximity_fetch(base_query, center, max_results=max_results, max_distance=max_distance) results_obj = [{ 'lat': result.location.lat, 'lng': result.location.lon, } for result in results] self.response.out.write( simplejson.dumps({ 'status': 'success', 'results': results_obj })) except: return _simple_error(str(sys.exc_info()[1]), code=500)
def edit(self, *args): if self.params.key: item = ShopProduct.get(self.params.key) if item: return {'op':'update', 'ShopProductForm': ShopProductForm(instance=item)} else: self.status = 'ShopProduct does not exists' self.redirect(ShopProductController.get_url()) else: return {'op':'insert' ,'ShopProductForm':ShopProductForm()}
def get(self): def _simple_error(message, code=400): self.error(code) self.response.out.write(simplejson.dumps({ 'status': 'error', 'error': { 'message': message }, 'results': [] })) return None self.response.headers['Content-Type'] = 'application/json' center = geotypes.Point(float(self.request.get('lat')), float(self.request.get('lon'))) max_results = 100 if self.request.get('maxresults'): max_results = int(self.request.get('maxresults')) max_distance = 80000 # 80 km ~ 50 mi if self.request.get('maxdistance'): max_distance = float(self.request.get('maxdistance')) try: # Can't provide an ordering here in case inequality filters are used. base_query = ShopProduct.all() results = ShopProduct.proximity_fetch( base_query, center, max_results=max_results, max_distance=max_distance) results_obj = [ { 'lat': result.location.lat, 'lng': result.location.lon, } for result in results] self.response.out.write(simplejson.dumps({ 'status': 'success', 'results': results_obj })) except: return _simple_error(str(sys.exc_info()[1]), code=500)
def delete(self,*args): if self.params.key: item = ShopProduct.get(self.params.key) if item: item.delete() self.status ='ShopProduct is deleted!' else: self.status='ShopProduct does not exist' else: self.status = 'Key was not Provided!' self.redirect(ShopProductController.get_url())
def delete(self, *args): if self.params.key: item = ShopProduct.get(self.params.key) if item: item.delete() self.status = 'ShopProduct is deleted!' else: self.status = 'ShopProduct does not exist' else: self.status = 'Key was not Provided!' self.redirect(ShopProductController.get_url())
def edit(self, *args): if self.params.key: item = ShopProduct.get(self.params.key) if item: return { 'op': 'update', 'ShopProductForm': ShopProductForm(instance=item) } else: self.status = 'ShopProduct does not exists' self.redirect(ShopProductController.get_url()) else: return {'op': 'insert', 'ShopProductForm': ShopProductForm()}
def index(self, *args): self.SetTemplate(templateName="ShopProduct_index.html") results =None index = 0; count=20 try: index = int(self.params.index) count = int(self.params.count) except: pass nextIndex = index+count; previousIndex = index<=0 and -1 or (index-count>0 and 0 or index-count) result = {'ShopProductList': ShopProduct.all().fetch(limit=count, offset=index)} result.update(locals()) return result
def save(self, *args): instance = None if self.params.key: instance = ShopProduct.get(self.params.key) form=ShopProductForm(data=self.request.POST, instance=instance) if form.is_valid(): result=form.save(commit=False) result.put() self.status = 'ShopProduct is saved' self.redirect(ShopProductController.get_url()) else: self.SetTemplate(templateName = 'ShopProduct_edit.html') self.status = 'Form is not Valid' return {'op':'update', 'ShopProductForm': form}
def save(self, *args): instance = None if self.params.key: instance = ShopProduct.get(self.params.key) form = ShopProductForm(data=self.request.POST, instance=instance) if form.is_valid(): result = form.save(commit=False) result.put() self.status = 'ShopProduct is saved' self.redirect(ShopProductController.get_url()) else: self.SetTemplate(templateName='ShopProduct_edit.html') self.status = 'Form is not Valid' return {'op': 'update', 'ShopProductForm': form}
def index(self, *args): self.SetTemplate(templateName="ShopProduct_index.html") results = None index = 0 count = 20 try: index = int(self.params.index) count = int(self.params.count) except: pass nextIndex = index + count previousIndex = index <= 0 and -1 or (index - count > 0 and 0 or index - count) result = { 'ShopProductList': ShopProduct.all().fetch(limit=count, offset=index) } result.update(locals()) return result