def recent_notifications(request): """ return 20 last notifications for the user. """ context_dict = {} recent_notifications = Notification.objects.recent_notifications(request.user) if recent_notifications: Notification.objects.filter(receiver = request.user, seen = False).update(seen = True) for notification in recent_notifications: notification.humanized_created_at = last_posted_date(notification.created_at) context_dict['notifications'] = recent_notifications else: context_dict['notifications'] = None return render(request, 'simplenation/notifications.html', context_dict)
def profile(request, profile_name_slug): context_dict = {} try: author = Author.objects.get(slug=profile_name_slug) explanations = Definition.objects.filter(author = author).order_by('-post_date') favorees = Favourite.objects.favorees_for_user(author.user) favoree_count = Favourite.objects.favoree_count(author.user) if request.user.is_authenticated(): if not Favourite.objects.is_favoree(request.user, author.user): context_dict['favorite_button_class'] = None context_dict['favor_button_text'] = "Add to favorites" else: context_dict['favorite_button_class'] = "is-favorite" context_dict['favor_button_text'] = "Added to favourites" if explanations: for explanation in explanations: explanation.last_posted = last_posted_date(explanation.post_date) context_dict['explanations'] = explanations context_dict['author'] = author context_dict['profile_name'] = author.user.username context_dict['profile_email'] = author.user.email context_dict['profile_bio'] = author.bio context_dict['favourites'] = favorees context_dict['favourites_count'] = favoree_count if author.picture: context_dict['profile_picture'] = author.picture context_dict['success'] = True except Author.DoesNotExist: context_dict['success'] = False context_dict['no_success_message'] = 'User does not exist.' return render(request, 'simplenation/profile.html', context_dict)
def term(request, term_name_slug): context_dict = {} liked = False reported = False report_by_explanation_id = {} try: term = Term.objects.get(slug=term_name_slug) explanations = Definition.objects.filter(term = term) if request.user.id: likes = Like.objects.filter(user=request.user) reports = Report.objects.filter(user = request.user) tags = term.tags.all() for explanation in explanations: explanation.last_posted = last_posted_date(explanation.post_date) if request.user.id: like = likes.filter(definition=explanation) if like: explanation.like_text = 'Unlike' else: pass report = reports.filter(definition=explanation) if report: explanation.reporter = request.user.id else: pass if explanation.times_reported > PROFANITY_CHECK_THRESHOLD: explanation.delete() context_dict['term_name'] = term.name context_dict['explanations'] = explanations context_dict['term'] = term context_dict['tags'] = tags if request.user.id: if likes: context_dict['likes'] = likes context_dict['liked'] = liked if request.method == 'POST' and 'add' in request.POST: form = DefinitionForm(request.POST or None) if form.is_valid(): definition = form.save(commit=False) suspect_word_count = profanityFilter(definition.body) if suspect_word_count > 0: definition.times_reported = suspect_word_count definition.term = term definition.author = request.user.author definition.save() return HttpResponseRedirect('/simplenation/term/'+ term_name_slug) else: print form.errors else: form = DefinitionForm() except Term.DoesNotExist: pass context_dict['form'] = form return render(request, 'simplenation/term.html', context_dict)
def term(request, term_name_slug): context_dict = {} reported = False report_by_explanation_id = {} pictures = [] context_dict['term_exists'] = True try: term = Term.objects.get(slug=term_name_slug) explanations = Definition.objects.filter(term = term).order_by('-likes') pictures = Picture.objects.filter(term = term) top_contributors = list(Author.objects.order_by('-score')[:50]) views = request.session.get('views_'+term.name) if not views: term.views = term.views + 1 view_registry = request.session.get('last_view_'+term.name) if view_registry: view_registry_date = datetime.strptime(view_registry[:-7], "%Y-%m-%d %H:%M:%S") if (datetime.now() - view_registry_date).days > 20: request.session.clear() else: request.session['last_view_'+term.name] = str(datetime.now()) request.session['views_'+term.name] = term.views term.save() if request.user.id: reports = Report.objects.filter(user = request.user) favorees = Favourite.objects.favorees_for_user(request.user) if favorees: favorees_of_favorees = [] for favoree in favorees: favorees_of_favorees.extend(list(Favourite.objects.favorees_for_user(favoree))) if top_contributors: if favoree.author in top_contributors: top_contributors.remove(favoree.author) if favorees and favorees_of_favorees: favorees_of_favorees = list(set(favorees_of_favorees) - set(favorees)) if request.user in favorees_of_favorees: favorees_of_favorees.remove(request.user) if favorees_of_favorees: for favoree in favorees_of_favorees: if favoree.author in top_contributors: top_contributors.remove(favoree.author) tags = term.tags.all() for explanation in explanations: explanation.last_posted = last_posted_date(explanation.post_date) if explanation.author == None: explanation.author = deleted_user_profile() if request.user.id: if Like.objects.has_liked(request.user, explanation): like = Like.objects.get(user=request.user, definition=explanation) if like.upvote: explanation.like_text = 'upvoted' elif like.downvote: explanation.like_text = 'downvoted' else: explanation.like_text = None report = reports.filter(definition=explanation) if report: explanation.reporter = request.user.id if explanation.times_reported > PROFANITY_CHECK_THRESHOLD: explanation.delete() context_dict['term_name'] = term.name context_dict['explanations'] = explanations context_dict['term'] = term context_dict['tags'] = tags if pictures: for picture in pictures: if picture.to_delete: picture.to_delete = False picture.save() if picture.to_add: picture.delete() pictures = Picture.objects.filter(term = term) context_dict['pictures'] = pictures if request.user.id: if favorees: context_dict['favourites'] = favorees if favorees_of_favorees: context_dict['favourites_of_favourites'] = favorees_of_favorees if top_contributors: if request.user.author in top_contributors: top_contributors.remove(request.user.author) context_dict['top_contributors'] = top_contributors # Posting on term page # Is user submitting a new post? if request.method == 'POST': form = DefinitionForm(request.POST or None) pictures = request.FILES.getlist('pictures') # Did user submit a valid post or is he sending blanks? if form.is_valid(): definition = form.save(commit=False) # How many times did the user swear? Report to admins if he did suspect_word_count = profanityFilter(definition.body) if suspect_word_count > 0: definition.times_reported = suspect_word_count definition.term = term # Is user logged in? user = None if not request.user.is_authenticated(): # Is it login_form or registration_form? if 'login' in request.POST: response = log_user_while_post(request) else: response = register_user_while_post(request) # Was the registraion or authentication successful? if response['success']: user = response['user'] else: # Seems the are some errors with registration or authentication if 'error_message' in response: context_dict['user_error_message'] = response['error_message'] if 'user_form' in response: context_dict['user_form'] = response['user_form'] context_dict['form'] = form context_dict['success'] = False return render(request, 'simplenation/term.html', context_dict) # Is it newly registered/logged or old one? if not user: definition.author = request.user.author else: definition.author = user.author definition.save() # Should we add pictures to the post if there are any? for picture in pictures: Picture(definition = definition, image = picture, image_thumbnail = picture, term=term).save() # Why not notify an article author that someone has expressed his thoughts about it? if definition.term.author: if request.user != definition.term.author.user: Notification(typeof = 'explanation_notification', sender = request.user, receiver = definition.term.author.user, term = term).save() return HttpResponseRedirect('/term/'+ term_name_slug) else: context_dict['post_error_message'] = 'Are you sending a blank post? If not, please try again.' else: form = DefinitionForm() context_dict['success'] = True context_dict['form'] = form except Term.DoesNotExist: context_dict['success'] = False context_dict['term_exists'] = False context_dict['no_success_message'] = "Term does not exist" return render(request, 'simplenation/term.html', context_dict)