~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/apps/browser/__init__.py

  • Committer: chadnickbok
  • Date: 2009-02-02 04:00:25 UTC
  • Revision ID: svn-v4:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1189
Adding the changes from my genshi branch into trunk.

Most apps now use the Genshi templating engine, in preparation
for future changes to dispatch

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
from ivle import (util, studpath)
32
32
import ivle.svn
33
33
 
 
34
import genshi
 
35
import genshi.core
 
36
import genshi.template
 
37
 
34
38
# url path for this app
35
39
THIS_APP = "files"
36
40
 
44
48
        redirectPath = util.make_path(os.path.join(THIS_APP,req.user.login))
45
49
        req.throw_redirect(util.make_path(redirectPath))
46
50
 
 
51
    ctx = genshi.template.Context()
 
52
 
47
53
    # Set request attributes
48
54
    req.content_type = "text/html"
49
55
    req.styles = [
74
80
            "The path specified is invalid.")
75
81
 
76
82
    # Start writing data
77
 
    req.write("""
78
 
<!-- Top bar section -->
79
83
 
80
 
<div id="topbar">
81
 
  <div id="path">
82
 
    """)
83
84
    # FIXME: This isn't completely reliable! We're not inside the jail, so we
84
85
    # can't know the type for sure. This is now only used for adding a / to the
85
86
    # end of displayed paths, so I'm leaving this although it will often break.
86
87
    isdir = os.path.isdir(localpath)
87
 
    presentpath(req, browsepath, isdir)
88
 
    req.write("""
89
 
  </div>
90
 
  <div id="actions1">
91
 
""")
92
 
    present_actions1(req)
93
 
    req.write("""  </div>
94
 
  <div id="actions2">
95
 
""")
96
 
    present_actions2(req)
97
 
    req.write("""  </div>
98
 
</div>
99
 
<!-- End topbar -->
100
 
 
101
 
<!-- Body. The JavaScript places content here relevant to the path -->
102
 
<div id="filesbody">
103
 
</div>
104
 
<!-- End body -->
105
 
""")
106
 
 
107
 
def presentpath(req, path, isdir):
108
 
    """
109
 
    Presents a path list (address bar inside the page) for clicking.
110
 
    Writes to req, expecting to have just written the opening div containing
111
 
    the listing.
112
 
    This will also have a revision indicator on the end, if we are viewing a
113
 
    revision.
114
 
    """
 
88
    ctx['isdir'] = isdir
 
89
    gen_path(req, browsepath, ctx)
 
90
    gen_actions(req, ctx)
 
91
    
 
92
    ctx['fileservice_action'] = util.make_path(os.path.join("fileservice", req.path))
 
93
    ctx['filename'] = cgi.escape(req.path)
 
94
 
 
95
    loader = genshi.template.TemplateLoader(".", auto_reload=True)
 
96
    tmpl = loader.load(util.make_local_path("apps/browser/template.html"))
 
97
    
 
98
    req.write(tmpl.generate(ctx).render('html')) #'xhtml', doctype='xhtml'))
 
99
 
 
100
#TODO: Move all this logic into the template
 
101
def gen_path(req, path, ctx):
 
102
 
115
103
    href_path = util.make_path(THIS_APP)
116
104
    nav_path = ""
117
 
 
118
105
    revision = ivle.svn.revision_from_string(
119
106
                     req.get_fieldstorage().getfirst('r'))
120
 
 
121
107
    try: 
122
108
        revno = revision.number
123
109
    except:
124
110
        revno = None
125
 
       
 
111
      
 
112
    ctx['revno'] = revno
 
113
    
126
114
    # Create all of the paths
127
115
    pathlist = path.split("/")
128
 
    segs_left = len(pathlist)
 
116
    ctx['paths'] = []
129
117
    for path_seg in pathlist:
130
118
        if path_seg == "":
131
119
            continue
132
 
        # Write a slash at the end unless this is the last path seg AND
133
 
        # it's not a directory.
134
 
        segs_left -= 1
135
 
        add_slash = segs_left != 0 or isdir
136
 
        # Make an 'a' element
 
120
        new_seg = {}
 
121
        
 
122
        nav_path = nav_path + path_seg
137
123
        href_path = href_path + '/' + path_seg
138
 
        nav_path = nav_path + path_seg
139
 
        if add_slash:
140
 
            nav_path = nav_path + '/'
141
 
        link = '<a href="%s" title="Navigate to %s">%s</a>' % (
142
 
            href_path if revno is None else href_path + '?r=%d' % revno,
143
 
            nav_path, path_seg)
144
 
        req.write(link)
145
 
        if add_slash:
146
 
            req.write('/')
147
 
 
148
 
    if revno is not None:
149
 
        req.write(' (revision %d)' % revno)
150
 
 
151
 
def present_actions1(req):
 
124
        
 
125
        new_seg['path'] = path_seg        
 
126
        new_seg['nav_path'] = nav_path
 
