<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> __copyright__ = '<STR_LIT>' <EOL> __licence__ = '<STR_LIT>' <EOL> __email__ = '<STR_LIT>' <EOL> from django . shortcuts import render , redirect , get_object_or_404 <EOL> from django . views . generic import View <EOL> from django . template import loader <EOL> from django . http import Http404 <EOL> from django . core . mail import mail_managers , BadHeaderError <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . contrib import messages <EOL> from django . contrib . auth import authenticate , login , logout <EOL> from django . contrib . auth . models import Group <EOL> from django . utils . translation import ugettext as _ <EOL> from ninecms . utils . render import NodeView <EOL> from ninecms . utils . perms import get_perms , set_perms <EOL> from ninecms . utils import status <EOL> from ninecms . models import Node , PageType , MenuItem <EOL> from ninecms . forms import ContactForm , LoginForm , RedirectForm , ContentTypePermissionsForm <EOL> class ContentNodeView ( NodeView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> node = get_object_or_404 ( Node , id = kwargs [ '<STR_LIT>' ] ) <EOL> if node . alias : <EOL> return redirect ( '<STR_LIT>' , url_alias = ( node . alias + '<STR_LIT:/>' ) , permanent = True ) <EOL> if not node . status and not request . user . has_perm ( '<STR_LIT>' ) : <EOL> raise PermissionDenied <EOL> return self . render ( node , request ) <EOL> class AliasView ( NodeView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if kwargs [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] == '<STR_LIT:/>' : <EOL> alias = kwargs [ '<STR_LIT>' ] [ : - <NUM_LIT:1> ] <EOL> if alias == '<STR_LIT:/>' : <EOL> return redirect ( '<STR_LIT>' , permanent = True ) <EOL> try : <EOL> node = self . get_node_by_alias ( alias , request ) <EOL> except IndexError : <EOL> raise Http404 <EOL> if not node . status and not request . user . has_perm ( '<STR_LIT>' ) : <EOL> raise PermissionDenied <EOL> if node . redirect : <EOL> return redirect ( node . get_redirect_path ( ) , permanent = True ) <EOL> return self . render ( node , request ) <EOL> else : <EOL> return redirect ( '<STR_LIT>' , url_alias = ( kwargs [ '<STR_LIT>' ] + '<STR_LIT:/>' ) , permanent = True ) <EOL> class IndexView ( NodeView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> node = self . get_node_by_alias ( '<STR_LIT:/>' , request ) <EOL> except IndexError : <EOL> messages . warning ( request , "<STR_LIT>" ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> return self . render ( node , request ) <EOL> class ContactView ( View ) : <EOL> """<STR_LIT>""" <EOL> form_class = ContactForm <EOL> def post ( self , request ) : <EOL> """<STR_LIT>""" <EOL> form = self . form_class ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> t = loader . get_template ( '<STR_LIT>' ) <EOL> c = { <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT:message>' : form . cleaned_data [ '<STR_LIT:message>' ] , <EOL> } <EOL> try : <EOL> mail_managers ( form . cleaned_data [ '<STR_LIT>' ] , t . render ( c ) ) <EOL> except BadHeaderError : <EOL> messages . error ( request , _ ( "<STR_LIT>" ) ) <EOL> else : <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return redirect ( form . cleaned_data [ '<STR_LIT>' ] ) <EOL> messages . warning ( request , _ ( "<STR_LIT>" ) ) <EOL> request . session [ '<STR_LIT>' ] = request . POST <EOL> return redirect ( form . cleaned_data [ '<STR_LIT>' ] ) <EOL> class LoginView ( View ) : <EOL> """<STR_LIT>""" <EOL> form_class = LoginForm <EOL> def post ( self , request ) : <EOL> """<STR_LIT>""" <EOL> form = self . form_class ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> user = authenticate ( username = form . cleaned_data [ '<STR_LIT:username>' ] , password = form . cleaned_data [ '<STR_LIT:password>' ] ) <EOL> if user is not None : <EOL> if user . is_active : <EOL> login ( request , user ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) % user . username ) <EOL> else : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> messages . warning ( request , msg ) <EOL> else : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> messages . warning ( request , msg ) <EOL> else : <EOL> messages . warning ( request , _ ( "<STR_LIT>" ) ) <EOL> request . session [ '<STR_LIT>' ] = request . POST <EOL> return redirect ( form . cleaned_data [ '<STR_LIT>' ] ) <EOL> class LogoutView ( View ) : <EOL> """<STR_LIT>""" <EOL> form_class = RedirectForm <EOL> def post ( self , request ) : <EOL> """<STR_LIT>""" <EOL> form = self . form_class ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> logout ( request ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return redirect ( form . cleaned_data [ '<STR_LIT>' ] ) <EOL> class ContentTypePermsView ( View ) : <EOL> """<STR_LIT>""" <EOL> permissions_form_class = ContentTypePermissionsForm <EOL> def get ( self , request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> page_type = get_object_or_404 ( PageType , id = kwargs [ '<STR_LIT>' ] ) <EOL> perms = get_perms ( page_type , list ( self . permissions_form_class ( ) . fields . keys ( ) ) , '<STR_LIT>' ) <EOL> permissions_form = self . permissions_form_class ( initial = perms ) <EOL> groups = Group . objects . all ( ) . count ( ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : permissions_form , <EOL> '<STR_LIT>' : groups , <EOL> '<STR_LIT>' : page_type , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' , False ) , <EOL> } ) <EOL> def post ( self , request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> page_type = get_object_or_404 ( PageType , id = kwargs [ '<STR_LIT>' ] ) <EOL> permissions_form = self . permissions_form_class ( request . POST ) <EOL> groups = Group . objects . all ( ) . count ( ) <EOL> if permissions_form . is_valid ( ) : <EOL> if request . user . has_perm ( ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) : <EOL> set_perms ( page_type , list ( permissions_form . fields . keys ( ) ) , '<STR_LIT>' , permissions_form . cleaned_data ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) % page_type . name ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> else : <EOL> messages . warning ( request , _ ( "<STR_LIT>" ) ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : permissions_form , <EOL> '<STR_LIT>' : groups , <EOL> '<STR_LIT>' : page_type , <EOL> } ) <EOL> class StatusView ( View ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return redirect ( '<STR_LIT>' , permanent = True ) <EOL> def post ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in request . POST : <EOL> MenuItem . objects . rebuild ( ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> if '<STR_LIT>' in request . POST : <EOL> status . cache_clear ( ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return redirect ( '<STR_LIT>' ) </s>
