def fetch(cls, person_id): with cls.db as cur: cur.execute(cls.select(where={'person_id': person_id})) if cur.rowcount: person = cls(one_to_dict(cur)) else: person = None return person
def get_user(): """Retreives a single user from a Database, and render it using a HTML template""" try: data = json.loads(request.data) except ValueError: return '', 400 else: email = data['customer']['email'] # For more complex queries, consider moving queries to a separate module with db as cur: cur.execute(select('auth_user', {'email': email}, select=('username'))) try: user = one_to_dict(cur) except TypeError: return '', 404 template = app.jinja_env.get_template('api/helpscout.html') return jsonify({'html': template.render(user)})
#!/usr/bin/env python # -*- coding: utf-8 -*- import psycopg2 from mosql.util import star from mosql.query import insert from mosql.db import Database, one_to_dict # We breed another insert with partial arguments. person_insert = insert.breed({'table': 'person'}) dave = { 'person_id': 'dave', 'name': 'Dave', } db = Database(psycopg2, host='127.0.0.1') with db as cur: cur.execute(person_insert(set=dave, returning=star)) print one_to_dict(cur) print assert 0, 'Rollback!'
#!/usr/bin/env python # -*- coding: utf-8 -*- import psycopg2 from mosql.util import star from mosql.query import insert from mosql.db import Database, one_to_dict # We breed another insert with partial arguments. person_insert = insert.breed({"table": "person"}) dave = {"person_id": "dave", "name": "Dave"} db = Database(psycopg2, host="127.0.0.1") with db as cur: cur.execute(person_insert(set=dave, returning=star)) print one_to_dict(cur) print assert 0, "Rollback!"