def load(request): from_feed = request.GET.get('from_feed') page = request.GET.get('page') feed_source = request.GET.get('feed_source') all_feeds = Feed.get_feeds(from_feed) if feed_source != 'all': all_feeds = all_feeds.filter(user__id=feed_source) paginator = Paginator(all_feeds, FEEDS_NUM_PAGES) try: feeds = paginator.page(page) except PageNotAnInteger: return HttpResponseBadRequest() except EmptyPage: feeds = [] html = '' csrf_token = (csrf(request)['csrf_token']) for feed in feeds: html = '{0}{1}'.format(html, render_to_string('feeds/partial_feed.html', { 'feed': feed, 'user': request.user, 'csrf_token': csrf_token })) return HttpResponse(html)
def check(request): last_feed = request.GET.get('last_feed') feed_source = request.GET.get('feed_source') feeds = Feed.get_feeds_after(last_feed) if feed_source != 'all': feeds = feeds.filter(user__id=feed_source) count = feeds.count() return HttpResponse(count)
def feeds(request): all_feeds = Feed.get_feeds() paginator = Paginator(all_feeds, FEEDS_NUM_PAGES) feeds = paginator.page(1) from_feed = -1 if feeds: from_feed = feeds[0].id return render(request, 'feeds/feeds.html', { 'feeds': feeds, 'from_feed': from_feed, 'page': 1, })
def update(request): first_feed = request.GET.get('first_feed') last_feed = request.GET.get('last_feed') feed_source = request.GET.get('feed_source') feeds = Feed.get_feeds().filter(id__range=(last_feed, first_feed)) if feed_source != 'all': feeds = feeds.filter(user__id=feed_source) dump = {} for feed in feeds: dump[feed.pk] = {'likes': feed.likes, 'comments': feed.comments} data = json.dumps(dump) return HttpResponse(data, content_type='application/json')
def profile(request, username): page_user = get_object_or_404(User, username=username) all_feeds = Feed.get_feeds().filter(user=page_user) paginator = Paginator(all_feeds, FEEDS_NUM_PAGES) feeds = paginator.page(1) from_feed = -1 if feeds: from_feed = feeds[0].id return render(request, 'core/profile.html', { 'page_user': page_user, 'feeds': feeds, 'from_feed': from_feed, 'page': 1 })
def signup(request): if request.method == 'POST': form = SignUpForm(request.POST) if not form.is_valid(): return render(request, 'authentication/signup.html', {'form': form}) else: username = form.cleaned_data.get('username') email = form.cleaned_data.get('email') password = form.cleaned_data.get('password') User.objects.create_user(username=username, password=password, email=email) user = authenticate(username=username, password=password) login(request, user) welcome_post = u'{0} has joined the network.'.format(user.username, user.username) feed = Feed(user=user, post=welcome_post) feed.save() return redirect('/') else: return render(request, 'authentication/signup.html', {'form': SignUpForm()})
def _html_feeds(last_feed, user, csrf_token, feed_source='all'): feeds = Feed.get_feeds_after(last_feed) if feed_source != 'all': feeds = feeds.filter(user__id=feed_source) html = '' for feed in feeds: html = '{0}{1}'.format(html, render_to_string('feeds/partial_feed.html', { 'feed': feed, 'user': user, 'csrf_token': csrf_token })) return html
def post(request): last_feed = request.POST.get('last_feed') user = request.user csrf_token = (csrf(request)['csrf_token']) feed = Feed() feed.user = user post = request.POST['post'] post = post.strip() if len(post) > 0: feed.post = post[:255] feed.save() html = _html_feeds(last_feed, user, csrf_token) return HttpResponse(html)