def newtopic(request, forum_id):
	request.user.forum_last_act = datetime.datetime.now()
	request.user.save()
	f = get_object_or_404(Forum, pk=forum_id)
	g = f.category
	c = g.chapter
	user = request.user
	if (user.is_superuser) or (g.exec_only == False) or (user.is_staff and ((c == user.chapter) or (c == None))):
		if request.method == 'POST':
			topicform = NewTopicForm(request.POST)
			if topicform.is_valid():
				data = topicform.cleaned_data
				newTopic = Topic()
				newTopic.forum = f
				newTopic.posted_by = user
				newTopic.subject = data['subject']
				newTopic.last_post_time = datetime.datetime.now()
				newTopic.last_post_user = user
				if Topic.objects.filter(forum=newTopic.forum, subject=newTopic.subject):
					msg = 'A similar topic already exists'
					request.user.message_set.create(message=unicode(_(msg)))
				else:
					newTopic.save()
					postMessage = Post()
					postMessage.topic = newTopic
					postMessage.posted_by = user
					postMessage.message = data['message']
					postMessage.save()
					f.num_topics = f.num_topics + 1
					f.num_posts = f.num_posts + 1
					f.last_post_time = datetime.datetime.now()
					f.last_post_user = user
					f.save()
			else:
				request.user.message_set.create(message=unicode(_('The fields "New Topic" and "Message" can not be empty')))
		else:
			raise Http404
		if 'return' in request.GET:
			return HttpResponseRedirect(request.GET['return'])
		elif 'return' in request.POST:
			return HttpResponseRedirect(request.POST['return'])
		elif c:
			return HttpResponseRedirect('/forums/' + c.myrobogals_url + '/forum/' + str(f.pk) + '/')
		else:
			return HttpResponseRedirect('/forums/' + request.user.chapter.myrobogals_url + '/forum/' + str(f.pk) + '/')
	else:
		raise Http404
Exemple #2
0
def newtopic(request, forum_id):
	request.user.forum_last_act = datetime.datetime.now()
	request.user.save()
	f = get_object_or_404(Forum, pk=forum_id)
	g = f.category
	c = g.chapter
	user = request.user
	if (user.is_superuser) or (user.is_staff and ((c == user.chapter) or (c == None))) or ((c == user.chapter) and (g.exec_only == False)) or ((c == None) and (g.exec_only == False)):
		if request.method == 'POST':
			topicform = NewTopicForm(request.POST)
			if topicform.is_valid():
				data = topicform.cleaned_data
				newTopic = Topic()
				newTopic.forum = f
				newTopic.posted_by = user
				newTopic.subject = data['subject']
				newTopic.last_post_time = datetime.datetime.now()
				newTopic.last_post_user = user
				if Topic.objects.filter(forum=newTopic.forum, subject=newTopic.subject):
					msg = '- A similar topic already exists'
					request.user.message_set.create(message=unicode(_(msg)))
				else:
					newTopic.save()
					postMessage = Post()
					postMessage.topic = newTopic
					postMessage.posted_by = user
					postMessage.message = data['message']
					postMessage.save()
					f.last_post_time = datetime.datetime.now()
					f.last_post_user = user
					f.save()
					watchers = f.watchers.all().exclude(pk=request.user.pk)
					if watchers:
						message = EmailMessage()
						message.subject = 'New Topic: ' + newTopic.subject
						message.body = 'New topic for forum "' + f.name + '" in category "' + g.name +'"\n\nTopic subject: ' + newTopic.subject + ' (started by ' + newTopic.posted_by.get_full_name() + ')\n\nTopic Message:\n' + postMessage.message
						message.from_name = "myRobogals"
						message.from_address = "*****@*****.**"
						message.reply_address = "*****@*****.**"
						message.sender = User.objects.get(username='******')
						message.html = False
						message.email_type = 1
						message.status = -1
						message.save()
						for watcher in watchers:
							recipient = EmailRecipient()
							recipient.message = message
							recipient.user = watcher
							recipient.to_name = watcher.get_full_name()
							recipient.to_address = watcher.email
							recipient.save()
						message.status = 0
						message.save()
			else:
				request.user.message_set.create(message=unicode(_('- The fields "New Topic" and "Message" can not be empty')))
		else:
			raise Http404
		if 'return' in request.GET:
			return HttpResponseRedirect(request.GET['return'])
		elif 'return' in request.POST:
			return HttpResponseRedirect(request.POST['return'])
		elif c:
			return HttpResponseRedirect('/forums/' + c.myrobogals_url + '/forum/' + str(f.pk) + '/')
		else:
			return HttpResponseRedirect('/forums/' + request.user.chapter.myrobogals_url + '/forum/' + str(f.pk) + '/')
	else:
		raise Http404