Move models to a separate file
This commit is contained in:
39
balises.py
39
balises.py
@@ -1,7 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from argparse import ArgumentParser
|
||||
from configparser import ConfigParser
|
||||
import datetime as dt
|
||||
|
||||
import bs4
|
||||
@@ -10,39 +9,11 @@ import requests
|
||||
import os
|
||||
import urllib.parse
|
||||
|
||||
from peewee import *
|
||||
from bottle import hook, request, route, run, view
|
||||
|
||||
|
||||
|
||||
# ------------------------------------------------------------
|
||||
conf = ConfigParser()
|
||||
conf.read(os.path.dirname(__file__) + '/balises.ini')
|
||||
|
||||
db = MySQLDatabase(conf['mysql'].get('database'),
|
||||
user=conf['mysql'].get('user'),
|
||||
password=conf['mysql'].get('password'),
|
||||
host=conf['mysql'].get('host', 'localhost'),
|
||||
port=conf['mysql'].getint('port', 3306))
|
||||
|
||||
class BaseModel(Model):
|
||||
class Meta:
|
||||
database = db
|
||||
|
||||
class Song(BaseModel):
|
||||
id = AutoField()
|
||||
artist = CharField(default='')
|
||||
title = CharField(default='')
|
||||
|
||||
class Meta:
|
||||
indexes = (
|
||||
(('artist', 'title'), True), # Unique on artist + title
|
||||
)
|
||||
|
||||
class AirCast(BaseModel):
|
||||
id = AutoField()
|
||||
date = DateTimeField()
|
||||
song = ForeignKeyField(Song, backref='dates')
|
||||
from config import conf
|
||||
import models
|
||||
from models import Song, AirCast, DoesNotExist
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
@@ -153,11 +124,11 @@ def set_locale():
|
||||
|
||||
@hook('before_request')
|
||||
def connect_to_db():
|
||||
db.connect()
|
||||
models.db.connect()
|
||||
|
||||
@hook('after_request')
|
||||
def close_db_connection():
|
||||
db.close()
|
||||
models.db.close()
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# Argument parsing
|
||||
|
||||
5
config.py
Normal file
5
config.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from configparser import ConfigParser
|
||||
import os
|
||||
|
||||
conf = ConfigParser()
|
||||
conf.read(os.path.dirname(__file__) + '/balises.ini')
|
||||
32
models.py
Normal file
32
models.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from config import conf
|
||||
|
||||
from peewee import MySQLDatabase
|
||||
from peewee import Model
|
||||
from peewee import AutoField, CharField, DateTimeField, ForeignKeyField
|
||||
|
||||
from peewee import DoesNotExist
|
||||
|
||||
db = MySQLDatabase(conf['mysql'].get('database'),
|
||||
user=conf['mysql'].get('user'),
|
||||
password=conf['mysql'].get('password'),
|
||||
host=conf['mysql'].get('host', 'localhost'),
|
||||
port=conf['mysql'].getint('port', 3306))
|
||||
|
||||
class BaseModel(Model):
|
||||
class Meta:
|
||||
database = db
|
||||
|
||||
class Song(BaseModel):
|
||||
id = AutoField()
|
||||
artist = CharField(default='')
|
||||
title = CharField(default='')
|
||||
|
||||
class Meta:
|
||||
indexes = (
|
||||
(('artist', 'title'), True), # Unique on artist + title
|
||||
)
|
||||
|
||||
class AirCast(BaseModel):
|
||||
id = AutoField()
|
||||
date = DateTimeField()
|
||||
song = ForeignKeyField(Song, backref='dates')
|
||||
Reference in New Issue
Block a user