/[debconf-data]/penta/frontaccounting_sync.py
ViewVC logotype

Contents of /penta/frontaccounting_sync.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1444 - (hide annotations) (download) (as text)
Tue Jun 15 22:22:58 2010 UTC (2 years, 11 months ago) by jimmy
File MIME type: text/x-python
File size: 3200 byte(s)
Hopefully the final version of the automated part of frontaccounting sync
1 jimmy 1439 import logging
2     import mechanize
3     import random
4     import re
5     import sys
6     import urllib
7 jimmy 1444 import dc_objs
8 jimmy 1439 from BeautifulSoup import BeautifulSoup
9    
10     FRONTACCOUNTING_URL = "https://debconf10.kaplowitz.org/account"
11     HTTP_USERNAME = "XXXXXX"
12     HTTP_PASSWORD = "XXXXXX"
13     APP_USERNAME = "XXXXXX"
14     APP_PASSWORD = "XXXXXX"
15    
16 jimmy 1444 CAT_MAPPINGS = { }
17     CAT_MAPPINGS.update(dict.fromkeys(dc_objs.CAT_B, 'Pays Own Bed & Food'))
18     CAT_MAPPINGS.update(dict.fromkeys(dc_objs.CAT_BA, 'Sponsored Bed Only'))
19     CAT_MAPPINGS.update(dict.fromkeys(dc_objs.CAT_BF, 'Sponsored Food Only'))
20     CAT_MAPPINGS.update(dict.fromkeys(dc_objs.CAT_BAF, 'Sponsored Bed & Food'))
21     CAT_MAPPINGS.update(dict.fromkeys(dc_objs.CAT_PRO, 'Professional'))
22     CAT_MAPPINGS.update(dict.fromkeys(dc_objs.CAT_COR, 'Corporate'))
23    
24 jimmy 1439 def clean_html(br):
25     data = br.response().get_data()
26     soup = BeautifulSoup(data)
27     response = mechanize.make_response(str(soup),
28     [("Content-Type", "text/html")], br.geturl(), 200, "OK")
29     br.set_response(response)
30    
31     def ajax_submit(br, name=None):
32     q = urllib.quote
33     # Yay adapting JavaScript weirdness
34     br.form.action += "?JsHttpRequest=0-xml"
35     req = mechanize.Request(br.form.action)
36     req.add_header('Content-Type', 'application/octet-stream; charset=UTF-8')
37     pairs = br.form.click_pairs()
38     body = "&".join(["%s=%s" % (q(k),q(v)) for (k,v) in br.form.click_pairs(name=name)])
39     #body = re.sub('&_[a-z_]+_update=%20', '', body)
40     if not "&submit=" in body:
41     body += "&submit=1"
42     body += "&_random=%f" % (random.random() * 1234567)
43     req.add_header('Content-Length', len(body))
44     req.add_data(body)
45     br.open(req)
46    
47     def ajax_select(br, label, name):
48     br.set_value_by_label(label, name=name) #XXX NAME
49     update_name = "_%s_update" % name
50     br.submit(name=update_name)
51     clean_html(br)
52     br.select_form(nr=0)
53    
54     br = mechanize.Browser()
55     br.add_password(FRONTACCOUNTING_URL, HTTP_USERNAME, HTTP_PASSWORD)
56     br.open(FRONTACCOUNTING_URL)
57    
58     br.select_form(name="loginform")
59     br["user_name_entry_field"] = APP_USERNAME
60     br["password"] = APP_PASSWORD
61     br.submit()
62    
63 jimmy 1444 People = dc_objs.get_people(where="dcvn.attend and dcvn.reconfirmed")
64 jimmy 1439
65 jimmy 1444 for P in people:
66     br.open(FRONTACCOUNTING_URL)
67     br.follow_link(text="Add and Manage Customers")
68 jimmy 1439
69 jimmy 1444 br.select_form(nr=0)
70     br["CustName"] = br["cust_ref"] = P.name
71     br["email"] = P.email
72     br["notes"] = "person_id: %d" % P.person_id
73     br.set_value_by_label([CAT_MAPPINGS[P.participant_category]], name="sales_type")
74     br.set_value_by_label(["Net 30"], name="payment_terms")
75     br.set_value_by_label(["Conference Registration"], name="dimension_id")
76 jimmy 1439
77 jimmy 1444 ajax_submit(br)
78 jimmy 1439
79 jimmy 1444 br.open(FRONTACCOUNTING_URL)
80     br.follow_link(text="Customer Branches")
81     clean_html(br)
82     br.select_form(nr=0)
83     ajax_select(br, [P.name], name="customer_id") #XXX NAME
84     ajax_submit(br, name="ADD_ITEM")
85    
86     # This part isn't working
87     #br.open(FRONTACCOUNTING_URL)
88     #br.follow_link(text="Direct Invoice")
89     #clean_html(br)
90     #br.select_form(nr=0)
91     #ajax_select(br, ["Test Monkey"], name="customer_id") #XXX NAME
92     ##ajax_select(br, ["Residence Hall Bed"], name="stock_id") #XXX line_item
93     ##br["qty"] = 6 #XXX line_item

  ViewVC Help
Powered by ViewVC 1.1.5