source code of /small-scripts/school-stuff/homework.cgi.brain

Last modified
Lines 214
Indexable Yes

Parent directory Download CGIread sitemap Main page

Quick links: f mkhash

  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. import cgi
  4. import cloudauth
  5. import cgitb
  6. import os
  7. import leetfool
  8. from homework import *
  9. cgitb.enable()
  10. leetfool.assertuid()
  11. def mkhash(foo):
  12.     def f(x):
  13.         if x is not None:
  14.             return x
  15.         else:
  16.             return ''
  17.     return hash(''.join(
  18.                 [f(foo[key1]) for key1 in sorted([key2 for key2 in foo])]))
  19. # Handle input
  20. form = cgi.FieldStorage()
  21. if form.getfirst('redirect', None) is not None:
  22.     print('''Status: 302 Temporary Redirect
  23. Location: /homework.cgi
  24. Cache-Control: max-age=0, nocache, must-revalidate
  25. ''')
  26.     quit()
  27. passwd, key = form.getfirst('passwd', None), form.getfirst('key', None)
  28. if passwd is not None and key is not None:
  29.     if cloudauth.passwd_ok(passwd, key, 600):
  30.         # Done any homework?
  31.         remove = []
  32.         for i in range(len(homework)):
  33.             if form.getfirst(str(mkhash(homework[i])), '') == 'rm':
  34.                 remove.append(i)
  35.         # Do it backwards
  36.         for i in reversed(remove):
  37.             homework.pop(i)
  38.         
  39.         # MORE homework??
  40.         hw_time = form.getfirst('time', None)
  41.         hw_class = form.getfirst('class', '')
  42.         hw_homework = form.getfirst('homework', None)
  43.         add_homework(hw_time, hw_class, hw_homework, 'CGI')
  44.     else:
  45.         print('''Content-Type: text/plain
  46. You have two options: succeed and fail
  47. Guess which you chose.
  48. ''')
  49.         quit()
  50. if os.getenv('REQUEST_METHOD', '') == 'POST':
  51.     print('''Status: 303 See Other
  52. Location: /homework.cgi?redirect=yes
  53. Cache-Control: max-age=0, nocache, must-revalidate
  54. ''')
  55.     quit()
  56. print('''Content-Type: application/xhtml+xml; charset=UTF-8
  57. Cache-control: max-age=0, nocache, must-revalidate
  58. <!DOCTYPE html>
  59. <html xmlns="http://www.w3.org/1999/xhtml" lang="sv" xml:lang="sv">
  60.     <head>
  61.         <meta charset="UTF-8"/>
  62.         <title>TODO</title>
  63.         <meta name="robots" content="noindex,nofollow"/>
  64.         <meta name="viewport" content="width=device-width, initial-scale=1"/>
  65.         <style type="text/css">
  66.             body
  67.             {
  68.                 margin-left: 1em;
  69.                 color: #000;
  70.                 background-color: #fff;
  71.             }
  72.             
  73.             table
  74.             {
  75.                 border: 1px solid #666;
  76.             }
  77.             
  78.             td, th
  79.             {
  80.                 border-left: 1px solid #666;
  81.                 border-right: 1px solid #666;
  82.                 border-top: none;
  83.                 border-bottom: none;
  84.             }
  85.             
  86.             tr:nth-child(2n)
  87.             {
  88.                 background-color: #eee;
  89.             }
  90.         </style>
  91.     </head>
  92.     <body>
  93.         <table>
  94.             <tr>
  95.                 <td></td>
  96.                 <th>Måndag</th>
  97.                 <th>Tisdag</th>
  98.                 <th>Onsdag</th>
  99.                 <th>Torsdag</th>
  100.                 <th>Fredag</th>
  101.             </tr>''')
  102. # Simple short calendar.
  103. for y in (0, 7):
  104.     print('            <tr>')
  105.     print('                <td></td>')
  106.     for x in range(5):
  107.         print('                <td>{}</td>'.format(nthday(x+y-adj)))
  108.     print('            </tr>')
  109. # Classes table
  110. timeranges = [
  111.     '08:00-09:10',
  112.     '09:25-10:35',
  113.     '11:10-12:20',
  114.     '12:30-13:40',
  115.     '13:50-15:00',
  116. ]
  117. for timerange, row in zip(timeranges, classes):
  118.     print('            <tr>')
  119.     print('                <th>{}</th>'.format(timerange))
  120.     for col in row:
  121.         print('                <td>{}</td>'.format(col))
  122.     print('            </tr>')
  123. # Print out the homework list.
  124. print('''        </table>
  125.         <hr/>
  126.         <form action="homework.cgi" method="POST">
  127.             <table>
  128.                 <tr>
  129.                     <th>Radera</th>
  130.                     <th>Deathline</th>
  131.                     <th>Kurs</th>
  132.                     <th>Källa</th>
  133.                     <th>Läxa</th>
  134.                 </tr>''')
  135. urlesc = lambda x: x.replace(
  136.     '&amp;', '&').replace(
  137.     '&lt;', '<').replace(
  138.     '&gt;', '>').replace(
  139.     '&', '&amp;').replace(
  140.     '<', '&lt;').replace(
  141.     '>', '&gt;')
  142. for entry in homework:
  143.     print('''                <tr>
  144.                    <td><input type="checkbox" value="rm" name="{}"/></td>
  145.                    <td>{}</td>
  146.                    <td>{}</td>
  147.                    <td>{}</td>
  148.                    <td>{}</td>
  149.                </tr>'''.format(mkhash(entry),
  150.                 entry['time'],
  151.                 entry['class'],
  152.                 entry['src'],
  153.                 urlesc(entry['homework'])))
  154. print('           </table>')
  155. print('           <hr/>')
  156. # Add homework.
  157. # Password.
  158. key = cloudauth.mk_key()
  159. print('''           <input type="hidden" name="key" value="{}"/>
  160.             <table>
  161.                 <tr>
  162.                     <th>Deathline</th>
  163.                     <td>
  164.                         <i>
  165.                             För nästa lektion:
  166.                             Lämna tom och fyll i följande fält.
  167.                         </i><br/>
  168.                         <code>YYYY-MM-DD:P</code>
  169.                     </td>
  170.                     <td><input type="text" name="time"/></td>
  171.                 </tr>
  172.                 <tr>
  173.                     <th>KURS</th>
  174.                     <td>
  175.                         <i>Hela koden utan siffror och punkter, ex.</i>
  176.                         <code>TYB</code>
  177.                     </td>
  178.                     <td><input type="text" name="class"/></td>
  179.                 </tr>
  180.                 <tr>
  181.                     <th>Läxa</th>
  182.                     <td></td>
  183.                     <td><input type="text" name="homework"/></td>
  184.                 </tr>
  185.                 <tr>
  186.                     <th>Lösenord</th>
  187.                     <td><strong>({})</strong></td>
  188.                     <td><input type="password" name="passwd"/><br/></td>
  189.                 </tr>
  190.                 <tr>
  191.                     <th>Skicka</th>
  192.                     <td></td>
  193.                     <td><input type="submit"/></td>
  194.                 </tr>
  195.             </table>
  196.         </form>
  197.     </body>
  198. </html>'''.format(key, key.split('_')[0]))
  199.