def api_feed_pks_get(request): """ JSON API to get entry pks for given feeds Arguments passed on GET: feed_pks List of feeds to get entry pks for about If none, returns entry pks for all feeds state The state of entries to read order The order to sort entries in Defaults to ORDER_DESC Returns in JSON format: success Boolean indicating success pks Object with feed pk as key, list of entry pks as list value feed_unread Unread counts as dict, { feed.pk: feed.count_unread, ... } """ feed_pks = request.GET.get('feed_pks', '') state = GET_state(request, 'state') order = request.GET.get('order', ORDER_DESC) # Get entries queryset, filtered by user and feed entries = models.Entry.objects.filter(feed__user=request.user) if feed_pks: try: entries = entries.filter(feed__pk__in=feed_pks.split(',')) except Exception: return utils.jsonResponse({ 'success': False, 'msg': 'Invalid request', }) # Filter by state if state == ENTRY_UNREAD: entries = entries.unread() elif state == ENTRY_READ: entries = entries.read() elif state == ENTRY_SAVED: entries = entries.saved() # Order them if order == ORDER_ASC: entries = entries.order_by('date') else: entries = entries.order_by('-date') # Get a list of remaining pks pks = list(entries.values_list('pk', flat=True)) # Get unread counts for feeds in this response feed_unread = {} for feed in entries.feeds(): feed_unread[str(feed.pk)] = feed.count_unread # Respond return utils.jsonResponse({ 'success': True, 'pks': pks, 'feed_unread': feed_unread, })
def api_entry_get(request, template="yarr/include/entry.html"): """ JSON API to get entry data Returns pre-rendered data using the entry template Arguments passed on GET: entry_pks List of entries to get """ # Get entries queryset pks = request.GET.get("entry_pks", "") if pks: success = True entries = models.Entry.objects.filter(feed__user=request.user, pk__in=pks.split(",")) else: success = False entries = models.Entry.objects.none() # Render rendered = [] compiled = loader.get_template(template) for entry in entries: rendered.append(compiled.render(Context({"entry": entry}))) # Respond return utils.jsonResponse({"success": success, "entries": rendered})
def api_entry_set(request): """ JSON API to set entry data Arguments passed on GET: entry_pks List of entries to update op Operation to perform ``read`` Change read flag ``saved`` Change saved flag is_read New value of read flag, if ``op=read`` (else ignored) Format: ``is_read=true`` or ``is_read==false`` is_saved New value of saved flag, if ``op=saved`` (else ignored) Format: ``is_saved=true`` or ``is_saved==false`` """ # Start assuming the worst success = False msg = 'Unknown operation' # Get entries queryset pks = request.GET.get('entry_pks', '').split(',') if pks: success = True entries = models.Entry.objects.filter( feed__user=request.user, pk__in=pks, ) else: success = False entries = models.Entry.objects.none() # Get operation op = request.GET.get('op', None) # Update flags if op == 'read': is_read = request.GET.get('is_read', 'true') == 'true' entries.update( read = is_read, saved = False, ) entries.update_feed_unread() success = True msg = 'Marked as %s' % ('read' if is_read else 'unread') elif op == 'saved': is_saved = request.GET.get('is_saved', 'true') == 'true' entries.update( saved = is_saved, read = False, ) success = True msg = 'Saved' if is_saved else 'No longer saved' # Respond return utils.jsonResponse({ 'success': success, 'msg': msg, })
def api_entry_get(request, template="yarr/include/entry.html"): """ JSON API to get entry data Arguments passed on GET: entry_pks List of entries to get order Order to send them back in Defaults to ORDER_DESC Returns in JSON format: success Boolean indicating success entries List of entries, rendered entry as object in value: html Entry rendered as HTML using template """ pks = request.GET.get('entry_pks', '') order = request.GET.get('order', ORDER_DESC) # Get entries queryset if pks: success = True entries = models.Entry.objects.filter( feed__user=request.user, pk__in=pks.split(','), ) else: success = False entries = models.Entry.objects.none() # Order them if order == ORDER_ASC: entries = entries.order_by('date') else: entries = entries.order_by('-date') # Render data = [] compiled = loader.get_template(template) for entry in entries: data.append({ 'pk': entry.pk, 'feed': entry.feed_id, 'state': entry.state, 'html': compiled.render(Context({ 'constants': constants, 'entry': entry, })) }) # Respond return utils.jsonResponse({ 'success': success, 'entries': data, })
def api_entry_set(request): """ JSON API to set entry data Arguments passed on GET: entry_pks List of entries to update op Operation to perform ``read`` Change read flag ``saved`` Change saved flag is_read New value of read flag, if ``op=read`` (else ignored) Format: ``is_read=true`` or ``is_read==false`` is_saved New value of saved flag, if ``op=saved`` (else ignored) Format: ``is_saved=true`` or ``is_saved==false`` """ # Start assuming the worst success = False msg = "Unknown operation" # Get entries queryset pks = request.GET.get("entry_pks", "").split(",") if pks: success = True entries = models.Entry.objects.filter(feed__user=request.user, pk__in=pks) else: success = False entries = models.Entry.objects.none() # Get operation op = request.GET.get("op", None) # Update flags if op == "read": is_read = request.GET.get("is_read", "true") == "true" entries.update(read=is_read, saved=False) entries.update_feed_unread() success = True msg = "Marked as %s" % ("read" if is_read else "unread") elif op == "saved": is_saved = request.GET.get("is_saved", "true") == "true" entries.update(saved=is_saved, read=False) success = True msg = "Saved" if is_saved else "No longer saved" # Respond return utils.jsonResponse({"success": success, "msg": msg})
def api_feed_get(request): """ JSON API to get feed data Arguments passed on GET: feed_pks List of feeds to get information about fields List of model fields to get If not provided, returns all fields Excluded fields: id, user, all related fields The pk (id) is provided as the key """ # Get feeds queryset pks = request.GET.get("feed_pks", "") if pks: success = True feeds = models.Feed.objects.filter(user=request.user, pk__in=pks.split(",")) else: success = False feeds = models.Feed.objects.none() # Get safe list of attributes fields_available = [field.name for field in models.Feed._meta.fields if field.name not in ["id", "user"]] fields_request = request.GET.get("fields", "") if fields_request: fields = [field_name for field_name in fields_request.split(",") if field_name in fields_available] else: fields = fields_available # Prep list of safe fields which don't need to be escaped safe_fields = [ field.name for field in models.Feed._meta.fields if (field.name in fields and isinstance(field, (django_models.DateTimeField, django_models.IntegerField))) ] # Get data data = {} for feed in feeds.values("pk", *fields): # Escape values as necessary, and add to the response dict under the pk data[feed.pop("pk")] = dict([(key, val if key in safe_fields else escape(val)) for key, val in feed.items()]) # Respond return utils.jsonResponse({"success": success, "feeds": data})
def api_entry_set(request): """ JSON API to set entry data Arguments passed on GET: entry_pks List of entries to update state New state Returns in JSON format: success Boolean msg Error message, if success == False feed_unread Unread counts as dict, { feed.pk: feed.count_unread, ... } """ # Start assuming success success = True msg = '' feed_unread = {} # Get entries queryset pks = request.GET.get('entry_pks', '') if pks: pks = pks.split(',') entries = models.Entry.objects.filter( feed__user=request.user, pk__in=pks, ) else: success = False msg = 'No entries found' # Check for if_state if_state = GET_state(request, 'if_state') if success and if_state is not None: if_state = int(if_state) if if_state == ENTRY_UNREAD: entries = entries.unread() elif if_state == ENTRY_READ: entries = entries.read() elif if_state == ENTRY_SAVED: entries = entries.saved() else: success = False msg = 'Unknown condition' # Update new state state = GET_state(request, 'state') if success: if state in (ENTRY_UNREAD, ENTRY_READ, ENTRY_SAVED): # Change state and get updated unread count feed_unread = entries.set_state(state, count_unread=True) # If they're not marked as read, they can't ever expire # If they're marked as read, they will be given an expiry date # when Feed._update_entries determines they can expire if state != ENTRY_READ: entries.clear_expiry() # Decide message if state == ENTRY_UNREAD: msg = 'Marked as unread' elif state == ENTRY_READ: msg = 'Marked as read' elif state == ENTRY_SAVED: msg = 'Saved' else: success = False msg = 'Unknown operation' # Respond return utils.jsonResponse({ 'success': success, 'msg': msg, 'feed_unread': feed_unread, })
def api_feed_get(request): """ JSON API to get feed data Arguments passed on GET: feed_pks List of feeds to get information about fields List of model fields to get If not provided, returns all fields Excluded fields: id, user, all related fields The pk (id) is provided as the key Returns in JSON format: success Boolean indicating success feeds Object with feed pk as key, feed data as object in value """ # Get feeds queryset pks = request.GET.get('feed_pks', '') if pks: success = True feeds = models.Feed.objects.filter( user=request.user, pk__in=pks.split(','), ) else: success = False feeds = models.Feed.objects.none() # Get safe list of attributes fields_available = [ field.name for field in models.Feed._meta.fields if field.name not in ['id', 'user'] ] fields_request = request.GET.get('fields', '') if fields_request: fields = [ field_name for field_name in fields_request.split(',') if field_name in fields_available ] else: fields = fields_available # Prep list of safe fields which don't need to be escaped safe_fields = [ field.name for field in models.Feed._meta.fields if (field.name in fields and isinstance(field, ( django_models.DateTimeField, django_models.IntegerField, ))) ] # Get data data = {} for feed in feeds.values('pk', *fields): # Escape values as necessary, and add to the response dict under the pk data[feed.pop('pk')] = dict([ (key, val if key in safe_fields else escape(val)) for key, val in list(feed.items()) ]) # Respond return utils.jsonResponse({ 'success': success, 'feeds': data, })
def api_feed_get(request): """ JSON API to get feed data Arguments passed on GET: feed_pks List of feeds to get information about fields List of model fields to get If not provided, returns all fields Excluded fields: id, user, all related fields The pk (id) is provided as the key Returns in JSON format: success Boolean indicating success feeds Object with feed pk as key, feed data as object in value """ # Get feeds queryset pks = request.GET.get('feed_pks', '') if pks: success = True feeds = models.Feed.objects.filter( user=request.user, pk__in=pks.split(','), ) else: success = False feeds = models.Feed.objects.none() # Get safe list of attributes fields_available = [ field.name for field in models.Feed._meta.fields if field.name not in [ 'id', 'user' ] ] fields_request = request.GET.get('fields', '') if fields_request: fields = [ field_name for field_name in fields_request.split(',') if field_name in fields_available ] else: fields = fields_available # Prep list of safe fields which don't need to be escaped safe_fields = [ field.name for field in models.Feed._meta.fields if ( field.name in fields and isinstance(field, ( django_models.DateTimeField, django_models.IntegerField, )) ) ] # Get data data = {} for feed in feeds.values('pk', *fields): # Escape values as necessary, and add to the response dict under the pk data[feed.pop('pk')] = dict([ (key, val if key in safe_fields else escape(val)) for key, val in feed.items() ]) # Respond return utils.jsonResponse({ 'success': success, 'feeds': data, })