1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 08:56:17 +00:00

Fix for peewee pre-3.15.4

This commit is contained in:
Alban Féron 2022-12-23 17:34:45 +01:00
parent b2c45ff03f
commit 50b98e641a
No known key found for this signature in database
GPG Key ID: 8CE0313646D16165

View File

@ -25,7 +25,7 @@ from peewee import (
IntegerField, IntegerField,
TextField, TextField,
) )
from peewee import CompositeKey, DatabaseProxy, MySQLDatabase from peewee import CompositeKey, DatabaseProxy, Model, MySQLDatabase
from peewee import fn from peewee import fn
from playhouse.db_url import parseresult_to_dict, schemes from playhouse.db_url import parseresult_to_dict, schemes
from urllib.parse import urlparse from urllib.parse import urlparse
@ -49,10 +49,15 @@ def PrimaryKeyField(**kwargs):
db = DatabaseProxy() db = DatabaseProxy()
db.Model._meta.legacy_table_names = False
class Meta(db.Model): class _Model(Model):
class Meta:
database = db
legacy_table_names = False
class Meta(_Model):
key = CharField(32, primary_key=True) key = CharField(32, primary_key=True)
value = CharField(256) value = CharField(256)
@ -64,23 +69,21 @@ class PathMixin:
path = kwargs.pop("path", None) path = kwargs.pop("path", None)
if path: if path:
kwargs["_path_hash"] = sha1(path.encode("utf-8")).digest() kwargs["_path_hash"] = sha1(path.encode("utf-8")).digest()
return db.Model.get.__func__(cls, *args, **kwargs) return _Model.get.__func__(cls, *args, **kwargs)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
if "path" in kwargs: if "path" in kwargs:
path = kwargs["path"] path = kwargs["path"]
kwargs["_path_hash"] = sha1(path.encode("utf-8")).digest() kwargs["_path_hash"] = sha1(path.encode("utf-8")).digest()
db.Model.__init__(self, *args, **kwargs) _Model.__init__(self, *args, **kwargs)
def __setattr__(self, attr, value): def __setattr__(self, attr, value):
db.Model.__setattr__(self, attr, value) _Model.__setattr__(self, attr, value)
if attr == "path": if attr == "path":
db.Model.__setattr__( _Model.__setattr__(self, "_path_hash", sha1(value.encode("utf-8")).digest())
self, "_path_hash", sha1(value.encode("utf-8")).digest()
)
class Folder(PathMixin, db.Model): class Folder(PathMixin, _Model):
id = AutoField() id = AutoField()
root = BooleanField() root = BooleanField()
name = CharField() name = CharField()
@ -177,7 +180,7 @@ class Folder(PathMixin, db.Model):
return total return total
class Artist(db.Model): class Artist(_Model):
id = PrimaryKeyField() id = PrimaryKeyField()
name = CharField() name = CharField()
@ -209,7 +212,7 @@ class Artist(db.Model):
) )
class Album(db.Model): class Album(_Model):
id = PrimaryKeyField() id = PrimaryKeyField()
name = CharField() name = CharField()
artist = ForeignKeyField(Artist, backref="albums") artist = ForeignKeyField(Artist, backref="albums")
@ -269,7 +272,7 @@ class Album(db.Model):
return cls.delete().where(cls.id.not_in(Track.select(Track.album))).execute() return cls.delete().where(cls.id.not_in(Track.select(Track.album))).execute()
class Track(PathMixin, db.Model): class Track(PathMixin, _Model):
id = PrimaryKeyField() id = PrimaryKeyField()
disc = IntegerField() disc = IntegerField()
number = IntegerField() number = IntegerField()
@ -377,7 +380,7 @@ class Track(PathMixin, db.Model):
return f"{self.album.artist.name}{self.album.name}{self.disc:02}{self.number:02}{self.title}".lower() return f"{self.album.artist.name}{self.album.name}{self.disc:02}{self.number:02}{self.title}".lower()
class User(db.Model): class User(_Model):
id = PrimaryKeyField() id = PrimaryKeyField()
name = CharField(64, unique=True) name = CharField(64, unique=True)
mail = CharField(null=True) mail = CharField(null=True)
@ -414,7 +417,7 @@ class User(db.Model):
} }
class ClientPrefs(db.Model): class ClientPrefs(_Model):
user = ForeignKeyField(User, backref="clients") user = ForeignKeyField(User, backref="clients")
client_name = CharField(32) client_name = CharField(32)
format = CharField(8, null=True) format = CharField(8, null=True)
@ -425,7 +428,7 @@ class ClientPrefs(db.Model):
def _make_starred_model(target_model): def _make_starred_model(target_model):
class Starred(db.Model): class Starred(_Model):
user = ForeignKeyField(User, backref="+") user = ForeignKeyField(User, backref="+")
starred = ForeignKeyField(target_model, backref="+") starred = ForeignKeyField(target_model, backref="+")
date = DateTimeField(default=now) date = DateTimeField(default=now)
@ -444,7 +447,7 @@ StarredTrack = _make_starred_model(Track)
def _make_rating_model(target_model): def _make_rating_model(target_model):
class Rating(db.Model): class Rating(_Model):
user = ForeignKeyField(User, backref="+") user = ForeignKeyField(User, backref="+")
rated = ForeignKeyField(target_model, backref="+") rated = ForeignKeyField(target_model, backref="+")
rating = IntegerField() # min=1, max=5 rating = IntegerField() # min=1, max=5
@ -460,7 +463,7 @@ RatingFolder = _make_rating_model(Folder)
RatingTrack = _make_rating_model(Track) RatingTrack = _make_rating_model(Track)
class ChatMessage(db.Model): class ChatMessage(_Model):
id = PrimaryKeyField() id = PrimaryKeyField()
user = ForeignKeyField(User, backref="+") user = ForeignKeyField(User, backref="+")
time = IntegerField(default=lambda: int(time.time())) time = IntegerField(default=lambda: int(time.time()))
@ -474,7 +477,7 @@ class ChatMessage(db.Model):
} }
class Playlist(db.Model): class Playlist(_Model):
id = PrimaryKeyField() id = PrimaryKeyField()
user = ForeignKeyField(User, backref="playlists") user = ForeignKeyField(User, backref="playlists")
name = CharField() name = CharField()
@ -547,7 +550,7 @@ class Playlist(db.Model):
self.tracks = ",".join(t for t in tracks if t) self.tracks = ",".join(t for t in tracks if t)
class RadioStation(db.Model): class RadioStation(_Model):
id = PrimaryKeyField() id = PrimaryKeyField()
stream_url = CharField() stream_url = CharField()
name = CharField() name = CharField()