Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/aaronsw/watchdog
Browse files Browse the repository at this point in the history
  • Loading branch information
jdthomas committed Oct 15, 2008
2 parents 75110f0 + 1847519 commit 0f3cf30
Show file tree
Hide file tree
Showing 25 changed files with 250 additions and 212 deletions.
2 changes: 2 additions & 0 deletions config.py
Expand Up @@ -3,3 +3,5 @@
#from address in mails sent by watchdog
from_address = '"watchdog.net" <info@watchdog.net>'
maildir_path = '/home/wathdog/Maildir'
send_errors_to = 'bugs@watchdog.net'
test_email = 'test@watchdog.net' #wyr test emails to go to this
18 changes: 10 additions & 8 deletions contacts.py
Expand Up @@ -31,9 +31,9 @@ def gmailLoginURL(email):

def msnLoginURL():
url = "https://consent.live.com/Delegation.aspx?RU=%s&ps=%s&pl=%s"
return_url = urllib.quote('http://0.0.0.0:8080/auth/msn')
return_url = urllib.quote(web.ctx.homedomain + '/auth/msn')
permissions = 'Contacts.View'
privacy_policy = urllib.quote('http://0.0.0.0:8080/privacy')
privacy_policy = urllib.quote(web.ctx.homedomain + '/privacy')
url = url % (return_url, permissions, privacy_policy)
#token = "appid=%s&ts=%s" % (getAppId(), time.time())
#url = url + urllib.quote(token)
Expand All @@ -51,7 +51,8 @@ def POST(self):
form = forms.loadcontactsform()
if form.validates(i):
session.email = email
session.pid = i.pid
session.url = i.url
session.title = i.title
if i.provider == 'yahoo':
ylogin_url = yahooLoginURL(email, '/WSLogin/V1/wslogin')
raise web.seeother(ylogin_url)
Expand All @@ -64,7 +65,8 @@ def POST(self):
else:
import petition
share_obj = petition.share()
return share_obj.GET(form)
emailform = forms.emailform()
return share_obj.GET(emailform, form)

def save_contacts(email, contacts, provider):
#Even if the user is not logged-in, but has an account with us, let him import contacts
Expand Down Expand Up @@ -130,7 +132,7 @@ def GET(self):
response = urllib2.urlopen(req).read()
contacts = self.get_contacts(response)
save_contacts(email, contacts, provider='YAHOO')
raise web.seeother('/c/%s/share' % (session.pid))
raise web.seeother('/share?url=%s&title=%s' % (session.url, session.title))

def get_text(elem):
#gets the text from XML DOM element `elem`
Expand Down Expand Up @@ -165,8 +167,8 @@ def GET(self):
response = urllib2.urlopen(request)
contacts = self.get_contacts(response)
save_contacts(email, contacts, provider='GOOGLE')
raise web.seeother('/c/%s/share' % (session.pid))

raise web.seeother('/share?url=%s&title=%s' % (session.url, session.title))
class auth_msn:
def get_consent(self, s):
d = {}
Expand Down Expand Up @@ -204,4 +206,4 @@ def POST(self):
contacts = self.get_contacts(response)
save_contacts(email, contacts, provider='MICROSOFT')

raise web.seeother('/c/%s/share' % (session.pid))
raise web.seeother('/share?url=%s&title=%s' % (session.url, session.title))
2 changes: 1 addition & 1 deletion import/Makefile
Expand Up @@ -92,7 +92,7 @@ database: $(LOADERS)
$(PYTHON) load/votesmart.py
touch $@

.wyr: .manual load/wyr.py $(CRAWL)/votesmart/wyr.json
.wyr: .schema load/wyr.py $(CRAWL)/votesmart/wyr.json
$(PYTHON) load/wyr.py
touch $@

Expand Down
31 changes: 17 additions & 14 deletions import/load/wyr.py
@@ -1,3 +1,4 @@
from __future__ import with_statement
import simplejson as json
from settings import db

Expand All @@ -6,23 +7,25 @@
types = dict(email='E', wyr='W', ima='I', zipauth='Z')

def load_wyr():
for distname, data in wyr.iteritems():
if data['contacttype'] not in types.keys():
continue
with db.transaction():
db.delete('pol_contacts', where='1=1')
for distname, data in wyr.iteritems():
if data['contacttype'] not in types.keys():
continue

if data['contacttype'] == 'wyr':
contact = 'https://forms.house.gov/wyr/welcome.shtml'
else:
contact = data['contact']
if data['contacttype'] == 'wyr':
contact = 'https://forms.house.gov/wyr/welcome.shtml'
else:
contact = data['contact']

