app/soc/views/document/show.py
author Todd Larsen <tlarsen@google.com>
Thu, 20 Nov 2008 18:50:30 +0000
changeset 512 aae25d2b4464
parent 500 44ea4620c5c0
child 513 3c1e16637ad7
permissions -rw-r--r--
Rename link_name to link_id everywhere, regardless of case (so LINK_NAME becomes LINK_ID, "link name" becomes "link id", etc.). Also, rename LINKNAME to LINK_ID. Patch by: Todd Larsen
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
238
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""Document viewers.
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
public: how the general public sees a Document
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
"""
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
__authors__ = [
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
  '"Todd Larsen" <tlarsen@google.com>',
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
  ]
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    26
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
from soc.logic import out_of_band
336
9d5b8880260f Fix not working docs show view. Document show view wasn't updated when the logic refactor was made.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    28
from soc.logic import path_link_name
9d5b8880260f Fix not working docs show view. Document show view wasn't updated when the logic refactor was made.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    29
from soc.logic.models import document
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 298
diff changeset
    30
from soc.views import helper
238
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
from soc.views import simple
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 361
diff changeset
    32
from soc.views.helper import decorators
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 298
diff changeset
    33
272
00cea07656c0 Move helpers/response_helpers.py to helper/responses.py.
Todd Larsen <tlarsen@google.com>
parents: 270
diff changeset
    34
import soc.views.helper.responses
270
7dd6d8347b56 Move helpers/template_helpers.py to helper/templates.py.
Todd Larsen <tlarsen@google.com>
parents: 242
diff changeset
    35
import soc.views.helper.templates
238
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
477
8a8b1bd035c4 Moved from docs -> document
Sverre Rabbelier <srabbelier@gmail.com>
parents: 398
diff changeset
    38
DEF_DOCS_PUBLIC_TMPL = 'soc/document/public.html'
238
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 361
diff changeset
    40
@decorators.view
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 500
diff changeset
    41
def public(request, page_name=None, partial_path=None, link_id=None,
238
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
           template=DEF_DOCS_PUBLIC_TMPL):
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
  """How the "general public" sees a Document.
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
  Args:
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
    request: the standard django request object
500
44ea4620c5c0 Replace old page parameter doc string description with new one for page_name.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 482
diff changeset
    47
    page_name: the page name displayed in templates as page and header title
238
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
    partial_path: the Document's site-unique "path" extracted from the URL,
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 500
diff changeset
    49
      minus the trailing link_id
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 500
diff changeset
    50
    link_id: the last portion of the Document's site-unique "path"
238
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
      extracted from the URL
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
    template: the "sibling" template (or a search list of such templates)
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
      from which to construct the public.html template name (or names)
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    54
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
  Returns:
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
    A subclass of django.http.HttpResponse which either contains the form to
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
    be filled out, or a redirect to the correct view in the interface.
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
  """
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
  # create default template context for use with any templates
272
00cea07656c0 Move helpers/response_helpers.py to helper/responses.py.
Todd Larsen <tlarsen@google.com>
parents: 270
diff changeset
    60
  context = helper.responses.getUniversalContext(request)
238
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
242
17984abf0c74 Some TODOs on access control that I didn't want to forget.
Todd Larsen <tlarsen@google.com>
parents: 238
diff changeset
    62
  # TODO: there eventually needs to be a call to some controller logic that
17984abf0c74 Some TODOs on access control that I didn't want to forget.
Todd Larsen <tlarsen@google.com>
parents: 238
diff changeset
    63
  #   implements some sort of access controls, based on the currently
17984abf0c74 Some TODOs on access control that I didn't want to forget.
Todd Larsen <tlarsen@google.com>
parents: 238
diff changeset
    64
  #   logged-in User's Roles, etc.
17984abf0c74 Some TODOs on access control that I didn't want to forget.
Todd Larsen <tlarsen@google.com>
parents: 238
diff changeset
    65
17984abf0c74 Some TODOs on access control that I didn't want to forget.
Todd Larsen <tlarsen@google.com>
parents: 238
diff changeset
    66
  # TODO: based on the User's Roles, Documents that the User can edit
17984abf0c74 Some TODOs on access control that I didn't want to forget.
Todd Larsen <tlarsen@google.com>
parents: 238
diff changeset
    67
  #   should display a link to a document edit form
336
9d5b8880260f Fix not working docs show view. Document show view wasn't updated when the logic refactor was made.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    68
  
9d5b8880260f Fix not working docs show view. Document show view wasn't updated when the logic refactor was made.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    69
  doc = None
242
17984abf0c74 Some TODOs on access control that I didn't want to forget.
Todd Larsen <tlarsen@google.com>
parents: 238
diff changeset
    70
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 500
diff changeset
    71
  # try to fetch User entity corresponding to link_id if one exists
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 500
diff changeset
    72
  path = path_link_name.combinePath([partial_path, link_id])
336
9d5b8880260f Fix not working docs show view. Document show view wasn't updated when the logic refactor was made.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    73
9d5b8880260f Fix not working docs show view. Document show view wasn't updated when the logic refactor was made.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    74
  # try to fetch Document entity corresponding to path if one exists    
238
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    75
  try:
336
9d5b8880260f Fix not working docs show view. Document show view wasn't updated when the logic refactor was made.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    76
    if path:
9d5b8880260f Fix not working docs show view. Document show view wasn't updated when the logic refactor was made.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
    77
      doc = document.logic.getFromFields(partial_path=partial_path,
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 500
diff changeset
    78
                                         link_id=link_id)
238
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    79
  except out_of_band.ErrorResponse, error:
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    80
    # show custom 404 page when Document path doesn't exist in Datastore
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 477
diff changeset
    81
    return simple.errorResponse(request, page_name, error, template, context)
238
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
270
7dd6d8347b56 Move helpers/template_helpers.py to helper/templates.py.
Todd Larsen <tlarsen@google.com>
parents: 242
diff changeset
    83
  doc.content = helper.templates.unescape(doc.content)
398
aa1e786a0b1d Rename 'existing_doc' and 'document' to entity
Sverre Rabbelier <srabbelier@gmail.com>
parents: 365
diff changeset
    84
  context['entity'] = doc
238
d6b533f99a45 A very simplistic Document viewer.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 361
diff changeset
    86
  return helper.responses.respond(request, template, context)