fixing slugs
This commit is contained in:
+22
-1
@@ -1,5 +1,26 @@
|
||||
import nanoid
|
||||
from nanoid_dictionary import human_alphabet
|
||||
|
||||
from pyramid_sqlalchemy import BaseObject
|
||||
from wtforms import validators
|
||||
from slugify import slugify
|
||||
|
||||
from sqlalchemy import Column
|
||||
from sqlalchemy import String
|
||||
|
||||
def genslug():
|
||||
return nanoid.generate(human_alphabet[2:], 5)
|
||||
|
||||
|
||||
class SlugMixin:
|
||||
slug = Column(String, index=True, unique=True, default=genslug)
|
||||
|
||||
@property
|
||||
def uri(self):
|
||||
return '-'.join([
|
||||
self.slug,
|
||||
slugify(self.name.title().replace(' ', ''), ok='', only_ascii=True, lower=False)
|
||||
])
|
||||
|
||||
|
||||
class IterableMixin:
|
||||
@@ -50,4 +71,4 @@ class FormValidatorMixin:
|
||||
|
||||
|
||||
# class Table(*Bases):
|
||||
Bases = [BaseObject, IterableMixin, FormValidatorMixin]
|
||||
Bases = [BaseObject, IterableMixin, FormValidatorMixin, SlugMixin]
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import logging
|
||||
import transaction
|
||||
|
||||
from ttfrog.db.manager import db
|
||||
from ttfrog.db import schema
|
||||
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.inspection import inspect
|
||||
|
||||
# move this to json or whatever
|
||||
data = {
|
||||
@@ -33,7 +31,6 @@ def bootstrap():
|
||||
try:
|
||||
with db.transaction():
|
||||
db.session.add(obj)
|
||||
obj.slug = db.slugify(rec)
|
||||
except IntegrityError as e:
|
||||
if 'UNIQUE constraint failed' in str(e):
|
||||
logging.info(f"Skipping existing {table} {obj}")
|
||||
|
||||
@@ -30,7 +30,7 @@ class SQLDatabaseManager:
|
||||
def engine(self):
|
||||
return create_engine(self.url)
|
||||
|
||||
@property
|
||||
@cached_property
|
||||
def session(self):
|
||||
return Session
|
||||
|
||||
|
||||
+6
-9
@@ -2,7 +2,6 @@ from sqlalchemy import Column
|
||||
from sqlalchemy import Integer
|
||||
from sqlalchemy import String
|
||||
from sqlalchemy import ForeignKey
|
||||
from sqlalchemy import CheckConstraint
|
||||
# from sqlalchemy import PrimaryKeyConstraint
|
||||
# from sqlalchemy import DateTime
|
||||
|
||||
@@ -14,20 +13,18 @@ class Ancestry(*Bases):
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
name = Column(String, index=True, unique=True)
|
||||
slug = Column(String, index=True, unique=True)
|
||||
|
||||
|
||||
class Character(*Bases):
|
||||
__tablename__ = "character"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
slug = Column(String, index=True, unique=True)
|
||||
ancestry = Column(String, ForeignKey("ancestry.name"), nullable=False)
|
||||
name = Column(String(255), nullable=False)
|
||||
level = Column(Integer, nullable=False, info={'min': 1, 'max': 20})
|
||||
str = Column(Integer, info={'min': 1})
|
||||
dex = Column(Integer, info={'min': 1})
|
||||
con = Column(Integer, info={'min': 1})
|
||||
int = Column(Integer, info={'min': 1})
|
||||
wis = Column(Integer, info={'min': 1})
|
||||
cha = Column(Integer, info={'min': 1})
|
||||
str = Column(Integer, info={'min': 0, 'max': 30})
|
||||
dex = Column(Integer, info={'min': 0, 'max': 30})
|
||||
con = Column(Integer, info={'min': 0, 'max': 30})
|
||||
int = Column(Integer, info={'min': 0, 'max': 30})
|
||||
wis = Column(Integer, info={'min': 0, 'max': 30})
|
||||
cha = Column(Integer, info={'min': 0, 'max': 30})
|
||||
|
||||
Reference in New Issue
Block a user