Browse Source

trying to switch from pony to sqlalchemy

master
Julio Biason 10 years ago
parent
commit
e374bebf60
  1. 16
      luncho/blueprints/users.py
  2. 29
      luncho/database.py
  3. 37
      luncho/server.py
  4. 5
      manage.py
  5. 2
      requirements.txt
  6. 6
      tests/users_tests.py

16
luncho/blueprints/users.py

@ -8,7 +8,7 @@ from flask import request
from flask import jsonify
# from flask import current_app
from pony.orm import commit
# from pony.orm import commit
from luncho.helpers import ForceJSON
@ -23,10 +23,14 @@ def create_user():
"""Create a new user. Request must be:
{ "username": "username", "full_name": "Full Name", "password": "hash" }"""
json = request.get_json(force=True)
new_user = User(username=json['username'],
fullname=json['full_name'],
passhash=json['password'],
validated=False)
commit()
# new_user = User(username=json['username'],
# fullname=json['full_name'],
# passhash=json['password'],
# validated=False)
User(username=json['username'],
fullname=json['full_name'],
passhash=json['password'],
validated=False)
# commit()
return jsonify(status='OK')

29
luncho/database.py

@ -1,29 +0,0 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import datetime
from flask import current_app
from pony.orm import Database
from pony.orm import PrimaryKey
from pony.orm import Optional
from pony.orm import Required
# from pony.orm import Set
db = Database("sqlite", current_app.config['SQLITE_FILENAME'], create_db=True)
class User(db.Entity):
"""Users."""
username = PrimaryKey(unicode)
fullname = Required(unicode)
passhash = Required(unicode)
token = Optional(unicode) # 1. if the user never logged in, they will
# not have a token.
# 2. This forces the user to have a single
# login everywhere, per day.
issue_date = Optional(datetime.datetime)
validated = Required(bool, default=False)
db.generate_mapping(create_tables=True)

37
luncho/server.py

@ -2,7 +2,6 @@
# -*- encoding: utf-8 -*-
import sys
import datetime
import logging
from flask import Flask
@ -12,7 +11,8 @@ from flask import Flask
# Config
# ----------------------------------------------------------------------
class Settings(object):
SQLITE_FILENAME = './luncho.db3'
SQLALCHEMY_DATABASE_URI = 'sqlite://./luncho.db3'
DEBUG = True
log = logging.getLogger('luncho.server')
@ -26,29 +26,22 @@ app.config.from_envvar('LUCNHO_CONFIG', True)
# ----------------------------------------------------------------------
# Database
# ----------------------------------------------------------------------
from pony.orm import db_session
from pony.orm import Database
from pony.orm import PrimaryKey
from pony.orm import Optional
from pony.orm import Required
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
db = Database("sqlite", app.config['SQLITE_FILENAME'], create_db=True)
class User(db.Model):
username = db.Column(db.String, primary_key=True)
full_name = db.Column(db.String, nullable=False)
passhash = db.Column(db.String, nullable=False)
token = db.Column(db.String)
issued_date = db.Column(db.Date)
validated = db.Column(db.Boolean, default=False)
class User(db.Entity):
"""Users."""
username = PrimaryKey(unicode)
fullname = Required(unicode)
passhash = Required(unicode)
token = Optional(unicode) # 1. if the user never logged in, they will
# not have a token.
# 2. This forces the user to have a single
# login everywhere, per day.
issue_date = Optional(datetime.datetime)
validated = Required(bool, default=False)
db.generate_mapping(create_tables=True)
app.wsgi_app = db_session(app.wsgi_app)
def __init__(self, username, full_name, passhash):
self.username = username
self.full_name = full_name
self.passhash = passhash
# ----------------------------------------------------------------------
# Blueprints

5
manage.py

@ -9,6 +9,11 @@ from luncho.server import app
manager = Manager(app)
@manager.command
def create_db():
"""Create the database."""
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
manager.run()

2
requirements.txt

@ -1,3 +1,3 @@
Flask
flask-script
pony
flask-sqlalchemy

6
tests/users_tests.py

@ -1,6 +1,7 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import os
import tempfile
import unittest
import json
@ -20,9 +21,8 @@ class TestUsers(unittest.TestCase):
print server.app.config['SQLITE_FILENAME']
self.app = server.app.test_client()
def tearDown(self):
# os.unlink(server.app.config['SQLITE_FILENAME'])
pass
# def tearDown(self):
# os.unlink(server.app.config['SQLITE_FILENAME'])
def test_create_user(self):
request = {'username': 'username',

Loading…
Cancel
Save