pol = db.select(politician, what='id', where='district_id=$distname', vars=locals())[0].id
d = {'politician':pol,
'contact':contact,
'contacttype': types[data['contacttype']],
'captcha': data['captcha']
}
pol = db.select('politician', what='id', where='district_id=$distname', vars=locals())[0].id
d = {'politician':pol,
'contact':contact,
'contacttype': types[data['contacttype']],
'captcha': data['captcha']
}

db.insert('pol_contacts', seqname=False, **d)
db.insert('pol_contacts', seqname=False, **d)

if __name__ == "__main__":
load_wyr()
90 changes: 50 additions & 40 deletions petition.py
Expand Up @@ -3,9 +3,9 @@

import web
from utils import forms, helpers, auth
from settings import db, render, session
from settings import db, render, render_plain, session
from utils.auth import require_login
from utils.users import fill_user_details
from utils.users import fill_user_details, update_user_details
import config
from utils.wyrutils import CaptchaException, add_captcha

Expand All @@ -18,13 +18,10 @@
'/login', 'login',
'/signup', 'signup',
'/verify', 'checkID',
'/(.*)/share', 'share',
'/(.*)/signatories', 'signatories',
'/(.*)', 'petition'
)

render_plain = web.template.render('templates/') #without base, useful for sending mails

class redir:
def GET(self): raise web.seeother('/')

Expand Down Expand Up @@ -82,9 +79,11 @@ def create_petition(i, email, wyrform):
tocongress = i.get('tocongress', 'off') == 'on'
i.pid = i.pid.replace(' ', '_')
u = helpers.get_user_by_email(email)

db.insert('petition', seqname=False, id=i.pid, title=i.ptitle, description=i.msg,
try:
db.insert('petition', seqname=False, id=i.pid, title=i.ptitle, description=i.msg,
owner_id=u.id, to_congress=tocongress)
except:
return
signid = save_signature(i, i.pid, u.id, tocongress)

if tocongress and captcha_to_be_filled(i): wyrform.fill(signid=signid)
Expand Down Expand Up @@ -163,7 +162,7 @@ def POST(self):
lf, pf = forms.loginform(), forms.petitionform()
sf.fill(i), pf.fill(i), wf.fill(i)
return render.petitionlogin(lf, sf, pf, wf)
user = auth.new_user( i.fname, i.lname, i.email, i.password)
user = auth.new_user(i.email, i.password)
helpers.set_login_cookie(i.email)
try:
create_petition(i, i.email, wf)
Expand Down Expand Up @@ -200,7 +199,7 @@ def save_signature(i, pid, uid, tocongress=False):
user_id=uid, share_with=share_with,
petition_id=pid, comment=i.get('comment'),
sent_to_congress=msg_status, referrer=referrer)

update_user_details(i)
helpers.set_msg("Thanks for your signing! Why don't you tell your friends about it now?")
return signid
else:
Expand Down Expand Up @@ -300,10 +299,9 @@ def GET_edit(self, pid):
p = get_petition_by_id(pid)
u = helpers.get_user_by_email(user_email)
pform = forms.petitionform()
pform.fill(userid=u.id, email=user_email, pid=p.id, ptitle=p.title, msg=p.description)
pform.fill(userid=u.id, email=user_email, pid=p.id, ptitle=p.title, msg=p.description, tocongress=p.to_congress)
cform = forms.wyrform()
cform.fill(prefix=u.prefix, fname=u.fname, lname=u.lname, addr1=u.addr1,
addr2=u.addr2, city=u.city, zipcode=u.zip5, phone=u.phone)
fill_user_details(cform)
title = "Edit your petition"
return render.petitionform(pform, cform, title, target='/c/%s?m=edit' % (pid))
else:
Expand Down Expand Up @@ -334,7 +332,7 @@ def GET_unsign(self, pid):
def GET_delete(self, pid):
user_email = helpers.get_loggedin_email()
if is_author(user_email, pid):
msg = render_plain.confirm_deletion(pid)
msg = str(render_plain.confirm_deletion(pid))
helpers.set_msg(msg)
else:
login_link = '<a href="/u/login">Login</a>'
Expand Down Expand Up @@ -364,9 +362,9 @@ def POST_sign(self, pid):
i = web.input()
sform = forms.signform()
tocongress = to_congress(pid)
p = get_petition_by_id(pid)

