app/soc/views/document/show.py
changeset 477 8a8b1bd035c4
parent 398 aa1e786a0b1d
child 482 839740b061ad
equal deleted inserted replaced
476:3b0662786f95 477:8a8b1bd035c4
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Document viewers.
       
    18 
       
    19 public: how the general public sees a Document
       
    20 """
       
    21 
       
    22 __authors__ = [
       
    23   '"Todd Larsen" <tlarsen@google.com>',
       
    24   ]
       
    25 
       
    26 
       
    27 from soc.logic import out_of_band
       
    28 from soc.logic import path_link_name
       
    29 from soc.logic.models import document
       
    30 from soc.views import helper
       
    31 from soc.views import simple
       
    32 from soc.views.helper import decorators
       
    33 
       
    34 import soc.views.helper.responses
       
    35 import soc.views.helper.templates
       
    36 
       
    37 
       
    38 DEF_DOCS_PUBLIC_TMPL = 'soc/document/public.html'
       
    39 
       
    40 @decorators.view
       
    41 def public(request, page=None, partial_path=None, link_name=None,
       
    42            template=DEF_DOCS_PUBLIC_TMPL):
       
    43   """How the "general public" sees a Document.
       
    44 
       
    45   Args:
       
    46     request: the standard django request object
       
    47     page: a soc.logic.site.page.Page object which is abstraction that combines 
       
    48       a Django view with sidebar menu info
       
    49     partial_path: the Document's site-unique "path" extracted from the URL,
       
    50       minus the trailing link_name
       
    51     link_name: the last portion of the Document's site-unique "path"
       
    52       extracted from the URL
       
    53     template: the "sibling" template (or a search list of such templates)
       
    54       from which to construct the public.html template name (or names)
       
    55 
       
    56   Returns:
       
    57     A subclass of django.http.HttpResponse which either contains the form to
       
    58     be filled out, or a redirect to the correct view in the interface.
       
    59   """
       
    60   # create default template context for use with any templates
       
    61   context = helper.responses.getUniversalContext(request)
       
    62 
       
    63   # TODO: there eventually needs to be a call to some controller logic that
       
    64   #   implements some sort of access controls, based on the currently
       
    65   #   logged-in User's Roles, etc.
       
    66 
       
    67   # TODO: based on the User's Roles, Documents that the User can edit
       
    68   #   should display a link to a document edit form
       
    69   
       
    70   doc = None
       
    71 
       
    72   # try to fetch User entity corresponding to link_name if one exists
       
    73   path = path_link_name.combinePath([partial_path, link_name])
       
    74 
       
    75   # try to fetch Document entity corresponding to path if one exists    
       
    76   try:
       
    77     if path:
       
    78       doc = document.logic.getFromFields(partial_path=partial_path,
       
    79                                          link_name=link_name)
       
    80   except out_of_band.ErrorResponse, error:
       
    81     # show custom 404 page when Document path doesn't exist in Datastore
       
    82     return simple.errorResponse(request, page, error, template, context)
       
    83 
       
    84   doc.content = helper.templates.unescape(doc.content)
       
    85   context['entity'] = doc
       
    86 
       
    87   return helper.responses.respond(request, template, context)