<s> from __future__ import absolute_import , unicode_literals <EOL> import logging <EOL> from django . conf import settings <EOL> from django . contrib import messages <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . core . urlresolvers import reverse , reverse_lazy <EOL> from django . http import HttpResponseRedirect <EOL> from django . shortcuts import get_object_or_404 , render_to_response <EOL> from django . template import RequestContext <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from acls . models import AccessControlList <EOL> from common . generics import ( <EOL> AssignRemoveView , SingleObjectCreateView , SingleObjectDeleteView , <EOL> SingleObjectEditView , SingleObjectListView <EOL> ) <EOL> from documents . models import Document , DocumentType <EOL> from documents . permissions import permission_document_view <EOL> from documents . views import DocumentListView <EOL> from permissions import Permission <EOL> from . forms import SmartLinkConditionForm , SmartLinkForm <EOL> from . models import ResolvedSmartLink , SmartLink , SmartLinkCondition <EOL> from . permissions import ( <EOL> permission_smart_link_create , permission_smart_link_delete , <EOL> permission_smart_link_edit , permission_smart_link_view <EOL> ) <EOL> logger = logging . getLogger ( __name__ ) <EOL> class SetupSmartLinkDocumentTypesView ( AssignRemoveView ) : <EOL> decode_content_type = True <EOL> left_list_title = _ ( '<STR_LIT>' ) <EOL> right_list_title = _ ( '<STR_LIT>' ) <EOL> object_permission = permission_smart_link_edit <EOL> def add ( self , item ) : <EOL> self . get_object ( ) . document_types . add ( item ) <EOL> def get_object ( self ) : <EOL> return get_object_or_404 ( SmartLink , pk = self . kwargs [ '<STR_LIT>' ] ) <EOL> def left_list ( self ) : <EOL> return AssignRemoveView . generate_choices ( <EOL> DocumentType . objects . exclude ( <EOL> pk__in = self . get_object ( ) . document_types . all ( ) <EOL> ) <EOL> ) <EOL> def right_list ( self ) : <EOL> return AssignRemoveView . generate_choices ( <EOL> self . get_object ( ) . document_types . all ( ) <EOL> ) <EOL> def remove ( self , item ) : <EOL> self . get_object ( ) . document_types . remove ( item ) <EOL> def get_extra_context ( self ) : <EOL> return { <EOL> '<STR_LIT:object>' : self . get_object ( ) , <EOL> '<STR_LIT:title>' : _ ( <EOL> '<STR_LIT>' <EOL> ) % self . get_object ( ) <EOL> } <EOL> class ResolvedSmartLinkView ( DocumentListView ) : <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> self . document = get_object_or_404 ( <EOL> Document , pk = self . kwargs [ '<STR_LIT>' ] <EOL> ) <EOL> self . smart_link = get_object_or_404 ( <EOL> SmartLink , pk = self . kwargs [ '<STR_LIT>' ] <EOL> ) <EOL> try : <EOL> Permission . check_permissions ( <EOL> request . user , ( permission_document_view , ) <EOL> ) <EOL> except PermissionDenied : <EOL> AccessControlList . objects . check_access ( <EOL> permission_document_view , request . user , self . document <EOL> ) <EOL> try : <EOL> Permission . check_permissions ( <EOL> request . user , ( permission_smart_link_view , ) <EOL> ) <EOL> except PermissionDenied : <EOL> AccessControlList . objects . check_access ( <EOL> permission_smart_link_view , request . user , self . smart_link <EOL> ) <EOL> return super ( <EOL> ResolvedSmartLinkView , self <EOL> ) . dispatch ( request , * args , ** kwargs ) <EOL> def get_document_queryset ( self ) : <EOL> try : <EOL> queryset = self . smart_link . get_linked_document_for ( self . document ) <EOL> except Exception as exception : <EOL> queryset = Document . objects . none ( ) <EOL> if self . request . user . is_staff or self . request . user . is_superuser : <EOL> messages . error ( <EOL> self . request , _ ( '<STR_LIT>' % exception ) <EOL> ) <EOL> return queryset <EOL> def get_extra_context ( self ) : <EOL> dynamic_label = self . smart_link . get_dynamic_label ( self . document ) <EOL> if dynamic_label : <EOL> title = _ ( '<STR_LIT>' ) % dynamic_label <EOL> else : <EOL> title = _ ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) % { <EOL> '<STR_LIT>' : self . document , <EOL> '<STR_LIT>' : self . smart_link . label , <EOL> } <EOL> return { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:object>' : self . document , <EOL> '<STR_LIT:title>' : title , <EOL> } <EOL> class SmartLinkListView ( SingleObjectListView ) : <EOL> object_permission = permission_smart_link_view <EOL> def get_queryset ( self ) : <EOL> self . queryset = self . get_smart_link_queryset ( ) <EOL> return super ( SmartLinkListView , self ) . get_queryset ( ) <EOL> def get_smart_link_queryset ( self ) : <EOL> return SmartLink . objects . all ( ) <EOL> def get_extra_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:title>' : _ ( '<STR_LIT>' ) , <EOL> } <EOL> class DocumentSmartLinkListView ( SmartLinkListView ) : <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> self . document = get_object_or_404 ( Document , pk = self . kwargs [ '<STR_LIT>' ] ) <EOL> try : <EOL> Permission . check_permissions ( <EOL> request . user , ( permission_document_view , ) <EOL> ) <EOL> except PermissionDenied : <EOL> AccessControlList . objects . check_access ( <EOL> permission_document_view , request . user , self . document <EOL> ) <EOL> return super ( <EOL> DocumentSmartLinkListView , self <EOL> ) . dispatch ( request , * args , ** kwargs ) <EOL> def get_smart_link_queryset ( self ) : <EOL> return ResolvedSmartLink . objects . filter ( <EOL> document_types = self . document . document_type , enabled = True <EOL> ) <EOL> def get_extra_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . document , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:object>' : self . document , <EOL> '<STR_LIT:title>' : _ ( '<STR_LIT>' ) % self . document , <EOL> } <EOL> class SmartLinkCreateView ( SingleObjectCreateView ) : <EOL> extra_context = { '<STR_LIT:title>' : _ ( '<STR_LIT>' ) } <EOL> form_class = SmartLinkForm <EOL> post_action_redirect = reverse_lazy ( '<STR_LIT>' ) <EOL> view_permission = permission_smart_link_create <EOL> class SmartLinkEditView ( SingleObjectEditView ) : <EOL> form_class = SmartLinkForm <EOL> model = SmartLink <EOL> post_action_redirect = reverse_lazy ( '<STR_LIT>' ) <EOL> view_permission = permission_smart_link_edit <EOL> def get_extra_context ( self ) : <EOL> return { <EOL> '<STR_LIT:object>' : self . get_object ( ) , <EOL> '<STR_LIT:title>' : _ ( '<STR_LIT>' ) % self . get_object ( ) <EOL> } <EOL> class SmartLinkDeleteView ( SingleObjectDeleteView ) : <EOL> model = SmartLink <EOL> post_action_redirect = reverse_lazy ( '<STR_LIT>' ) <EOL> view_permission = permission_smart_link_delete <EOL> def get_extra_context ( self ) : <EOL> return { <EOL> '<STR_LIT:object>' : self . get_object ( ) , <EOL> '<STR_LIT:title>' : _ ( '<STR_LIT>' ) % self . get_object ( ) <EOL> } <EOL> class SmartLinkConditionListView ( SingleObjectListView ) : <EOL> view_permission = permission_smart_link_edit <EOL> def get_extra_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:object>' : self . get_smart_link ( ) , <EOL> '<STR_LIT:title>' : _ ( <EOL> '<STR_LIT>' <EOL> ) % self . get_smart_link ( ) , <EOL> } <EOL> def get_smart_link ( self ) : <EOL> return get_object_or_404 ( SmartLink , pk = self . kwargs [ '<STR_LIT>' ] ) <EOL> def get_queryset ( self ) : <EOL> return self . get_smart_link ( ) . conditions . all ( ) <EOL> def smart_link_condition_create ( request , smart_link_pk ) : <EOL> smart_link = get_object_or_404 ( SmartLink , pk = smart_link_pk ) <EOL> try : <EOL> Permission . check_permissions ( <EOL> request . user , ( permission_smart_link_edit , ) <EOL> ) <EOL> except PermissionDenied : <EOL> AccessControlList . objects . check_access ( <EOL> ( permission_smart_link_edit , ) , request . user , smart_link <EOL> ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = SmartLinkConditionForm ( data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> new_smart_link_condition = form . save ( commit = False ) <EOL> new_smart_link_condition . smart_link = smart_link <EOL> new_smart_link_condition . save ( ) <EOL> messages . success ( <EOL> request , _ ( <EOL> '<STR_LIT>' <EOL> ) % new_smart_link_condition <EOL> ) <EOL> return HttpResponseRedirect ( <EOL> reverse ( <EOL> '<STR_LIT>' , args = ( smart_link . pk , ) <EOL> ) <EOL> ) <EOL> else : <EOL> form = SmartLinkConditionForm ( ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:title>' : _ ( '<STR_LIT>' ) % smart_link , <EOL> '<STR_LIT:object>' : smart_link , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> def smart_link_condition_edit ( request , smart_link_condition_pk ) : <EOL> smart_link_condition = get_object_or_404 ( <EOL> SmartLinkCondition , pk = smart_link_condition_pk <EOL> ) <EOL> try : <EOL> Permission . check_permissions ( <EOL> request . user , ( permission_smart_link_edit , ) <EOL> ) <EOL> except PermissionDenied : <EOL> AccessControlList . objects . check_access ( <EOL> ( permission_smart_link_edit , ) , request . user , <EOL> smart_link_condition . smart_link <EOL> ) <EOL> next = request . POST . get ( '<STR_LIT>' , request . GET . get ( '<STR_LIT>' , request . META . get ( '<STR_LIT>' , reverse ( settings . LOGIN_REDIRECT_URL ) ) ) ) <EOL> previous = request . POST . get ( '<STR_LIT>' , request . GET . get ( '<STR_LIT>' , request . META . get ( '<STR_LIT>' , reverse ( settings . LOGIN_REDIRECT_URL ) ) ) ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = SmartLinkConditionForm ( <EOL> request . POST , instance = smart_link_condition <EOL> ) <EOL> if form . is_valid ( ) : <EOL> smart_link_condition = form . save ( ) <EOL> messages . success ( <EOL> request , _ ( <EOL> '<STR_LIT>' <EOL> ) % smart_link_condition <EOL> ) <EOL> return HttpResponseRedirect ( next ) <EOL> else : <EOL> form = SmartLinkConditionForm ( instance = smart_link_condition ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : smart_link_condition , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : ( '<STR_LIT:object>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : next , <EOL> '<STR_LIT:object>' : smart_link_condition . smart_link , <EOL> '<STR_LIT>' : previous , <EOL> '<STR_LIT:title>' : _ ( '<STR_LIT>' ) , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> def smart_link_condition_delete ( request , smart_link_condition_pk ) : <EOL> smart_link_condition = get_object_or_404 ( <EOL> SmartLinkCondition , pk = smart_link_condition_pk <EOL> ) <EOL> try : <EOL> Permission . check_permissions ( <EOL> request . user , ( permission_smart_link_edit , ) <EOL> ) <EOL> except PermissionDenied : <EOL> AccessControlList . objects . check_access ( <EOL> ( permission_smart_link_edit , ) , request . user , <EOL> smart_link_condition . smart_link <EOL> ) <EOL> next = request . POST . get ( '<STR_LIT>' , request . GET . get ( '<STR_LIT>' , request . META . get ( '<STR_LIT>' , reverse ( settings . LOGIN_REDIRECT_URL ) ) ) ) <EOL> previous = request . POST . get ( '<STR_LIT>' , request . GET . get ( '<STR_LIT>' , request . META . get ( '<STR_LIT>' , reverse ( settings . LOGIN_REDIRECT_URL ) ) ) ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> try : <EOL> smart_link_condition . delete ( ) <EOL> messages . success ( <EOL> request , _ ( <EOL> '<STR_LIT>' <EOL> ) % smart_link_condition <EOL> ) <EOL> except Exception as exception : <EOL> messages . error ( <EOL> request , _ ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) % { <EOL> '<STR_LIT>' : smart_link_condition , <EOL> '<STR_LIT>' : exception <EOL> } <EOL> ) <EOL> return HttpResponseRedirect ( next ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : smart_link_condition , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : ( '<STR_LIT:object>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : next , <EOL> '<STR_LIT:object>' : smart_link_condition . smart_link , <EOL> '<STR_LIT>' : previous , <EOL> '<STR_LIT:title>' : _ ( '<STR_LIT>' ) % smart_link_condition , <EOL> } , context_instance = RequestContext ( request ) ) </s>