if tocongress:
p = get_petition_by_id(pid)
i.pid, i.ptitle, i.msg = pid, p.title, p.description
wyrform = forms.wyrform()
wyr_valid = wyrform.validates(i)
Expand All @@ -385,7 +383,7 @@ def POST_sign(self, pid):
return self.GET(pid, signform=sform, wyrform=wyrform)
if signid:
sendmail_to_signatory(user, pid)
raise web.seeother('/%s/share' % pid)
raise web.seeother('/share?url=/c/%s&title=%s' % (pid, p.title), absolute=True)
else:
return self.GET(pid, signform=sform, wyrform=wyrform)

Expand All @@ -396,13 +394,13 @@ def POST_edit(self, pid):
pform = forms.petitionform()
pform.inputs = filter(lambda i: i.name != 'pid', pform.inputs)
wyrform = forms.wyrform()
i.email = helpers.get_loggedin_email()
wyr_valid = (not(tocongress) or wyrform.validates(i))
if not pform.validates(i) or not wyr_valid:
title = "Edit petition"
return render.petitionform(pform, wyrform, title, target='/c/%s?m=edit' % (pid))
db.update('petition', where='id=$pid', title=i.ptitle, description=i.msg, vars=locals())
db.update('users', where='id=$i.userid', prefix=i.prefix, fname=i.fname, lname=i.lname,
addr1=i.addr1, addr2=i.addr2, city=i.city, zip5=i.zipcode, phone=i.phone, vars=locals())
db.update('petition', where='id=$pid', title=i.ptitle, description=i.msg, to_congress=tocongress, vars=locals())
update_user_details(i)
raise web.seeother('/%s' % pid)

def POST_unsign(self, pid):
Expand Down Expand Up @@ -459,42 +457,54 @@ def signed(email, pid):
is_signatory = db.select('signatory', where='user_id=$user_id and petition_id=$pid', vars=locals())
return bool(is_signatory)


class share:
def GET(self, pid, emailform=None, loadcontactsform=None):
def GET(self, emailform=None, loadcontactsform=None):
i = web.input()
url = i.get('url', '/')
title = i.get('title', 'The good government site with teeth')

user_id = helpers.get_loggedin_userid()
contacts = get_contacts(user_id)
if (not contacts) and ('email' in session):
contacts = get_contacts(session.get('email'), by='email')

contacts = filter(lambda c: not signed(c.email, pid), contacts)
petition = db.select('petition', where='id=$pid', vars=locals())[0]
petition.url = 'http://watchdog.net/c/%s' %(pid)

page_or_petition = 'page'
if not emailform:
emailform = forms.emailform
track_id = helpers.get_trackid(user_id, pid)
msg = render_plain.share_petition_mail(petition, track_id)
emailform.fill(subject=petition.title, body=msg)

loadcontactsform = forms.loadcontactsform()

emailform = forms.emailform()
track_id, description = None, None
if url.startswith('/c/') and url != '/c/':
url = url.rstrip('/')
pid = web.lstrips(url, '/c/')
p = get_petition_by_id(pid)
description = p and p.description
track_id = helpers.get_trackid(user_id, pid)
contacts = filter(lambda c: not signed(c.email, pid), contacts)
page_or_petition = 'petition'

msg = render_plain.share_mail(title, url, description, track_id)
emailform.fill(subject=title, body=msg)

loadcontactsform = loadcontactsform or forms.loadcontactsform()

msg, msg_type = helpers.get_delete_msg()
return render.share_petition(petition, emailform,
contacts, loadcontactsform, msg)
return render.share(title, url, emailform,
contacts, loadcontactsform, page_or_petition, msg)

def POST(self, pid):
def POST(self):
i = web.input()
emailform = forms.emailform()
emailform, loadcontactsform = forms.emailform(), forms.loadcontactsform()
if emailform.validates(i):
pid, msg, subject = i.pid, i.body, i.subject
url, msg, subject = i.url, i.body, i.subject
emails = [e.strip() for e in i.emails.strip(', ').split(',')]
web.sendmail(config.from_address, emails, subject, msg)
helpers.set_msg('Thanks for sharing this petition with your friends!')
raise web.seeother('/%s' % (pid))
u = helpers.get_user_by_email(helpers.get_loggedin_email())
from_address = u and "%s %s <%s>" % (u.fname, u.lname, u.email) or config.from_address
web.sendmail(from_address, emails, subject, msg)
page_or_petition = url.startswith('/c/') and 'petition' or 'page'
helpers.set_msg('Thanks for sharing this %s with your friends!' % page_or_petition)
raise web.seeother(url)
else:
return self.GET(pid, emailform=emailform)

