def _feed_entry_read_post(request, uuid_): context = Context() context.parse_request(request) context.parse_query_dict(request.GET) read_feed_entry_user_mapping = None with transaction.atomic(): feed_entry = None try: feed_entry = models.FeedEntry.objects.get(uuid=uuid_) except models.FeedEntry.DoesNotExist: return HttpResponseNotFound('feed entry not found') try: read_feed_entry_user_mapping = models.ReadFeedEntryUserMapping.objects.get( feed_entry=feed_entry, user=request.user) except models.ReadFeedEntryUserMapping.DoesNotExist: read_feed_entry_user_mapping = models.ReadFeedEntryUserMapping.objects.create( feed_entry=feed_entry, user=request.user) ret_obj = context.format_datetime(read_feed_entry_user_mapping.read_at) content, content_type = query_utils.serialize_content(ret_obj) return HttpResponse(content, content_type)
def _user_category_get(request, uuid_): context = Context() context.parse_request(request) context.parse_query_dict(request.GET) field_maps = None try: fields = query_utils.get_fields__query_dict(request.GET) field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) user_category = None try: user_category = models.UserCategory.objects.get(uuid=uuid_, user=request.user) except models.UserCategory.DoesNotExist: return HttpResponseNotFound('user category not found') ret_obj = query_utils.generate_return_object(field_maps, user_category, context) content, content_type = query_utils.serialize_content(ret_obj) return HttpResponse(content, content_type)
def _feed_get(request): context = Context() context.parse_request(request) context.parse_query_dict(request.GET) url = request.GET.get('url') if not url: return HttpResponseBadRequest('\'url\' missing') url = url_normalize(url) field_maps = None try: fields = query_utils.get_fields__query_dict(request.GET) field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) feed = None try: feed = models.Feed.annotate_subscription_data( models.Feed.objects.all(), request.user).get(feed_url=url) except models.Feed.DoesNotExist: try: feed = _save_feed(url) except QueryException as e: return HttpResponse(e.message, status=e.httpcode) ret_obj = query_utils.generate_return_object(field_maps, feed, context) content, content_type = query_utils.serialize_content(ret_obj) return HttpResponse(content, content_type)
def _user_category_post(request): context = Context() context.parse_request(request) context.parse_query_dict(request.GET) field_maps = None try: fields = query_utils.get_fields__query_dict(request.GET) field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) if not request.body: return HttpResponseBadRequest('no HTTP body') # pragma: no cover json_ = None try: json_ = ujson.loads(request.body) except ValueError: # pragma: no cover return HttpResponseBadRequest('HTTP body cannot be parsed') if type(json_) is not dict: return HttpResponseBadRequest( 'JSON body must be object') # pragma: no cover if 'text' not in json_: return HttpResponseBadRequest('\'text\' missing') if type(json_['text']) is not str: return HttpResponseBadRequest('\'text\' must be string') user_category = models.UserCategory(user=request.user, text=json_['text']) try: user_category.save() except IntegrityError: return HttpResponse('user category already exists', status=409) ret_obj = query_utils.generate_return_object(field_maps, user_category, context) content, content_type = query_utils.serialize_content(ret_obj) return HttpResponse(content, content_type)
def _feed_entries_query_stable_create_post(request): cache = caches['stable_query'] context = Context() context.parse_request(request) context.parse_query_dict(request.GET) if not request.body: return HttpResponseBadRequest('no HTTP body') # pragma: no cover json_ = None try: json_ = ujson.loads(request.body) except ValueError: # pragma: no cover return HttpResponseBadRequest('HTTP body cannot be parsed') if type(json_) is not dict: return HttpResponseBadRequest( 'JSON body must be object') # pragma: no cover sort = None try: sort = query_utils.get_sort(json_, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) search = None try: search = query_utils.get_search(context, json_, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) token = f'feedentry-{uuid.uuid4().int}' cache.set( token, list( models.FeedEntry.annotate_search_vectors( models.FeedEntry.objects.all()).filter(*search).order_by( *sort).values_list('uuid', flat=True))) content, content_type = query_utils.serialize_content(token) return HttpResponse(content, content_type)
def _user_get(request): context = Context() context.parse_request(request) context.parse_query_dict(request.GET) user = request.user field_maps = None try: fields = query_utils.get_fields__query_dict(request.GET) field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) ret_obj = query_utils.generate_return_object(field_maps, user, context) content, content_type = query_utils.serialize_content(ret_obj) return HttpResponse(content, content_type)
def _feed_entries_query_stable_post(request): cache = caches['stable_query'] context = Context() context.parse_request(request) context.parse_query_dict(request.GET) if not request.body: return HttpResponseBadRequest('no HTTP body') # pragma: no cover json_ = None try: json_ = ujson.loads(request.body) except ValueError: # pragma: no cover return HttpResponseBadRequest('HTTP body cannot be parsed') if type(json_) is not dict: return HttpResponseBadRequest( 'JSON body must be object') # pragma: no cover token = None try: token = json_['token'] except KeyError: return HttpResponseBadRequest('\'token\' missing') if type(token) is not str: return HttpResponseBadRequest('\'token\' must be string') if re.search(r'^feedentry-\d+$', token) is None: return HttpResponseBadRequest('\'token\' malformed') count = None try: count = query_utils.get_count(json_) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) skip = None try: skip = query_utils.get_skip(json_) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) field_maps = None try: fields = query_utils.get_fields__json(json_) field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) return_objects = None try: return_objects = query_utils.get_return_objects(json_) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) return_total_count = None try: return_total_count = query_utils.get_return_total_count(json_) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) cache.touch(token) uuids = cache.get(token, []) ret_obj = {} if return_objects: current_uuids = uuids[skip:skip + count] feed_entries = { feed_entry.uuid: feed_entry for feed_entry in models.FeedEntry.objects.filter( uuid__in=current_uuids) } objs = [] if len(current_uuids) == len(feed_entries): for uuid_ in current_uuids: feed_entry = feed_entries[uuid_] obj = query_utils.generate_return_object( field_maps, feed_entry, context) objs.append(obj) ret_obj['objects'] = objs if return_total_count: ret_obj['totalCount'] = len(uuids) content, content_type = query_utils.serialize_content(ret_obj) return HttpResponse(content, content_type)
def _feed_entries_query_post(request): context = Context() context.parse_request(request) context.parse_query_dict(request.GET) if not request.body: return HttpResponseBadRequest('no HTTP body') # pragma: no cover json_ = None try: json_ = ujson.loads(request.body) except ValueError: # pragma: no cover return HttpResponseBadRequest('HTTP body cannot be parsed') if type(json_) is not dict: return HttpResponseBadRequest( 'JSON body must be object') # pragma: no cover count = None try: count = query_utils.get_count(json_) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) skip = None try: skip = query_utils.get_skip(json_) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) sort = None try: sort = query_utils.get_sort(json_, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) search = None try: search = query_utils.get_search(context, json_, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) field_maps = None try: fields = query_utils.get_fields__json(json_) field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) return_objects = None try: return_objects = query_utils.get_return_objects(json_) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) return_total_count = None try: return_total_count = query_utils.get_return_total_count(json_) except QueryException as e: # pragma: no cover return HttpResponse(e.message, status=e.httpcode) feed_entries = models.FeedEntry.annotate_search_vectors( models.FeedEntry.objects.all()).filter(*search) ret_obj = {} if return_objects: objs = [] for feed_entry in feed_entries.order_by(*sort)[skip:skip + count]: obj = query_utils.generate_return_object(field_maps, feed_entry, context) objs.append(obj) ret_obj['objects'] = objs if return_total_count: ret_obj['totalCount'] = feed_entries.count() content, content_type = query_utils.serialize_content(ret_obj) return HttpResponse(content, content_type)