127
        new_seg['href_path'] = href_path
 
128
        if revno is not None:
 
129
            new_seg['href_path'] += '?r=%d' % revno
 
130
        
 
131
        ctx['paths'].append(new_seg)
 
132
 
 
133
 
 
134
def gen_actions(req, ctx):
152
135
    """
153
136
    Presents a set of links/buttons for the "actions1" row of the top bar.
154
137
    This is always exactly the same - the JavaScript will customize it later.
155
138
    """
156
139
    # Set up our actions. The second field of each group is whether to disable
157
140
    # the items by default.
158
 
    moreactions = [
 
141
    ctx['moreactions'] = [
159
142
      ('Publishing', True, [
160
143
        ('publish', ['Publish',          'Make it so this directory can be seen by anyone on the web']),
161
144
        ('share',   ['Share this file',  'Get a link to this published file, to give to friends']),
186
169
        ('svnlog',      ['View Log',       'View the log of commits of the selected file']),
187
170
      ])
188
171
    ]
189
 
 
190
 
    req.write("""    <a id="act_open" class="disabled">Open</a> :
191
 
    <a id="act_serve"
192
 
        title="View this file on the web, running your code if this is a CGI file"
193
 
        class="disabled" target="_blank">Serve</a> :
194
 
    <a id="act_run" title="Run the selected Python file in the console"
195
 
        class="disabled">Run</a> :
196
 
    <a id="act_download" class="choice">Download</a> :
197
 
    <a title="Refresh the current page" onclick="refresh()"
198
 
        class="choice">Refresh</a><span id="moreactions_area"> :
199
 
    <select id="moreactions" onchange="handle_moreactions()"
200
 
        onblur="handle_moreactions()">
201
 
      <option class="moreactions" value="top"
202
 
        selected="selected">More actions...</option>
203
 
""")
204
 
 
205
 
    for (name, disablement, group) in moreactions:
206
 
        if disablement:
207
 
            disable = 'class="disabled" disabled="disabled"'
208
 
        else:
209
 
            disable = ''
210
 
        req.write('<optgroup label="%s">' % name)
211
 
        for (id, bits) in group:
212
 
            req.write('<option id="act_%s" %s title="%s" value="%s">%s</option>'
213
 
                      % (id, disable, bits[1], id, bits[0]))
214
 
        req.write('</optgroup>')
215
 
 
216
 
    req.write('</select></span>')
217
 
 
218
 
def present_actions2(req):
219
 
    """
220
 
    Presents a set of links/buttons for the "actions2" row of the top bar.
221
 
    This depends on whether it is a directory (listing) or normal file
222
 
    (editor), but we'll let the JavaScript decide which.
223
 
    """
224
 
    req.write("""    <form id="actions2_directory"
225
 
          target="upload_iframe"
226
 
          action="%s"
227
 
          enctype="multipart/form-data" method="post">
228
 
      <p>
229
 
      <span id="selectallpanel">
230
 
      Select:
231
 
      <a onclick="action_selectall(true)"
232
 
          title="Select all files in this directory">All</a> :
233
 
      <a onclick="action_selectall(false)"
234
 
          title="Deselect all files in this directory">None</a>
235
 
      </span>
236
 
 
237
 
      <span style="display:none" id="uploadpanel">|
238
 
        <label for="upload_file">Upload file:</label>
239
 
        <input type="hidden" value="putfiles" name="action" />
240
 
        <input type="hidden" value="" name="path" />
241
 
        <input type="file" name="data" id="upload_file" />
242
 
        <input type="checkbox" checked="checked" value="true" name="unpack" id="unpack" />
243
 
        <label title="Extract files and directories from a ZIP file if one is uploaded" for="unpack">Unpack ZIP</label>
244
 
        <input type="button" onclick="show_uploadpanel(false)" value="Cancel" />
245
 
        <input type="submit" value="Upload" />
246
 
      </span>
247
 
      <!-- This iframe is for making a call to upload the file without
248
 
           refreshing the page. (It will refresh the listing). -->
249
 
      <iframe onload="upload_callback()" style="display: none;"
250
 
          name="upload_iframe" id="upload_iframe"></iframe>
251
 
      </p>
252
 
    </form>
253
 
""" % cgi.escape(util.make_path(os.path.join("fileservice", req.path))))
254
 
 
255
 
    req.write("""    <p id="actions2_file">
256
 
      <input type="button" id="save_button" value="Save" onclick="save_file('%s')" />
257
 
      <input type="button" id="saveas_button" value="Save As..." onclick="save_file_as('%s')" />
258
 
      <select id="highlighting_select" onchange="highlighting_changed(this)">
259
 
          <option value="text">Text</option>
260
 
          <option value="python">Python</option>
261
 
          <option value="html">HTML</option>
262
 
          <option value="javascript">JavaScript</option>
263
 
          <option value="css">CSS</option>
264
 
      </select>
265
 
    </p>
266
 
""" % ((cgi.escape(req.path),) * 2))