File: textfile.py

package info (click to toggle)
python-wikkid 0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 728 kB
  • sloc: python: 3,051; makefile: 12
file content (89 lines) | stat: -rw-r--r-- 2,840 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#
# Copyright (C) 2010 Wikkid Developers
#
# This software is licensed under the GNU Affero General Public License
# version 3 (see the file LICENSE).

"""View classes to control the rendering of text pages."""

import logging

from webob.exc import HTTPSeeOther

from wikkid.filestore import UpdateConflicts
from wikkid.interface.resource import ITextFile
from wikkid.view.edit import BaseEditView
from wikkid.view.wiki import format_content


class EditTextFile(BaseEditView):
    """The page shows the text content in a large edit field."""

    for_interface = ITextFile

    @property
    def rev_id(self):
        return self.context.last_modified_in_revision

    @property
    def content(self):
        # We want to pass unicode to the view.
        byte_string = self.context.get_bytes()
        try:
            return byte_string.decode('utf-8')
        except UnicodeDecodeError:
            try:
                return byte_string.decode('latin-1')
            except UnicodeDecodeError:
                return byte_string.decode('ascii', 'replace')


class SaveNewTextContent(BaseEditView):
    """Update the text of a file."""

    name = 'save'

    def _render(self, skin):
        """Save the text file.

        If it conflicts, render the edit, otherwise render the page (ideally
        redirect back to the plain page.
        """
        # TODO: barf on a GET
        # TODO: barf if there is no user.
        params = self.request.params
        content = params['content']
        description = params['description']
        rev_id = (
            params['rev-id'].encode('utf-8') if 'rev-id' in params else None)
        preview = params.get('preview', None)
        if preview is not None:
            self.rev_id = rev_id
            self.description = description
            self.content = content
            default_format = self.execution_context.default_format
            self.preview_content = format_content(
                content, self.context.base_name, default_format)
        else:
            try:
                self.context.put_bytes(
                    content.encode('utf-8'), self.user.committer_id, rev_id,
                    description)

                location = self.canonical_url(self.context)
                raise HTTPSeeOther(location=location)
            except UpdateConflicts as e:
                # Show the edit page again.
                logger = logging.getLogger('wikkid')
                logger.info('Conflicts detected: \n%r\n', e.content)
                self.rev_id = e.basis_rev
                self.content = e.content
                self.message = "Conflicts detected during merge."

        self.description = description
        return super(SaveNewTextContent, self)._render(skin)


class UpdateTextFile(SaveNewTextContent):

    for_interface = ITextFile