[952d9fb] | 1 | #!/usr/bin/env python |
---|
[004d06d] | 2 | |
---|
| 3 | import os |
---|
| 4 | import subprocess |
---|
| 5 | import sys |
---|
| 6 | import re |
---|
| 7 | import datetime |
---|
| 8 | from mechanize import Browser |
---|
| 9 | |
---|
| 10 | pdfviewer = 'evince' |
---|
| 11 | django_username = 'downloader' |
---|
| 12 | |
---|
| 13 | from settings import * |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | def id_predicate(value): |
---|
| 17 | def _predicate(form): |
---|
| 18 | if 'id' in form.attrs: |
---|
| 19 | if form.attrs['id'] == value: |
---|
| 20 | return True |
---|
| 21 | return False |
---|
| 22 | return _predicate |
---|
| 23 | |
---|
| 24 | def login2Admin(br): |
---|
| 25 | # Log in to the admin interface |
---|
| 26 | br.open(baseurl + 'admin/') |
---|
| 27 | assert br.viewing_html() |
---|
| 28 | sys.stderr.write("Viewing '%s'\n" % (br.title(), )) |
---|
| 29 | assert br.title() == "Log in | Django site admin" |
---|
| 30 | br.select_form(predicate=id_predicate('login-form')) |
---|
| 31 | # Browser passes through unknown attributes (including methods) |
---|
| 32 | # to the selected HTMLForm (from ClientForm). |
---|
| 33 | br["username"] = django_username # (the method here is __setitem__) |
---|
| 34 | br["password"] = password # (the method here is __setitem__) |
---|
| 35 | response2 = br.submit() # submit current form |
---|
| 36 | |
---|
| 37 | def getLaTeX(br, latex_file, ): |
---|
[feed77c] | 38 | br.open(baseurl + 'vouchers/generate/vouchers.tex') |
---|
[f8a5e4b] | 39 | if br.viewing_html(): |
---|
| 40 | print br.response().get_data() |
---|
| 41 | assert not br.viewing_html() |
---|
[004d06d] | 42 | latex_file.write(br.response().get_data()) |
---|
| 43 | |
---|
| 44 | if __name__ == '__main__': |
---|
| 45 | label = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M') |
---|
| 46 | texpath = pathtpl % { 'label':label, 'ext':'tex' } |
---|
| 47 | pdfpath = pathtpl % { 'label':label, 'ext':'pdf' } |
---|
| 48 | texdir, texfile = os.path.split(texpath) |
---|
| 49 | |
---|
| 50 | br = Browser() |
---|
| 51 | login2Admin(br) |
---|
| 52 | texfileobj = open(texpath, 'w') |
---|
| 53 | getLaTeX(br, texfileobj, ) |
---|
| 54 | texfileobj.close() |
---|
| 55 | |
---|
| 56 | subprocess.check_call(['pdflatex', texfile], cwd=texdir) |
---|
| 57 | subprocess.check_call(['pdflatex', texfile], cwd=texdir) |
---|
| 58 | subprocess.check_call([pdfviewer, pdfpath]) |
---|