return self.GET(emailform=emailform, loadcontactsform=loadcontactsform)

app = web.application(urls, globals())

Expand Down
2 changes: 2 additions & 0 deletions settings.py
Expand Up @@ -3,8 +3,10 @@
import web

render = web.template.render('templates/', base='base')
render_plain = web.template.render('templates/') #without base, useful for sending mails
db = web.database(dbn=os.environ.get('DATABASE_ENGINE', 'postgres'),
db=os.environ.get('WATCHDOG_TABLE', 'watchdog_dev'))
production_mode = os.environ.get('PRODUCTION_MODE', False)

#@@@@ is temp directory really okay for sessions??
sess_store = tempfile.mkdtemp()
Expand Down
6 changes: 2 additions & 4 deletions static/css/style.css
Expand Up @@ -75,9 +75,9 @@ input.highlight:focus { background-color: #fdf6c5; border: 1px solid #EE4400; }
/* fieldsets */
fieldset { width: 510px; border: 1px solid #ccc; background: #F5F5FD; padding: 0 2em 2em 2em; margin: 0 0 2em 0; }
legend { font-weight: bold; font-size: 1.2em; padding: 0 0.5em; color:#EE4400; }
fieldset label { display: block; font-size: 0.95em; margin: 1em 0 0 0; color: #023; }
fieldset label { display: block; font-size: 0.95em; margin: 0.5em 0 0 0; color: #023; }
fieldset input { padding: 0.3em; font-weight: bold; font-size: 1.1em; max-width: 500px; margin: 0; color: #023; }
fieldset textarea { font-size: 1.1em; width: 505px; padding: 0.2em; height: 200px; margin: 0}
fieldset textarea { font-size: 1.1em; width: 505px; padding: 0.2em; margin: 0}
fieldset th { vertical-align: middle; }
fieldset { vertical-align: middle; }
fieldset .wrong { margin-left: 4px; }
Expand Down Expand Up @@ -243,8 +243,6 @@ h2.repred { color: #EE4400; margin-bottom: 6px; }
#loadcontacts #email { margin-bottom: 0.5em;}
#loadcontacts #provider span { display: block; }

.login { padding-right: 2em; border-right: 1px solid #ccc; }
.signup { padding-left: 2em; }
.login th, .signup th { font-weight: normal; }
.login fieldset, .signup fieldset { width: 250px; }
.login input, .signup input { width: 250px; }
Expand Down
2 changes: 1 addition & 1 deletion static/js/petition.js
Expand Up @@ -7,11 +7,11 @@ var ptitle_event = function fillURL(){
url = strip(this.value).toLowerCase().replace(/\s+/g, '-');
url = remove_punctuation(url);
$('#pid').val(url);
$('#pid').change();
return;
}

$('#ptitle').change(ptitle_event);
$('#ptitle').blur(ptitle_event);

$('#pid').change( function checkID(){
$.post('/c/verify', {pid: strip(this.value)},
Expand Down
6 changes: 3 additions & 3 deletions templates/about_api.html
Expand Up @@ -80,21 +80,21 @@ <h2>Content</h2>
</p>

<pre>
<strong>$ curl -s http://watchdog.net/us/ak-00 | head</strong>
<strong>\$ curl -s http://watchdog.net/us/ak-00 | head</strong>
&lt;html>
&lt;head>
&lt;title>AK-00: Alaska’s at-large congressional district (watchdog.net)&lt/title>
<em>[...]</em>

<strong>$ curl -s http://watchdog.net/us/ak-00.n3 | head</strong>
<strong>\$ curl -s http://watchdog.net/us/ak-00.n3 | head</strong>
@prefix : &lt;http://watchdog.net/about/api#> .

&lt;http://watchdog.net/us/AK-00> a :District;
:almanac &lt;http://nationaljournal.com/pubs/almanac/2008/people/ak/rep_ak01.htm>;
:area_sqmi 663267;
<em>[...]</em>

<strong>$ curl -s -H "Accept: application/json" http://watchdog.net/us/ak-00 | head</strong>
<strong>\$ curl -s -H "Accept: application/json" http://watchdog.net/us/ak-00 | head</strong>
[
{
"almanac": "http://nationaljournal.com/pubs/almanac/2008/people/ak/rep_ak01.htm",
Expand Down

0 comments on commit 0f3cf30

Please sign in to comment.