def fave(request, slug): """Save reading to current user's favorites.""" reading = get_object_or_404(Reading, slug=slug) if request.method == 'POST': # Check to see if user already has reading saved to favorites try: favorite = request.user.favorite_set.get(reading=reading) # del rep del_rep(request, fv=favorite) favorite.delete() # If user has not saved reading to favorites except ObjectDoesNotExist: favorite = request.user.favorite_set.create(reading=reading) # add rep add_rep(request, fv=favorite) d = { 'reading': reading, 'static': settings.STATIC_URL, } t = loader.get_template('favorites/favorite_form.html') context = RequestContext(request, add_csrf(request, d)) data = { 'favorite_count': len(request.user.favorite_set.all()), 'favorite_form': t.render(context), 'pk': reading.pk, } return HttpResponse(json.dumps(data), mimetype='application/json') return HttpResponseRedirect(reverse('readings.views.detail', args=[reading.slug]))
def new(request, pk): note = get_object_or_404(Note, pk=pk) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.note = note comment.user = request.user comment.save() # add rep add_rep(request, c=comment) # create notification notify(comment=comment) d = { 'comment': comment, 'comment_form': CommentForm(), 'note': note, 'reply_form': ReplyForm(), 'static': settings.STATIC_URL, } comment_form = loader.get_template('comments/comment_form.html') comment_temp = loader.get_template('comments/comment.html') context = RequestContext(request, add_csrf(request, d)) data = { 'comment': comment_temp.render(context), 'comment_count': note.comment_count(), 'comment_form': comment_form.render(context), 'comment_pk': comment.pk, 'note_pk': note.pk, } return HttpResponse(json.dumps(data), mimetype='application/json') return HttpResponseRedirect(reverse('readings.views.detail', args=[reading.slug]))
def auto_tag(request, reading): """Automatically generate a tag and tie based on the reading's title.""" try: # check to see if user already tied a tag to this reading existing_tie = reading.tie_set.get(user=request.user) except ObjectDoesNotExist: title = reading.title.lower() pattern = re.compile( '|'.join([tag.name for tag in Tag.objects.all()])) # match the title against all tag names result = re.search(pattern, title) # if there is a match if result: match = title[result.start():result.end()] try: # check to see if there is a tag with that name tag = Tag.objects.get(name=match) try: # if there is a tie with that tag already, do nothing tie = reading.tie_set.get(tag=tag) except ObjectDoesNotExist: # tie a tag to reading tie = reading.user.tie_set.create(reading=reading, tag=tag) # add rep add_rep(request, t=tie) except ObjectDoesNotExist: pass
def auto_vote(request, reading): notes = reading.note_set.all() users = first_ten_users() # create votes for first ten users for user in users: # vote each note for note in notes: vote = note.vote_set.filter(user=user) if not vote: if randint(0, 4): value = 1 else: value = -1 # create vote vote = note.vote_set.create(user=user, value=value) # add rep add_rep(request, v=vote) # vote reading vote = reading.vote_set.filter(user=user) if not vote: if randint(0, 4): value = 1 else: value = -1 # create vote vote = reading.vote_set.create(user=user, value=value) # add rep add_rep(request, v=vote)
def new(request, pk): """Create new reply.""" comment = get_object_or_404(Comment, pk=pk) if request.method == 'POST': form = ReplyForm(request.POST) if form.is_valid(): reply = form.save(commit=False) reply.comment = comment reply.user = request.user reply.save() # add rep add_rep(request, rp=reply) # create notification notify(reply=reply) d = { 'comment': comment, 'reply': reply, 'reply_form': ReplyForm(), 'static': settings.STATIC_URL, } reply_form = loader.get_template('replies/reply_form.html') reply_temp = loader.get_template('replies/reply.html') context = RequestContext(request, add_csrf(request, d)) data = { 'comment_pk': comment.pk, 'reply': reply_temp.render(context), 'reply_count': comment.reply_count(), 'reply_form': reply_form.render(context), 'reply_pk': reply.pk, } return HttpResponse(json.dumps(data), mimetype='application/json') return HttpResponseRedirect(reverse('readings.views.detail', args=[comment.note.reading.slug]))
def comment(request, pk): """Detail comment.""" comment = get_object_or_404(Comment, pk=pk) if request.method == 'POST': if request.user.pk == 2: form = DavidReplyForm(request.POST) else: form = AdminReplyForm(request.POST) if form.is_valid(): # save reply reply = form.save(commit=False) reply.comment = comment reply.save() # add rep add_rep(request, rp=reply) # create notification notify(reply=reply) messages.success(request, 'Reply created') return HttpResponseRedirect(reverse( 'admins.views.comment', args=[comment.pk])) else: if request.user.pk == 2: form = DavidReplyForm() else: form = AdminReplyForm() d = { 'comment': comment, 'comment_pk': comment.pk, 'form': form, 'title': 'Comment: Add Reply', } return render_to_response('admins/detail_comment.html', add_csrf(request, d), context_instance=RequestContext(request))
def note(request, pk): """Detail note.""" note = get_object_or_404(Note, pk=pk) if request.method == 'POST': if request.user.pk == 2: form = DavidCommentForm(request.POST) else: form = AdminCommentForm(request.POST) if form.is_valid(): # save comment comment = form.save(commit=False) comment.note = note comment.save() # add rep add_rep(request, c=comment) # create notification notify(comment=comment) messages.success(request, 'Comment created') return HttpResponseRedirect(reverse( 'admins.views.note', args=[note.pk])) else: if request.user.pk == 2: form = DavidCommentForm(request.POST) else: form = AdminCommentForm() d = { 'form': form, 'note': note, 'note_pk': note.pk, 'title': 'Note: Add Comment', } return render_to_response('admins/detail_note.html', add_csrf(request, d), context_instance=RequestContext(request))
def follow(request, slug): """Follow or unfollow a user.""" user = user_exists(slug) change_count = 1 # if request method is POST and # the follow/unfollow user is not the current user if request.method == 'POST' and user.pk != request.user.pk: # if request POST has follow in it if request.POST.get('follow'): # follow user follow = user.followed_set.create(follower=request.user) # add rep add_rep(request, fw=follow) # create notification notify(follow=follow) # if request POST has unfollow in it elif request.POST.get('unfollow'): # unfollow user follow = user.followed_set.filter(follower=request.user)[0] # del rep del_rep(request, fw=follow) follow.delete() # if current user is on another user's page if user.pk == int(request.POST.get('current_pk', 0)): followers_count = str(user.profile.followers_count()) following_count = str(user.profile.following_count()) # if current user is on their own page elif request.user.pk == int(request.POST.get('current_pk', 0)): followers_count = str(request.user.profile.followers_count()) following_count = str(request.user.profile.following_count()) # if current user is not on anyone's page else: followers_count = 0 following_count = 0 change_count = 0 d = { 'current_pk': request.POST.get('current_pk'), 'profile_user': user, } t = loader.get_template('follows/follow_form.html') c = RequestContext(request, add_csrf(request, d)) data = { 'change_count': change_count, 'follow_form': t.render(c), 'followers_count': followers_count, 'following_count': following_count, 'user_id': str(user.pk), } return HttpResponse(json.dumps(data), mimetype='application/json') else: return HttpResponseRedirect(reverse('readings.views.list_user', args=[profile.slug]))
def new(request, slug): """Create new tie for reading.""" reading = get_object_or_404(Reading, slug=slug) if request.method == "POST": # If reading has 5 tags, do not add or create if len(reading.tie_set.all()) >= 5: messages.warning(request, "Each read can only have 5 tags") else: try: # If user already added a tag user = reading.tie_set.get(user=request.user) messages.error(request, "You can only add 1 tag per read") except ObjectDoesNotExist: name = request.POST.get("tag_name") name = name.lower() pattern = only_letters() # If name contains only letters if re.search(pattern, name): # If name does not contain any banned words blacklist = banned_words() if not re.search(blacklist, name): try: # If tag exists, get tag tag = Tag.objects.get(name=name) except ObjectDoesNotExist: # If tag does not exist, create tag tag = Tag(name=name, user=request.user) tag.slug = slugify(tag.name) tag.save() try: # Check to see if tie exists tie = reading.tie_set.get(tag=tag) except ObjectDoesNotExist: # If tie does not exist, create it tie = request.user.tie_set.create(reading=reading, tag=tag) # add rep add_rep(request, t=tie) # create notification notify(tie=tie) d = {"reading": reading} tags = loader.get_template("tags/tags.html") context = RequestContext(request, d) data = {"ties": tags.render(context)} return HttpResponse(json.dumps(data), mimetype="application/json") return HttpResponseRedirect(reverse("readings.views.detail", args=[reading.slug]))
def new(request, slug): """Create new note for reading.""" reading = get_object_or_404(Reading, slug=slug) if request.method == 'POST': form = NoteForm(request.POST) if form.is_valid(): note = form.save(commit=False) note.reading = reading note.user = request.user note.save() # facebook open graph add note facebook_graph_add_note(request.user, reading) # add rep add_rep(request, n=note) request.user.vote_set.create(note=note, value=1) notify(note=note) if request.user.is_staff: # auto create votes for reading and reading.notes auto_vote(request, reading) d = { 'comment_form': CommentForm(), 'note': note, 'note_form': NoteForm(), 'reading': reading, 'static': settings.STATIC_URL, } bullet_notes = loader.get_template('notes/bullet_notes.html') note_form = loader.get_template('notes/note_form.html') note_temp = loader.get_template('notes/note.html') context = RequestContext(request, add_csrf(request, d)) data = { 'bullet_notes': bullet_notes.render(context), 'note': note_temp.render(context), 'note_form': note_form.render(context), 'note_pk': note.pk, } return HttpResponse(json.dumps(data), mimetype='application/json') return HttpResponseRedirect(reverse('readings.views.detail', args=[reading.slug]))
def reading(request, slug): """Detail reading.""" reading = get_object_or_404(Reading, slug=slug) if request.method == 'POST': if request.user.pk == 2: form = DavidNoteForm(request.POST) else: form = AdminNoteForm(request.POST) if form.is_valid(): # save note note = form.save(commit=False) note.reading = reading note.save() # facebook open graph add note if note.user.pk == request.user.pk: facebook_graph_add_note(request.user, reading) # add rep add_rep(request, n=note) # create notification notify(note=note) note.user.vote_set.create(note=note, value=1) # auto create votes for reading and reading.notes auto_vote(request, reading) messages.success(request, 'Note created') return HttpResponseRedirect(reverse('admins.views.reading', args=[reading.slug])) else: if request.user.pk == 2: form = DavidNoteForm() else: form = AdminNoteForm() d = { 'form': form, 'reading': reading, 'title': 'Reading: Add Note', } return render_to_response('admins/detail_reading.html', add_csrf(request, d), context_instance=RequestContext(request))
def follow_tag(request, slug): """Follow or unfollow a tag.""" tag = get_object_or_404(Tag, slug=slug) if request.method == 'POST': # If current user is already following tag if request.user in tag.followers(): # delete tag follow tagfollow = tag.tagfollow_set.get(user=request.user) # del rep del_rep(request, tf=tagfollow) tagfollow.delete() # If current user is not following tag else: # create tag follow tagfollow = request.user.tagfollow_set.create(tag=tag) # add rep add_rep(request, tf=tagfollow) d = { 'tag': tag, } t = loader.get_template('follows/tagfollow_form.html') context = RequestContext(request, add_csrf(request, d)) data = { 'tagfollowers_count': tag.followers_count(), 'tag_pk': tag.pk, 'tagfollow_form': t.render(context), 'topic_count': request.user.profile.topic_count(), } tie = tag.tie_set.all()[0] if tie: d = { 'tie': tie } t = loader.get_template('follows/tiefollow_form.html') context = RequestContext(request, add_csrf(request, d)) data['tiefollow_form'] = t.render(context) return HttpResponse(json.dumps(data), mimetype='application/json') return HttpResponseRedirect(reverse('tags.views.detail', args=[tag.slug]))
def new_reading(request, pk): """Create a downvote or upvote for reading.""" reading = get_object_or_404(Reading, pk=pk) if request.method == 'POST' and request.POST.get('action'): # If user is downvoting if request.POST.get('action') == 'downvote': value = -1 # If user is upvoting elif request.POST.get('action') == 'upvote': value = 1 try: # Check to see if user already voted vote = reading.vote_set.get(user=request.user) # If user already upvoted if vote.value == 1: # If user is upvoting again, remove the vote if value == 1: # del rep del_rep(request, v=vote) vote.delete() # If user is downvoting, downvote the note else: vote.value = value vote.save() # If user changes their vote, update the notifications notifications = Notification.objects.filter(vote=vote) for notification in notifications: notification.created = timezone.now() notification.viewed = False notification.save() # If user already downvoted elif vote.value == -1: # If user is downvoting again, remove the vote if value == -1: # del rep del_rep(request, v=vote) vote.delete() # If user is upvoting, upvote the note else: vote.value = value vote.save() # If user changes their vote, update the notifications notifications = Notification.objects.filter(vote=vote) for notification in notifications: notification.created = timezone.now() notification.viewed = False notification.save() except ObjectDoesNotExist: # If the user has not yet voted, create a new vote vote = request.user.vote_set.create(reading=reading, value=value) # add rep add_rep(request, v=vote) # create notification notify(vote=vote) vote_reading_form = loader.get_template('votes/vote_reading_form.html') context = RequestContext(request, add_csrf(request, { 'reading': reading })) data = { 'pk': reading.pk, 'vote_reading_form': vote_reading_form.render(context), } return HttpResponse(json.dumps(data), mimetype='application/json') return HttpResponseRedirect(reverse('readings.views.detail', args=[reading.slug]))
def new_bookmarklet(request): """Create new reading from bookmarklet.""" if request.method == "POST": content = request.POST.get("content", "") image = request.POST.get("image", "") link = request.POST.get("link", "") titl = request.POST.get("title", "")[:80] if request.user.is_staff: user_pk = request.POST.get("user") if user_pk: try: user = User.objects.get(pk=int(user_pk)) except ObjectDoesNotExist: user = request.user else: user = request.user else: user = request.user # if there is a link and title, create reading if link and titl: try: # if reading with link exists, add notes to that reading reading = Reading.objects.get(link=link) reading_exists = True except ObjectDoesNotExist: reading_exists = False titles = Reading.objects.filter(title=titl) # if there is a reading with the title if titles: titl = "%s-%s" % (titl, str(titles.count())) reading = Reading(image=image, link=link, title=titl, user=user) reading.save() # create vote for reading reading.vote_set.create(user=user, value=1) # add tag name = request.POST.get("tag_name") # if user added tag if name: try: # check to see if user already tied a tag to this reading existing_tie = reading.tie_set.get(user=user) # if user has not added a tag to this reading except ObjectDoesNotExist: name = name.lower() pattern = only_letters() # If name contains only letters if re.search(pattern, name): # If name does not contain any banned words blacklist = banned_words() if not re.search(blacklist, name): try: # If tag exists, get tag tag = Tag.objects.get(name=name) except ObjectDoesNotExist: # If tag does not exist, create tag tag = Tag(name=name, user=user) tag.slug = slugify(tag.name) tag.save() tie = user.tie_set.create(reading=reading, tag=tag) # add rep add_rep(request, t=tie) # if user did not add a tag, auto tag else: auto_tag(request, reading) # add rep add_rep(request, rd=reading) # if there is content, create note if content.strip(): note = Note(content=content, reading=reading, user=user) note.save() # create first vote for note user.vote_set.create(note=note, value=1) if reading_exists: facebook_graph_add_note(note.user, note.reading) if not reading_exists: facebook_graph_add_reading(reading.user, reading) if request.user.is_staff: # auto create votes for reading and reading.notes auto_vote(request, reading) data = {"success": 1} return HttpResponse(json.dumps(data), mimetype="application/json") content = request.GET.get("note", "").lstrip(" ") link = request.GET.get("link", "") # split title html_title = split_title(request.GET.get("title", "")) if request.user.is_staff: if request.user.pk == 2: users = admin_david_list() else: users = admin_user_list() else: users = [] d = {"content": content, "link": link, "titl": html_title, "title": "Add Reading", "users": users} return render_to_response( "readings/new_bookmarklet.html", add_csrf(request, d), context_instance=RequestContext(request) )
def new(request): """Add new reading.""" NoteFormset = formset_factory(NoteForm, extra=3) if request.method == "POST": link = request.POST.get("link") form = ReadingForm(request.POST) formset = NoteFormset(request.POST) # if reading with link already exists, add notes to it try: reading = Reading.objects.get(link=link) if formset.is_valid(): # add tag name = request.POST.get("tag_name") # if user added a tag if name: name = name.lower() pattern = only_letters() # If name contains only letters if re.search(pattern, name): # If name does not contain any banned words blacklist = banned_words() if not re.search(blacklist, name): try: # If tag exists, get tag tag = Tag.objects.get(name=name) except ObjectDoesNotExist: # If tag does not exist, create tag tag = Tag(name=name, user=request.user) tag.slug = slugify(tag.name) tag.save() try: # Check to see if tie exists tie = reading.tie_set.get(tag=tag) except ObjectDoesNotExist: # If tie does not exist, create it tie = request.user.tie_set.create(reading=reading, tag=tag) # add rep add_rep(request, t=tie) # create notification notify(tie=tie) # if user did not add a tag, auto tag else: auto_tag(request, reading) # create notes and add it to the existing reading for index, note_form in enumerate(formset): note = note_form.save(commit=False) note.reading = reading note.user = request.user if note.content.strip(): note.save() # create first vote for note note.user.vote_set.create(note=note, value=1) # if this is the first note if index == 0: # facebook open graph add note facebook_graph_add_note(note.user, note.reading) if request.user.is_staff: # auto create votes for reading and reading.notes auto_vote(request, reading) messages.success(request, "This reading exists, your content has been added to it") return HttpResponseRedirect(reverse("readings.views.detail", args=[reading.slug])) # if reading with link does not exist, create the reading except ObjectDoesNotExist: if form.is_valid() and formset.is_valid(): reading = form.save(commit=False) reading.user = request.user reading.save() # create vote for reading reading.vote_set.create(user=reading.user, value=1) # add tag name = request.POST.get("tag_name") # if user added tag if name: name = name.lower() pattern = only_letters() # If name contains only letters if re.search(pattern, name): # If name does not contain any banned words blacklist = banned_words() if not re.search(blacklist, name): try: # If tag exists, get tag tag = Tag.objects.get(name=name) except ObjectDoesNotExist: # If tag does not exist, create tag tag = Tag(name=name, user=request.user) tag.slug = slugify(tag.name) tag.save() tie = request.user.tie_set.create(reading=reading, tag=tag) # add rep add_rep(request, t=tie) # if user did not add a tag, auto tag else: auto_tag(request, reading) # facebook open graph add reading facebook_graph_add_reading(request.user, reading) # add rep add_rep(request, rd=reading) # create notes for this reading for note_form in formset: note = note_form.save(commit=False) note.reading = reading note.user = request.user if note.content.strip(): note.save() # create first vote for note request.user.vote_set.create(note=note, value=1) if request.user.is_staff: # auto create votes for reading and reading.notes auto_vote(request, reading) messages.success(request, "Reading created") return HttpResponseRedirect(reverse("readings.views.detail", args=[reading.slug])) else: form = ReadingForm() formset = NoteFormset() di = {"static": settings.STATIC_URL} t = loader.get_template("javascript/bookmarklet.js") context = RequestContext(request, di) d = { "bookmarklet": re.sub(r"\s", "%20", str(t.render(context))), "form": form, "formset": formset, "title": "New Reading", } return render_to_response("readings/new.html", add_csrf(request, d), context_instance=RequestContext(request))
def new_reading(request): """Create a new reading.""" NoteFormset = formset_factory(NoteForm, extra=3, formset=RequiredFormSet) if request.method == 'POST': if request.user.pk == 2: form = DavidReadingForm(request.POST) else: form = AdminReadingForm(request.POST) formset = NoteFormset(request.POST) if form.is_valid() and formset.is_valid(): # save reading reading = form.save() # add tag name = request.POST.get('tag_name') # if user added a tag if name: name = name.lower() pattern = only_letters() # If name contains only letters if re.search(pattern, name): # If name does not contain any banned words blacklist = banned_words() if not re.search(blacklist, name): try: # If tag exists, get tag tag = Tag.objects.get(name=name) except ObjectDoesNotExist: # If tag does not exist, create tag tag = Tag(name=name, user=request.user) tag.slug = slugify(tag.name) tag.save() tie = request.user.tie_set.create(reading=reading, tag=tag) # add rep add_rep(request, t=tie) # if user did not add a tag, auto tag else: auto_tag(request, reading) # facebook open graph add reading if reading.user.pk == request.user.pk: facebook_graph_add_reading(request.user, reading) # add rep add_rep(request, rd=reading) first = True # save each note for note_form in formset: note = note_form.save(commit=False) if note.content.strip(): note.reading = reading # if this is the first note # set note.user to reading.user if first: note.user = reading.user first = False else: note.user = random_user() # save note note.save() # create first vote for note note.user.vote_set.create(note=note, value=1) # add rep add_rep(request, n=note) # auto create votes for reading and reading.notes auto_vote(request, reading) messages.success(request, 'Reading created') return HttpResponseRedirect(reverse('admins.views.reading', args=[reading.slug])) else: if request.user.pk == 2: form = DavidReadingForm() else: form = AdminReadingForm() formset = NoteFormset() di = { 'static': settings.STATIC_URL } t = loader.get_template('javascript/bookmarklet.js') context = RequestContext(request, di) d = { 'bookmarklet': re.sub(r'\s', '%20', str(t.render(context))), 'form': form, 'formset': formset, 'title': 'New Reading', } return render_to_response('readings/new.html', add_csrf(request, d), context_instance=RequestContext(request))