Move models to a separate file

This commit is contained in:
2021-06-03 00:31:22 +02:00
parent e603288bf2
commit 7e81ea0b74
3 changed files with 42 additions and 34 deletions

View File

@@ -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