Skip to content

Instantly share code, notes, and snippets.

View JonCooperWorks's full-sized avatar

JonCooperWorks

View GitHub Profile
@JonCooperWorks
JonCooperWorks / form_macros.html
Created August 4, 2012 06:05
Form Macros for Jinja2
{% macro render_field(field) %}
<dt>{{ field.label }}</dt>
<dd>{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class="alert alert-error">
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
@JonCooperWorks
JonCooperWorks / send_email.py
Created August 4, 2012 06:11
Send email using Python on Google App Engine via Mail API
def send_email(form, to, name):
recipient = "%s <%s>" % (name, to)
subject = form.subject.data
sender = "%s <%s>" % (form.name.data, users.get_current_user().email())
body = form.body.data
try:
mail.send_mail(sender=sender, to=recipient, subject=subject, body=body)
return True
except:
logging.error(traceback.extract_stack())
@JonCooperWorks
JonCooperWorks / validators.py
Created August 4, 2012 06:27
Custom WTForms Validators written in python
def is_phone_number(form, field):
digits = filter(lambda x: x.isdigit(),field.data)
if len(digits) != 10 and len(digits) != 7:
raise validators.ValidationError(u'Invalid phone number')
def is_valid_header(form, field):
if '\n' in field.data or '\r' in field.data:
raise validators.ValidationError(u'Email headers cannot contain newlines')
@JonCooperWorks
JonCooperWorks / filters.py
Created August 4, 2012 06:33
Jinja2 Filters
@evalcontextfilter
def to_phone_number(eval_ctx, value):
#Start by ensuring string has 10 digits
digits = filter(lambda x: x.isdigit(),value)
if len(digits) == 10:
phone_number = '(%s)%s-%s'%(digits[0:3], digits[3:6], digits[6:])
else:
phone_number = '%s-%s'%(digits[0:3], digits[3:])
if eval_ctx.autoescape:
return Markup(phone_number)
@JonCooperWorks
JonCooperWorks / boolfilter.py
Created August 4, 2012 06:41
Boolean to Yes/No filter
@evalcontextfilter
def boolval2yesno(eval_ctx, value, true="Yes", false="No"):
if value:
result = true
else:
result = false
if eval_ctx.autoescape:
result = Markup(result)
return result
@JonCooperWorks
JonCooperWorks / ourvle.py
Created September 15, 2012 05:45
A Python wrapper for the OurVLE student note portal at UWI.
#Usage
#Initiate a new instance of an OurVleBrowser
browser = OurVleBrowser()
'''
Returns dict in the form:
{
'name' : '[NAME]',
'courses' : [ { 'id' : '[COURSE_ID]', 'title' : '[COURSE_TITLE]', 'url' : '[COURSE_URL]' } ]
}
'''
@JonCooperWorks
JonCooperWorks / sas.py
Created September 17, 2012 02:54
A SAS wrapper to allow for timetable access
'''
sas.py
Logs in to a student's SAS account and gets their schedule, then parses the relevant information using regex, because
SAS's HTML is too ugly to use a parser.
Todo:
(!) Refactor times returned by the parser to datetime objects to allow for easy time-related operations.
Or not, because it's just a prototype, and that's a lot of effort for a proof-of-concept
'''
import urllib2
@JonCooperWorks
JonCooperWorks / password_harvester.py
Created September 19, 2012 05:18
Tool to harvest passwords from UWI's OurVLE system.
#!/usr/bin/env python
'''
UWI data harvester. Created by Jonathan Cooper.
Based loosly on idiocy.py by Jonty (https://github.com/Jonty/)
Dumps UWI student information sent over an open hotspot by scanning
traffic to OurVLE. This also works with SAS by scanning port 9010
instead of 80 and checking for POST requests to /pls/data_mona/twbkwbis.P_ValLogin
with host sas.uwimona.edu.jm:9010.
To run, just type
#Discrete Math Homework
import math
def fib_check(l):
def is_fibonacci(n):
phi = 0.5 + 0.5 * math.sqrt(5.0)
a = phi * n
return n == 0 or abs(round(a) - a) < 1.0 / n
#Check if each number in list is fibonacci
if sorted(l) == l:
body {
background: url('http://www.cs.washington.edu/education/courses/cse190m/12su/homework/2/background.png');
font-family: 'Tahoma', 'Verdana', 'sans-serif';
font-size: 8pt;
margin: 0;
padding: 0;
}
dt {
font-weight: bold;