Conversion tool is near from complete. Use Peewee as ORM

This commit is contained in:
Yax 2015-05-01 18:57:17 +02:00
parent b38d47228a
commit 7b5c0822ff
7 changed files with 140 additions and 19 deletions

24
app/models/comment.py Normal file
View file

@ -0,0 +1,24 @@
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from peewee import Model
from peewee import CharField
from peewee import DateTimeField
from peewee import IntegerField
from peewee import ForeignKeyField
from app.models.site import Site
from app.services.database import get_db
class Comment(Model):
url = CharField()
date = DateTimeField()
rel_index = IntegerField()
author_name = CharField()
author_email = CharField(default='')
author_site = CharField()
content = CharField()
site = ForeignKeyField(Site, related_name='site')
class Meta:
database = get_db()

15
app/models/site.py Normal file
View file

@ -0,0 +1,15 @@
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from peewee import Model
from peewee import CharField
from app.services.database import get_db
class Site(Model):
name = CharField(unique=True)
url = CharField()
token = CharField()
class Meta:
database = get_db()

39
app/services/database.py Normal file
View file

@ -0,0 +1,39 @@
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import hashlib
import config
import functools
from config import DB_URL
from playhouse.db_url import connect
def get_db():
return connect(DB_URL)
def provide_db(func):
@functools.wraps(func)
def new_function(*args, **kwargs):
return func(get_db(), *args, **kwargs)
return new_function
def hash(value):
string = '%s%s' % (value, config.SALT)
dk = hashlib.sha256(string.encode())
return dk.hexdigest()
@provide_db
def setup(db):
from app.models.user import User
db.create_tables([User], safe=True)
# create admin user if user table is empty
if User.select().count() == 0:
admin_user = User(username='admin', password=hash('admin'),
displayname='Admin')
admin_user.save()