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 profile(request, username): user = request.user 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: # pragma: no cover from_feed = feeds[0].id feeds_count = Feed.objects.filter(user=page_user).count() article_count = Article.objects.filter(create_user=page_user).count() article_comment_count = ArticleComment.objects.filter( user=page_user).count() question_count = Question.objects.filter(user=page_user).count() answer_count = Answer.objects.filter(user=page_user).count() activity_count = Activity.objects.filter(user=page_user).count() messages_count = Message.objects.filter( Q(from_user=page_user) | Q(user=page_user)).count() data, datepoints = Activity.daily_activity(page_user) data = { 'user': user, 'page_user': page_user, 'feeds_count': feeds_count, 'article_count': article_count, 'article_comment_count': article_comment_count, 'question_count': question_count, 'global_interactions': activity_count + article_comment_count + answer_count + messages_count, # noqa: E501 'answer_count': answer_count, 'bar_data': [ feeds_count, article_count, article_comment_count, question_count, answer_count, activity_count ], 'bar_labels': json.dumps( '["Feeds", "Articles", "Comments", "Questions", "Answers", "Activities"]' ), # noqa: E501 'line_labels': datepoints, 'line_data': data, 'feeds': feeds, 'from_feed': from_feed, 'page': 1 } return render(request, 'core/profile.html', data)
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 signup(request): if request.method == 'POST': form = SignUpForm(request.POST) if form.is_valid(): ''' Begin reCAPTCHA validation ''' recaptcha_response = request.POST.get('g-recaptcha-response') data = { 'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY, 'response': recaptcha_response } r = requests.post( 'https://www.google.com/recaptcha/api/siteverify', data=data) result = r.json() ''' End reCAPTCHA validation ''' if result['success']: 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 = '{0} has joined the network.'.format( user.username) feed = Feed(user=user, post=welcome_post) feed.save() return redirect('/') else: recaptcha_error = "Please solve the reCAPTCHA for creating an account." return render(request, 'authentication/signup.html', { 'form': SignUpForm(), 'recaptcha_error': recaptcha_error }) else: return render(request, 'authentication/signup.html', {'form': form}) else: return render(request, 'authentication/signup.html', {'form': SignUpForm()})
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 _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'] photo_link = request.POST['photo_link'] post = post.strip() if len(post) > 0: feed.post = post[:255] feed.photo_link = photo_link feed.save() html = _html_feeds(last_feed, user, csrf_token) return HttpResponse(html)