def save_feed_entries(subscription, entries, host): """ Save recieved entries for a given subscription - this compares the entry's source with host if they're similar, don't save that entry. """ if subscription is None: return False if subscription.is_service: the_author = subscription.owners[0].username the_owner = subscription.owners[0] # here the fun begins: parse that feed and create entries! for entry in entries: entry.subscription = subscription entry.subscribers_usernames = [s.username for s in subscription.owners] if entry.subscription.is_service: entry.author = the_author entry.owner = the_owner entry.author_url = host+'/'+the_author #avoid the Ouroboros bug logging.info('entry id is %s' % entry.entry_id) if not entry.source.startswith(host): #is there an entry with the same id? The Lernaean Hydra bug old = Entry.objects.get_by_id(entry.entry_id) if old is not None: logging.info('entry already exists %s' % entry.entry_id) old.body = entry.body old.link = entry.link old.save() else: entry.save() # inform the hub -Todo: Try do it after confirming to the HUB that the # entries were recieved. i.e. the last HttpResponse # THINK: of tasks if entries: for subscriber in subscription.owners: post_publish.send(sender=Subscription, username=subscriber.username, host=host) # thank the hub return True
def add_entry(request): host = 'http://%s' % request.get_host() if request.method == 'POST': form =EntryForm(request.POST) if form.is_valid(): entry = form.save(commit=False) entry.subscription = None entry.owner = request.user entry.author = request.user.username entry.subscribers_usernames = [request.user.username] # let the author_url blank so it won't be hardcoded in the store and # in the template, if author_link is empty, generate a link to his # entries entry.author_url = '' entry.save() # invalidate cache clear_entries_cache(request.user.username) # inform the hub post_publish.send(sender=Entry, username=entry.author, host=host) if request.is_ajax(): e = dict( author=request.user.username, author_url = host+'/'+request.user.username, body = entry.body, link = entry.link, linktext = entry.link, key = str(entry.key()), permalink = '/show/'+str(entry.key()), editlink = '/edit/'+str(entry.key()), deletelink = '/delete/'+str(entry.key()) ) return json_response(e) else: return HttpResponseRedirect(reverse('user-entries', kwargs={'username': request.user.username})) else: form =EntryForm() return render_to_response('entry_form.html', { 'form': form })
def edit_entry(request, key): host = 'http://%s' % request.get_host() entry = db.get(key) if entry.author != request.user.username: response = HttpResponse('unauthorized') response.status_code = 401 return response if request.is_ajax(): body = request.POST.get('body') link = request.POST.get('link') entry.body = body entry.link = link entry.save() e = dict( author=request.user.username, author_url = host+'/'+request.user.username, body = entry.body, link = entry.link, linktext = entry.link, key = str(entry.key()), permalink = '/show/'+str(entry.key()), editlink = '/edit/'+str(entry.key()), deletelink = '/delete/'+str(entry.key()) ) # invalidate cache clear_entries_cache(request.user.username) # ping hub post_publish.send(sender=Entry, username=request.user.username, host=host) return json_response(e) else: response = update_object(request, object_id=key, form_class=EntryForm, post_save_redirect=reverse('user-entries', kwargs={'username':request.user.username})) if isinstance(response, HttpResponseRedirect): post_publish.send(sender=Entry, username=request.user.username, host=host) return response