Advertisement
Guest User

Untitled

a guest
May 13th, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.25 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import cgi
  4. import cgitb;cgitb.enable()
  5. import os
  6. import pickle
  7. import sys
  8.  
  9. cgi_form = cgi.FieldStorage()
  10.  
  11. message = ''
  12.  
  13. def get_cgi(field, default = ""):
  14. return cgi_form.getfirst(field, default)
  15.  
  16. try:
  17. sys.stderr.write('abc: 1')
  18. input_file = open(
  19. os.path.join(os.path.dirname(__file__), '../../../russian/pickled'), 'rb')
  20. sys.stderr.write('abc: 2')
  21. state = pickle.load(input_file)
  22. sys.stderr.write('abc: 3')
  23. state['changed'] = False
  24. sys.stderr.write('abc: 4')
  25. state['loaded'] = True
  26. sys.stderr.write('abc: 5')
  27. except IOError:
  28. state = {}
  29. state['phrases'] = []
  30. state['changed'] = True
  31. state['loaded'] = False
  32. #except UnicodeDecodeError:
  33. #state = {}
  34. #state['phrases'] = []
  35. #state['changed'] = True
  36. state['loaded'] = False
  37.  
  38. if get_cgi('russian') and get_cgi('english'):
  39. state['phrases'].append([get_cgi('russian'), get_cgi('english')])
  40. message = 'Your changes have been saved.'
  41. state['changed'] = True
  42. elif get_cgi('english'):
  43. state['phrases'].append([None, get_cgi('english')])
  44. message = 'Your change has been saved.'
  45. state['changed'] = True
  46.  
  47. if get_cgi('mode') == 'edit':
  48. to_delete = []
  49. for index in range(len(state['phrases'])):
  50. if get_cgi('russian_' + str(index), None) != None:
  51. state['phrases'][index][0] = get_cgi('russian_' + str(index))
  52. if get_cgi('english_' + str(index), None) != None:
  53. state['phrases'][index][1] = get_cgi('english_' + str(index))
  54. if get_cgi('delete_' + str(index), None) != None:
  55. to_delete.insert(0, index)
  56. #to_delete.append(index)
  57. #to_delete.sort(lambda a, b: -cmp(a, b))
  58. for element in to_delete:
  59. del state['phrases'][element]
  60. state['changed'] = True
  61.  
  62. sys.stderr.write('abc: ' + repr(state))
  63.  
  64. if state['changed']:
  65. output_file = open(
  66. os.path.join(os.path.dirname(__file__), '../../../russian/pickled_new.' +
  67. str(os.getpid())), 'wb')
  68. pickle.dump(state, output_file)
  69. output_file.close()
  70. os.rename(
  71. os.path.join(os.path.dirname(__file__), '../../../russian/pickled_new.' +
  72. str(os.getpid())),
  73. os.path.join(os.path.dirname(__file__), '../../../russian/pickled'))
  74.  
  75. if get_cgi('mode') == 'add':
  76. print('''Content-type: text/html
  77.  
  78. <!DOCTYPE html>
  79. <html>
  80. <head>
  81. <meta charset='UTF-8' />
  82. <style type="text/css">
  83. body
  84. {
  85. font-family: Verdana, Arial, sans;
  86. }
  87. input[type=text]
  88. {
  89. width: 100%%;
  90. }
  91. div.message
  92. {
  93. background-color: silver;
  94. }
  95. </style>
  96. </head>
  97. <body>
  98. <div class='message'>
  99. %(message)s
  100. </div>
  101. <form action='' method='POST'>
  102. <p><strong>Russian:</strong><br />
  103. <input type='text' id='russian' name='russian'></p>
  104.  
  105. <p><strong>English:</strong><br />
  106. <input type='text' id='english' name='english'></p>
  107.  
  108. <p><input type='submit'>
  109. </form>
  110. <script src='/include/jquery.js'></script>
  111. <script>
  112. jQuery('#Russian').focus();
  113. </script>
  114. </body>
  115. </html>''' % locals())
  116. elif get_cgi('mode') == 'edit':
  117. edit_table = '<table>'
  118. edit_table += '<thead>'
  119. edit_table += '<th>Russian</th>'
  120. edit_table += '<th>English</th>'
  121. edit_table += '<th>Delete</th>'
  122. edit_table += '</thead>'
  123. edit_table += '<tbody>'
  124. for index in range(len(state['phrases'])):
  125. if state['phrases'][index][0]:
  126. russian = state['phrases'][index][0].replace('"', "''")
  127. else:
  128. russian = None
  129. english = state['phrases'][index][1].replace('"', "''")
  130. edit_table += '<tr>'
  131. edit_table += '<td>'
  132. if russian != None:
  133. edit_table += '''<input type='text' name='russian_%(index)d'
  134. id='russian_%(index)d' value="%(russian)s">''' % locals()
  135. edit_table += '</td>\n'
  136. edit_table += '<td>'
  137. edit_table += '''<input type='text' name='english_%(index)d'
  138. id='english_%(index)d' value="%(english)s">''' % locals()
  139. edit_table += '<td>'
  140. edit_table += '''<input type='checkbox' name='delete_%(index)d'
  141. id='delete_%(index)d'>''' % locals()
  142. edit_table += '</td>\n'
  143. edit_table += '</tr>'
  144. print ('''Content-Type: text/html
  145.  
  146. <!DOCTYPE html>
  147. <html>
  148. <head>
  149. <title>Edit Phrases</title>
  150. <meta charset='utf-8' />
  151. </head>
  152. <body>
  153. <form action='' name='edit' id='edit' />
  154. <input type='hidden' name='mode' value='edit' />
  155. %(edit_table)s
  156. <input type='submit'>
  157. </form>
  158. </body>
  159. </html>''' % locals())
  160. else:
  161. text = ''
  162. for phrase in state['phrases']:
  163. if phrase[0]:
  164. text += ('<p title="' + phrase[1].replace('"', "''") + '">' +
  165. phrase[0] + '</p>')
  166. else:
  167. text += '<h2>' + phrase[1] + '</h2>'
  168.  
  169. print ('''Content-type: text/html
  170.  
  171. <!DOCTYPE html>
  172. <html>
  173. <head>
  174. <title>The Divine Liturgy</title>
  175. <meta charset='utf-8'>
  176. <style type='text/css'>
  177. body
  178. {
  179. font-family: Verdana, Arial, sans;
  180. }
  181. div
  182. {
  183. background-color: #ffff00;
  184. }
  185. </style>
  186. <link rel='stylesheet' type='text/css'
  187. href='/js/jquery-ui-1.10.2.custom/css/smoothness/jquery-ui-1.10.2.custom.css'
  188. />
  189. </head>
  190. <body>
  191. %(text)s
  192. <script src='/js/vendor/jquery-1.8.2-min.js'></script>
  193.  
  194. <script
  195. src='/js/jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.min.js'></script>
  196. <script>
  197. jQuery(document).tooltip();
  198. </script>
  199. </body>
  200. </html>''' % locals())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement