Пример #1
0
def follow(request, follow_username = None):
	""" Follow a User """
	try:
		follow_user = User.objects.get(username = follow_username)
		followed_item = UserFollow()
		followed_item.user = request.user
		followed_item.followed_user = follow_user
		followed_item.save()
		try:
			profile = UserProfile.objects.get(user=follow_user)
			if profile.user_follows_you: #User wants to recieve a notification
				queue = EmailQueue(
					mail_to=follow_user.email,
					mail_subject="User started following you",
					mail_body=render_to_string('emails/user_follows_you.txt', {
						'user': follow_user,
						'username_that_follows': request.user.username,
						'SITE': request.META['HTTP_HOST']}
					)
				)
				queue.save()
		except:
			pass
		request.session['flash'] = ['You started following ' + follow_user.username, 'success']
	except:
		request.session['flash'] = ['This user does not exist', 'error']
	return HttpResponseRedirect(request.META['HTTP_REFERER'])
Пример #2
0
def create(request):
	data = {}
	if request.method == 'POST':
		data['form'] = SnippetForm(request.POST) # A form bound to the POST data
		if data['form'].is_valid():
			formData = data['form'].save(commit = False)
			formData.author = request.user
			if not formData.lexer:
				try:
					lexer = guess_lexer(formData.body)
					for lex in LEXERS.itervalues():
						if lexer.name == lex[1]:
							formData.lexer = lex[2][0].lower()
				except ClassNotFound:
					formData.lexer = u'text'
			if 'preview' in request.POST:
				data['title'] = formData.title;
				data['preview_body'] = highlight(formData.body, get_lexer_by_name(formData.lexer), HtmlFormatter(cssclass = 'source'))
				data['lines'] = range(1, formData.body.count('\n') + 2)
				return render_to_response('snippets/process.html', data, context_instance=build_context(request))
			else:#save - notify followers this user and have the option on
				formData.body = str(formData.body).replace("\r\n","\n")
				formData.save()
				try:
					followers = UserFollow.objects.select_related().filter(followed_user=request.user).all()
				except:
					followers = None
				if followers: # this is very inneficient - find some other way
					for follower in followers:
						profile = follower.user.get_profile();
						if profile.followed_user_created: #User wants to recieve a notification
							queue = EmailQueue(
								mail_to=follower.user.email,
								mail_subject="Followed user created a new snippet",
								mail_body=render_to_string('emails/followed_user_created.txt', {
									'user': follower.user,
									'username': request.user.username,
									'SITE': request.META['HTTP_HOST']}
								)
							)
							queue.save()
				request.session['flash'] = ['#' + str(formData.pk) +' created successfuly', 'success']
				return HttpResponseRedirect('/accounts/profile/') # Redirect after POST
		else:
		   	return render_to_response('snippets/process.html', data, context_instance=build_context(request))
	else:
		data['form'] = SnippetForm() # An unbound form
	return render_to_response('snippets/process.html', data, context_instance=build_context(request))
Пример #3
0
def comment(request, id = None):
	""" Create a new comment. Django comments framework sucks ass! """
	if request.GET.get('delete'):
		if request.user.is_staff:
			get_object_or_404(SnippetComment, pk=id).delete()
			request.session['flash'] = ['Comment deleted succesfully', 'success'];
		else:
			request.session['flash'] = ['Permission denied', 'error'];
		return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/accounts/profile/'))
	else:
		data = {}
		snippet = get_object_or_404(Snippet, pk=id)
		if request.user.is_authenticated:
			body = request.POST.get('body')
			if body:
				comment = SnippetComment(snippet = snippet, user = request.user, body = body)
				comment.save()
				if snippet.author != request.user: # send notification if you are not the author
					profile = UserProfile.objects.get(user=snippet.author)
					if profile.user_commented: #User wants to recieve a notification
						queue = EmailQueue(
							mail_to=snippet.author.email,
							mail_subject="Your snippet has been commented",
							mail_body=render_to_string('emails/user_commented.txt', {
								'user': snippet.author,
								'username': request.user.username,
								'comment': comment,
								'snippet': snippet,
								'SITE': request.META['HTTP_HOST']}
							)
						)
						queue.save()
				data['content'] = render_to_string('elements/comment.html', {'comment': comment})
			else:
				data['error'] = 'Body field is required'
		else:
			data['error'] = 'You must login to post a comment'
		return JsonResponse(data)
Пример #4
0
def post_save_snippet(sender, **kwargs):
	"""
	Send notifications to users that follow the snippet creator
	"""
	queue = EmailQueue()
	queue.save()