source: client/downloader.py @ 524b81e

client
Last change on this file since 524b81e was 004d06d, checked in by Alex Dehnert <adehnert@…>, 15 years ago

Script to download and PDF the vouchers

  • Property mode set to 100755
File size: 1.6 KB
Line 
1#!/usr/bin/python
2
3import os
4import subprocess
5import sys
6import re
7import datetime
8from mechanize import Browser
9
10pdfviewer = 'evince'
11django_username = 'downloader'
12
13from settings import *
14
15
16def 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
24def 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
37def getLaTeX(br, latex_file, ):
38    br.open(baseurl + 'vouchers/generate/')
39    latex_file.write(br.response().get_data())
40
41if __name__ == '__main__':
42    label = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M')
43    texpath  = pathtpl % { 'label':label, 'ext':'tex' }
44    pdfpath  = pathtpl % { 'label':label, 'ext':'pdf' }
45    texdir, texfile = os.path.split(texpath)
46
47    br = Browser()
48    login2Admin(br)
49    texfileobj = open(texpath, 'w')
50    getLaTeX(br, texfileobj, )
51    texfileobj.close()
52
53    subprocess.check_call(['pdflatex', texfile], cwd=texdir)
54    subprocess.check_call(['pdflatex', texfile], cwd=texdir)
55    subprocess.check_call([pdfviewer, pdfpath])
Note: See TracBrowser for help on using the repository browser.