P5-703 add support for categories
This commit is contained in:
+2
-1
@@ -17,8 +17,9 @@ db = SQLAlchemy(app)
|
||||
# eSales
|
||||
esales = Cluster(app.config.get("ESALES_URL"))
|
||||
|
||||
from api.views import designers
|
||||
from api.views import designers, categories
|
||||
app.register_blueprint(designers.mod)
|
||||
app.register_blueprint(categories.mod)
|
||||
|
||||
|
||||
def check_authentication():
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
# coding=UTF-8
|
||||
import unidecode
|
||||
import re
|
||||
|
||||
|
||||
def slugify(text):
|
||||
"""Converts a string to a human-readable url"""
|
||||
|
||||
# translate
|
||||
text = unidecode.unidecode(text)
|
||||
|
||||
# lowercase
|
||||
text = text.lower()
|
||||
|
||||
# replace unwanted chars
|
||||
numbers = re.compile('(?<=\d),(?=\d)')
|
||||
text = numbers.sub('', text)
|
||||
unwanted_chars = re.compile(r'[^_a-z0-9/]+')
|
||||
text = unwanted_chars.sub('_', text)
|
||||
|
||||
# remove redundant _
|
||||
duplicated_underscores = re.compile('_{2,}')
|
||||
text = duplicated_underscores.sub('_', text)
|
||||
text = text.strip('_')
|
||||
|
||||
return text
|
||||
@@ -1 +1,2 @@
|
||||
from .designer import Designer
|
||||
from .category import Category
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
from api import db
|
||||
from api.helpers import slugify
|
||||
|
||||
|
||||
class Category(db.Model):
|
||||
__tablename__ = 'categories'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(255), nullable=False)
|
||||
texts = db.relationship(
|
||||
'CategoryTexts', backref='category', lazy='dynamic')
|
||||
|
||||
def path(self, locale='sv_SE'):
|
||||
row = CategoryTreeI18n.query.filter_by(
|
||||
category_id=self.id, locale=locale).first()
|
||||
if not row:
|
||||
raise Exception(
|
||||
'Could not find a path for category: {}, locale: {}'.format(self.id, locale))
|
||||
path = row.path.replace('rot/', '', 1)
|
||||
path = slugify(path)
|
||||
return path
|
||||
|
||||
def to_json(self, nested=False):
|
||||
j = {
|
||||
'id': self.id,
|
||||
'name': self.name
|
||||
}
|
||||
if nested:
|
||||
j['texts'] = [t.to_json() for t in self.texts]
|
||||
|
||||
return j
|
||||
|
||||
|
||||
class CategoryTexts(db.Model):
|
||||
__tablename__ = 'category_texts'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
category_id = db.Column(db.Integer, db.ForeignKey('categories.id'))
|
||||
locale = db.relationship("Locale")
|
||||
locale_id = db.Column(db.Integer, db.ForeignKey('locales.id'))
|
||||
wallpaper_name = db.Column(db.String(250))
|
||||
wallpaper_h1 = db.Column(db.String(250))
|
||||
wallpaper_description = db.Column(db.Text)
|
||||
canvas_name = db.Column(db.String(250))
|
||||
canvas_h1 = db.Column(db.String(250))
|
||||
canvas_description = db.Column(db.Text)
|
||||
design_wallpaper_name = db.Column('name', db.String(250))
|
||||
design_wallpaper_h1 = db.Column(db.String(250))
|
||||
design_wallpaper_description = db.Column(db.Text)
|
||||
|
||||
def to_json(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
'locale': self.locale.value,
|
||||
'wallpaper_name': self.wallpaper_name,
|
||||
'wallpaper_h1': self.wallpaper_h1,
|
||||
'wallpaper_description': self.wallpaper_description,
|
||||
'canvas_name': self.canvas_name,
|
||||
'canvas_h1': self.canvas_h1,
|
||||
'canvas_description': self.canvas_description,
|
||||
'design_wallpaper_name': self.design_wallpaper_name,
|
||||
'design_wallpaper_h1': self.design_wallpaper_h1,
|
||||
'design_wallpaper_description': self.design_wallpaper_description,
|
||||
}
|
||||
|
||||
|
||||
class CategoryTreeI18n(db.Model):
|
||||
__tablename__ = 'v_categorytree_i18n'
|
||||
|
||||
category_id = db.Column('id', db.Integer)
|
||||
path = db.Column(db.String(250))
|
||||
locale = db.Column(db.String(250))
|
||||
|
||||
__table_args__ = (db.PrimaryKeyConstraint(category_id, locale),)
|
||||
@@ -5,10 +5,7 @@ class Locale(db.Model):
|
||||
__tablename__ = 'locales'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
value = db.Column(db.String(5), nullable=False)
|
||||
value = db.Column(db.String(5))
|
||||
|
||||
def to_json(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"value": self.value
|
||||
}
|
||||
def __repr__(self):
|
||||
return self.value
|
||||
|
||||
@@ -26,3 +26,24 @@ def update_designer(designer):
|
||||
|
||||
directory = app.config.get("ESALES_IMPORT_DIR")
|
||||
importfile.write(directory)
|
||||
|
||||
|
||||
def update_category(category):
|
||||
importfile = ImportFile()
|
||||
for text in category.texts:
|
||||
locale = text.locale
|
||||
tree = CategoryTree("categories_{}".format(locale.value))
|
||||
tree.add_category(Category(
|
||||
key='root/{}'.format(category.path(locale.value)),
|
||||
display_name=category.name,
|
||||
wallpaper_description=text.wallpaper_description,
|
||||
wallpaper_h1=text.wallpaper_h1,
|
||||
canvas_description=text.canvas_description,
|
||||
canvas_h1=text.canvas_h1,
|
||||
design_wallpaper_description=text.design_wallpaper_description,
|
||||
design_wallpaper_h1=text.design_wallpaper_h1
|
||||
))
|
||||
importfile.add_operation("update", tree)
|
||||
|
||||
directory = app.config.get("ESALES_IMPORT_DIR")
|
||||
importfile.write(directory)
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from api import db, tasks
|
||||
from api.models import Category
|
||||
|
||||
mod = Blueprint('categories', __name__, url_prefix='/categories')
|
||||
|
||||
|
||||
@mod.route("/", methods=["GET"])
|
||||
def list():
|
||||
categories = Category.query.all()
|
||||
return jsonify(categories=[c.to_json() for c in categories])
|
||||
|
||||
|
||||
@mod.route("/<int:id>", methods=["GET"])
|
||||
def show(id):
|
||||
category = Category.query.get_or_404(id)
|
||||
return jsonify(category.to_json(nested=True))
|
||||
|
||||
|
||||
@mod.route("/<id>", methods=["PUT"])
|
||||
def update(id):
|
||||
category = Category.query.get_or_404(id)
|
||||
data = request.json
|
||||
for k, v in data.items():
|
||||
setattr(category, k, v)
|
||||
db.session.add(category)
|
||||
db.session.commit()
|
||||
tasks.esales.update_category(category)
|
||||
return jsonify(category.to_json())
|
||||
Reference in New Issue
Block a user