-
Notifications
You must be signed in to change notification settings - Fork 20
/
open.py
executable file
·141 lines (118 loc) · 4.56 KB
/
open.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Basic interface to choose a user file to open. Only the logged-in user's file
is opened and file availability depends on an administrator assigning the document
to the user. Other versions of each document's annotation are visible to those users.
Author: Amir Zeldes
"""
import cgitb
import cgi
import _version
from modules.logintools import login
from modules.configobj import ConfigObj
from modules.pathutils import *
from modules.rstweb_sql import *
def open_main(user, admin, mode, **kwargs):
cpout = ""
scriptpath = os.path.dirname(os.path.realpath(__file__)) + os.sep
userdir = scriptpath + "users" + os.sep
theform = kwargs
cgitb.enable()
config = ConfigObj(userdir + 'config.ini')
templatedir = scriptpath + config['controltemplates'].replace("/",os.sep)
template = "main_header.html"
header = readfile(templatedir+template)
header = header.replace("**page_title**","Open a file for editing")
header = header.replace("**user**",user)
if mode == "server":
cpout += "Content-Type: text/html\n\n\n"
header = header.replace("**logout_control**",'(<a href="logout.py">log out</a>)')
else:
header = header.replace("**logout_control**",'')
cpout += header
if "current_doc" in theform:
current_doc = theform["current_doc"]
current_project = theform["current_project"]
else:
current_doc = ""
current_project = ""
edit_bar = "edit_bar.html"
edit_bar = readfile(templatedir+edit_bar)
edit_bar = edit_bar.replace("**doc**",current_doc)
edit_bar = edit_bar.replace("**project**",current_project)
edit_bar = edit_bar.replace("**structure_disabled**",'')
edit_bar = edit_bar.replace("**segment_disabled**",'')
edit_bar = edit_bar.replace("**relations_disabled**",'')
if mode == "server":
edit_bar = edit_bar.replace("**submit_target**",'structure.py')
else:
edit_bar = edit_bar.replace("**submit_target**",'structure')
edit_bar = edit_bar.replace("**action_type**",'')
edit_bar = edit_bar.replace("**serve_mode**",mode)
edit_bar = edit_bar.replace("**quickexp_disabled**",'disabled="disabled"')
edit_bar = edit_bar.replace("**screenshot_disabled**",'disabled="disabled"')
edit_bar = edit_bar.replace("**open_disabled**",'disabled="disabled"')
edit_bar = edit_bar.replace("**reset_disabled**",'disabled="disabled"')
edit_bar = edit_bar.replace("**save_disabled**",'disabled="disabled"')
edit_bar = edit_bar.replace("**undo_disabled**",'disabled="disabled"')
edit_bar = edit_bar.replace("**redo_disabled**",'disabled="disabled"')
edit_bar = edit_bar.replace('id="nav_open" class="nav_button"','id="nav_open" class="nav_button nav_button_inset"')
if admin == "0":
edit_bar = edit_bar.replace("**admin_disabled**",'disabled="disabled"')
else:
edit_bar = edit_bar.replace("**admin_disabled**",'')
cpout += edit_bar
help = "help.html"
help = readfile(templatedir+help)
help_match = re.search(r'(<div id="help_open".*?</div>)',help,re.MULTILINE|re.DOTALL)
help = help_match.group(1)
cpout += help
about = "about.html"
about = readfile(templatedir+about)
about = about.replace("**version**", _version.__version__)
cpout += about
cpout += "<h2>Current Documents</h2>"
cpout += '<p>List of documents you are authorized to view:</p>'
docs = get_docs_by_project(user)
if not docs:
cpout += "<p>No documents have been assigned for user name: <b>" + user + "</b></p>"
else:
cpout += '<select id="doclist" name="doclist" class="doclist" size="15">\n'
project_group=""
for doc in docs:
if project_group!=doc[1]:
if project_group !="":
cpout += '</optgroup>\n'
project_group = doc[1]
cpout += '<optgroup label="'+doc[1]+'">\n'
cpout += '\t<option value="'+doc[1]+"/"+doc[0]+'">'+doc[0]+'</option>\n'
cpout += '</optgroup>\n</select>\n<br/>'
cpout += '''<button class="nav_button" onclick="do_open(document.getElementById('doclist').value);">Open file</button>'''
cpout += '''
</body>
</html>
'''
if mode != "server":
cpout = cpout.replace(".py","")
return cpout
# Main script when running from Apache
def open_main_server():
thisscript = os.environ.get('SCRIPT_NAME', '')
action = None
theform = cgi.FieldStorage()
scriptpath = os.path.dirname(os.path.realpath(__file__)) + os.sep
userdir = scriptpath + "users" + os.sep
action, userconfig = login(theform, userdir, thisscript, action)
user = userconfig["username"]
admin = userconfig["admin"]
kwargs={}
for key in theform:
kwargs[key] = theform[key].value
print(open_main(user, admin, 'server', **kwargs))
if "/" in os.environ.get('SCRIPT_NAME', ''):
mode = "server"
else:
mode = "local"
if mode == "server":
open_main_server()