Skip to content

Instantly share code, notes, and snippets.

View alexanderjulo's full-sized avatar

Alexander Jung-Loddenkemper alexanderjulo

View GitHub Profile
@alexanderjulo
alexanderjulo / chosenselect.py
Last active August 29, 2015 14:15
Customized WTForms widget for Selects with Chosen
import json
from wtforms.widgets import Select, HTMLString
class ChosenSelect(Select):
def __init__(self, multiple=False, renderer=None, options={}):
"""
Initiate the widget. This offers you two general options.
First off it allows you to configure the ChosenSelect to
import os
from datetime import datetime
from werkzeug.contrib.atom import AtomFeed
from flask import Flask, request, render_template, flash, url_for
www = Flask(__name__)
www.config['SECRET_KEY'] = 'kjsas fo98qw 83'
www.debug = True
from flaskext.uploads import UploadSet, IMAGES, configure_uploads
127.0.0.1 - - [20/May/2012 13:11:36] "GET /blog/ HTTP/1.1" 500 -
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/flask/app.py", line 1518, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1506, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/flask/app.py", line 1504, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1264, in full_dispatch_request
rv = self.handle_user_exception(e)
@alexanderjulo
alexanderjulo / gist:3084938
Created July 10, 2012 17:35
Example unittest testcase
import unittest
class ExampleTestCase(unittest.TestCase):
def setUp(self):
# open files, set up database and stuff
pass
def test_1(self):
# start all tests with test, so unittest finds it
#!/bin/bash
# enable virtual environment for python
source venv/bin/activate
# hide changes that are not in this commit
git stash -q --keep-index
./manage.py test
RESULT=$?
<div class="control-group {% if input.errors %}error{% endif %}">
{% if input.type == 'BooleanField' %}
<div class="controls">
<label for="{{ input.name }}" class="checkbox">
{{ input }}
{{ input.label.text }}
</label>
</div>
{% else %}
<label for="{{ input.name }}" class="control-label">{{ input.label.text }}</label>
from babel import support
def man_gettext(string, domain, **variables):
translations = support.Translations.load(os.path.join(www.root_path, 'translations'))
return translations.dgettext(domain, string) % variables
def man_lazy_gettext(string, domain, **variables):
from speaklater import make_lazy_string
return make_lazy_string(man_gettext, string, domain, **variables)
def hosturl(url):
host = www.config['HOST']
if host.endswith('/'):
host = host[0:-1]
if url.startswith('/'):
url = url[1:]
return '/'.join([host, url])
@alexanderjulo
alexanderjulo / permission.py
Created August 27, 2012 19:40
permissions approach
class User(object):
def permission_list(self):
permissions = Permission.query.filter_by(user_id=self).all()
l = []
for permission in permissions:
l.append((permission.to, permission.value))
return l
def permission_has(self, to):
@alexanderjulo
alexanderjulo / sqlping.py
Created August 29, 2012 20:45
sqlalchemy mysql cleanup
from sqlalchemy import exc
from sqlalchemy import event
from sqlalchemy.pool import Pool
@event.listens_for(Pool, "checkout")
def ping_connection(dbapi_connection, connection_record, connection_proxy):
cursor = dbapi_connection.cursor()
try:
cursor.execute("SELECT 1")
except: