def post(self, request): try: data = JSONParser().parse(request) data = data.get(PARAMETER_DATA) except JSONDecodeError: raise ParseError(detail="No data found on the request") ''' check all values ''' catid = WebRequestParameter.get_data(PARAMETER_CATEGORY, data) question = WebRequestParameter.get_data(PARAMETER_QUESTION, data) options = WebRequestParameter.get_data(PARAMETER_OPTIONS, data) answer = WebRequestParameter.get_data(PARAMETER_ANSWER, data) if not all( (catid, question, options, answer ) ): raise ParseError(detail="Some parameters are missing from request") ''' search for category ''' category = Category.objects(catid = int(catid)).first() if category is None: raise APIException("Category not exists!") try: self.add_question(category, question, options, answer) except: traceback.print_exc() raise APIException("Unable to add question. Try again later!") return Response(True)
def get(self, request): categories = ObjectMemoryCache.get_key(Category) if categories: categories = categories.values() else: ''' extract all categories with status 'active' true ''' categories = Category.objects(active=True) if len(categories) == 0: return Response([]) ''' put categories in memory ''' memo = ObjectMemoryCache(Category, PARAMETER_CATEGORY_ID) memo.queryset(categories) loaded, categories = memo.load(serialize=True, serializer_class=CategoryResponse) if not loaded: raise APIException(detail="Unable to load categories") return Response(categories)