<s> from django . contrib import messages <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect <EOL> from django . shortcuts import render , get_object_or_404 <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from aristotle_mdr import models as MDR <EOL> from aristotle_mdr import forms as MDRForms <EOL> from aristotle_mdr import perms <EOL> @ login_required <EOL> def all ( request ) : <EOL> page = render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : request . user . profile . discussions <EOL> } ) <EOL> return page <EOL> @ login_required <EOL> def workgroup ( request , wgid ) : <EOL> wg = get_object_or_404 ( MDR . Workgroup , pk = wgid ) <EOL> if not perms . user_in_workgroup ( request . user , wg ) : <EOL> raise PermissionDenied <EOL> page = render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : wg , <EOL> '<STR_LIT>' : wg . discussions . all ( ) <EOL> } ) <EOL> return page <EOL> @ login_required <EOL> def post ( request , pid ) : <EOL> post = get_object_or_404 ( MDR . DiscussionPost , pk = pid ) <EOL> if not perms . user_in_workgroup ( request . user , post . workgroup ) : <EOL> raise PermissionDenied <EOL> comment_form = MDRForms . discussions . CommentForm ( initial = { '<STR_LIT>' : pid } ) <EOL> page = render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : post . workgroup , <EOL> '<STR_LIT>' : post , <EOL> '<STR_LIT>' : comment_form <EOL> } ) <EOL> return page <EOL> @ login_required <EOL> def toggle_post ( request , pid ) : <EOL> post = get_object_or_404 ( MDR . DiscussionPost , pk = pid ) <EOL> if not perms . user_can_alter_post ( request . user , post ) : <EOL> raise PermissionDenied <EOL> post . closed = not post . closed <EOL> post . save ( ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ post . pk ] ) ) <EOL> @ login_required <EOL> def new ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = MDRForms . discussions . NewPostForm ( request . POST , user = request . user ) <EOL> if form . is_valid ( ) : <EOL> new = MDR . DiscussionPost ( <EOL> workgroup = form . cleaned_data [ '<STR_LIT>' ] , <EOL> title = form . cleaned_data [ '<STR_LIT:title>' ] , <EOL> body = form . cleaned_data [ '<STR_LIT:body>' ] , <EOL> author = request . user , <EOL> ) <EOL> new . save ( ) <EOL> new . relatedItems = form . cleaned_data [ '<STR_LIT>' ] <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ new . pk ] ) ) <EOL> else : <EOL> initial = { } <EOL> if request . GET . get ( '<STR_LIT>' ) : <EOL> if request . user . profile . myWorkgroups . filter ( id = request . GET . get ( '<STR_LIT>' ) ) . exists ( ) : <EOL> initial = { '<STR_LIT>' : request . GET . get ( '<STR_LIT>' ) } <EOL> else : <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> if request . GET . getlist ( '<STR_LIT>' ) : <EOL> workgroup = request . user . profile . myWorkgroups . get ( id = request . GET . get ( '<STR_LIT>' ) ) <EOL> items = request . GET . getlist ( '<STR_LIT>' ) <EOL> initial . update ( { '<STR_LIT>' : workgroup . items . filter ( id__in = items ) } ) <EOL> form = MDRForms . discussions . NewPostForm ( user = request . user , initial = initial ) <EOL> return render ( request , "<STR_LIT>" , { "<STR_LIT>" : form } ) <EOL> @ login_required <EOL> def new_comment ( request , pid ) : <EOL> post = get_object_or_404 ( MDR . DiscussionPost , pk = pid ) <EOL> if not perms . user_in_workgroup ( request . user , post . workgroup ) : <EOL> raise PermissionDenied <EOL> if post . closed : <EOL> messages . error ( request , _ ( '<STR_LIT>' ) ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ post . pk ] ) ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = MDRForms . discussions . CommentForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> new = MDR . DiscussionComment ( <EOL> post = post , <EOL> body = form . cleaned_data [ '<STR_LIT:body>' ] , <EOL> author = request . user , <EOL> ) <EOL> new . save ( ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ new . post . pk ] ) + "<STR_LIT>" % new . id ) <EOL> else : <EOL> return render ( request , "<STR_LIT>" , { "<STR_LIT>" : form } ) <EOL> else : <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ post . pk ] ) ) <EOL> @ login_required <EOL> def delete_comment ( request , cid ) : <EOL> comment = get_object_or_404 ( MDR . DiscussionComment , pk = cid ) <EOL> post = comment . post <EOL> if not perms . user_can_alter_comment ( request . user , comment ) : <EOL> raise PermissionDenied <EOL> comment . delete ( ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ post . pk ] ) ) <EOL> @ login_required <EOL> def delete_post ( request , pid ) : <EOL> post = get_object_or_404 ( MDR . DiscussionPost , pk = pid ) <EOL> workgroup = post . workgroup <EOL> if not perms . user_can_alter_post ( request . user , post ) : <EOL> raise PermissionDenied <EOL> post . comments . all ( ) . delete ( ) <EOL> post . delete ( ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ workgroup . pk ] ) ) <EOL> @ login_required <EOL> def edit_comment ( request , cid ) : <EOL> comment = get_object_or_404 ( MDR . DiscussionComment , pk = cid ) <EOL> post = comment . post <EOL> if not perms . user_can_alter_comment ( request . user , comment ) : <EOL> raise PermissionDenied <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = MDRForms . discussions . CommentForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> comment . body = form . cleaned_data [ '<STR_LIT:body>' ] <EOL> comment . save ( ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ comment . post . pk ] ) + "<STR_LIT>" % comment . id ) <EOL> else : <EOL> form = MDRForms . discussions . CommentForm ( instance = comment ) <EOL> return render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : post , <EOL> '<STR_LIT>' : form } ) <EOL> @ login_required <EOL> def edit_post ( request , pid ) : <EOL> post = get_object_or_404 ( MDR . DiscussionPost , pk = pid ) <EOL> if not perms . user_can_alter_post ( request . user , post ) : <EOL> raise PermissionDenied <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = MDRForms . discussions . EditPostForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> post . title = form . cleaned_data [ '<STR_LIT:title>' ] <EOL> post . body = form . cleaned_data [ '<STR_LIT:body>' ] <EOL> post . save ( ) <EOL> post . relatedItems = form . cleaned_data [ '<STR_LIT>' ] <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ post . pk ] ) ) <EOL> else : <EOL> form = MDRForms . discussions . EditPostForm ( instance = post ) <EOL> return render ( request , "<STR_LIT>" , { "<STR_LIT>" : form , '<STR_LIT>' : post } ) </s>
