def home_tab(request, tab): ''' user's homepage with activity feed ''' shelves = [] for identifier in ['reading', 'to-read']: shelf = models.Shelf.objects.get( user=request.user, identifier=identifier, ) if not shelf.books.count(): continue shelves.append({ 'name': shelf.name, 'identifier': shelf.identifier, 'books': shelf.books.all()[:3], 'size': shelf.books.count(), }) # allows us to check if a user has shelved a book user_books = models.Book.objects.filter(shelves__user=request.user).all() # books new to the instance, for discovery recent_books = models.Book.objects.order_by('-created_date')[:5] # status updates for your follow network following = models.User.objects.filter( Q(followers=request.user) | Q(id=request.user.id)) activities = models.Status.objects.select_subclasses().order_by( '-created_date') if tab == 'home': # people you follow and direct mentions activities = activities.filter( Q(user__in=following, privacy='public') | \ Q(mention_users=request.user) ) elif tab == 'local': # everyone on this instance activities = activities.filter(user__local=True, privacy='public') else: # all activities from everyone you federate with activities = activities.filter(privacy='public') activities = activities[:10] comment_form = forms.CommentForm() data = { 'user': request.user, 'shelves': shelves, 'recent_books': recent_books, 'user_books': user_books, 'activities': activities, 'feed_tabs': ['home', 'local', 'federated'], 'active_tab': tab, 'comment_form': comment_form, } return TemplateResponse(request, 'feed.html', data)
def comment(request): ''' respond to a book review ''' form = forms.CommentForm(request.POST) # this is a bit of a formality, the form is just one text field if not form.is_valid(): return redirect('/') parent_id = request.POST['parent'] parent = models.Status.objects.get(id=parent_id) outgoing.handle_comment(request.user, parent, form.data['content']) return redirect('/')
def comment(request): ''' create a book comment ''' form = forms.CommentForm(request.POST) book_identifier = request.POST.get('book') # TODO: better failure behavior if not form.is_valid(): return redirect('/book/%s' % book_identifier) # TODO: validation, htmlification content = form.data.get('content') outgoing.handle_comment(request.user, book_identifier, content) return redirect('/book/%s' % book_identifier)
def home_tab(request, tab): ''' user's homepage with activity feed ''' page_size = 15 try: page = int(request.GET.get('page', 1)) except ValueError: page = 1 shelves = [] shelves = get_user_shelf_preview(request.user, [('reading', 3), ('read', 1), ('to-read', 3)]) size = sum(len(s['books']) for s in shelves) # books new to the instance, for discovery if size < 6: recent_books = models.Work.objects.order_by('-created_date')[:6 - size] recent_books = [b.default_edition for b in recent_books] shelves.append({ 'name': 'Recently added', 'identifier': None, 'books': recent_books, 'count': 6 - size, }) # allows us to check if a user has shelved a book user_books = models.Edition.objects.filter( shelves__user=request.user).all() activities = get_activity_feed(request.user, tab) activity_count = activities.count() activities = activities[(page - 1) * page_size:page * page_size] next_page = '/?page=%d' % (page + 1) prev_page = '/?page=%d' % (page - 1) data = { 'user': request.user, 'shelves': shelves, 'user_books': user_books, 'activities': activities, 'feed_tabs': [{ 'id': 'home', 'display': 'Home' }, { 'id': 'local', 'display': 'Local' }, { 'id': 'federated', 'display': 'Federated' }], 'active_tab': tab, 'review_form': forms.ReviewForm(), 'quotation_form': forms.QuotationForm(), 'comment_form': forms.CommentForm(), 'next': next_page if activity_count > (page_size * page) else None, 'prev': prev_page if page > 1 else None, } return TemplateResponse(request, 'feed.html', data)