def add_topic(request, forum_id): '''Adds a new task to the department forum''' forum = get_object_or_404(Forum, pk=forum_id) topics = forum.topics.order_by('-updated').select_related() if request.method == 'POST': form=AddTopicForm(request.POST) if form.is_valid(): data=form.save(commit=False) data.forum=forum data.creator=request.user forum.topic_count+=1 data.save() body = '<span style="color:blue;">%s</span> started <span style="color:red;">%s</span> under <span style="color:green;">%s</span>' %(request.user.first_name,data.title,data.forum.title) link = '/forum/topic/%d/' %(data.id) notif = Notification(notif_body=body,link=link) notif.save() if data.forum.department == 'public': notif.is_public = True elif data.forum.department == 'coregroup': for user in Group.objects.get(name='Core').user_set.all(): notif.receive_users.add(user) else: notif.depts.add(Department.objects.get(name=data.forum.department)) notif.save() print notif.depts.all() forum.save() return redirect('forum.views.add_post',topic_id=data.pk) else: for error in form.errors: pass else: form=AddTopicForm() return render(request, 'forum/add_topic.html',{'form':form, 'forum': forum, 'topics': topics, })
def add_post(request, topic_id): topic = get_object_or_404(Topic, pk=topic_id) profile = get_object_or_404(UserProfile, pk=request.user.id) posts = topic.posts.all().select_related() #for i in posts: # This will truncates the description if it is greater than 100 characters and adds some dots #i.description = (i.description[:300] + " ....") if len(i.description) > 300 else i.description #i.description = i.description if request.method == 'POST': form=AddPostForm(request.POST) if form.is_valid(): profile.post_count+=1 profile.save() data=form.save(commit=False) data.user=request.user data.topic=topic data.save() body = '<span style="color:blue;">%s</span> posted in <span style="color:red;">%s</span> under <span style="color:green;">%s</span>' %(request.user.first_name,data.topic.title,data.topic.forum.title) link = '/forum/topic/%d/' %(data.topic.id) notif = Notification(notif_body=body,link=link) notif.save() if data.topic.forum.department == 'public': notif.is_public = True elif data.topic.forum.department == 'coregroup': print '-------------------' print 'coregroup' print '-------------------' for user in Group.objects.get(name='Core').user_set.all(): notif.receive_users.add(user) else: notif.receive_depts.add(Department.objects.get(name=data.topic.forum.department)) notif.save() print notif.receive_depts.all() topic.updated=datetime.datetime.now() topic.post_count+=1 topic.last_post=data topic.save() print topic.forum.pk forum = get_object_or_404(Forum, pk=topic.forum.pk) print forum forum.post_count+=1 forum.last_post=data forum.save() print data.pk return redirect('forum.views.show_topic', topic_id = topic.pk) else: for error in form.errors: messages.warning(request, error) else: form=AddPostForm() return render(request, 'forum/add.html',{ 'form':form, 'topic': topic, 'post_count': topic.post_count, 'posts': posts, })