Browse Source

Translate; seed categories in bdd; sort tree

master
Dashie der otter 3 years ago
parent
commit
bdf59948f5
3 changed files with 63 additions and 27 deletions
  1. 51
    15
      dbseed.py
  2. 11
    11
      setup-data/categories.json
  3. 1
    1
      static/js/global.js

+ 51
- 15
dbseed.py View File

@@ -1,12 +1,14 @@
from models import user_datastore, PartCategory, FootprintCategory, Manufacturer, \
PartMeasurementUnit, Footprint, Unit
import yaml
import json


def make_db_seed(db):
print("== Seeding database")
seed_users(db)
seed_root_categories(db)
seed_categories(db)
seed_manufacturers(db)
seed_parts_units(db)
seed_footprints(db)
@@ -24,21 +26,6 @@ def seed_users(db):
return


def seed_root_categories(db):
print("++ Seeding root categories")
a = PartCategory()
a.parent_id = None
a.name = "Root"
a.description = "Root category"

db.session.add(a)
db.session.commit()

b = PartCategory(parent_id=a.id, name="# Uncategorized")
db.session.add(b)
db.session.commit()


def seed_parts_units(db):
print("++ Seeding parts units")
for i in [['Centimeters', 'cm'], ['Pieces', 'pcs']]:
@@ -94,3 +81,52 @@ def seed_units(db):
a.slug = "%s%s" % (a.symbol, a.prefix)
db.session.add(a)
db.session.commit()


def seed_root_categories(db):
print("++ Seeding root categories")
a = PartCategory()
a.parent_id = None
a.name = "Root"
a.description = "Root category"

db.session.add(a)
db.session.commit()

b = PartCategory(parent_id=a.id, name="# Uncategorized")
db.session.add(b)
db.session.commit()


def seed_categories(db):
print("++ Seeding categories")
a = db.session.query(PartCategory).filter(PartCategory.name == 'Root', PartCategory.id == 1).one()
if not a:
print('Can\'t find root category \'Root\' with null parent...')
return

with open('setup-data/categories.json', 'r') as stream:
cats = json.load(stream)

if not cats:
print("Cats null, is the file correct or not empty filled with json ?")
return

categories = cats['response']

def walk_tree(child, parent_id):
print(u"> {0} - {1}".format(parent_id, child['name']))
# Here we have a category
_c = PartCategory(parent_id=parent_id,
name=child['name'],
description=child['description'])
db.session.add(_c)
db.session.commit()
# Walking through childs if any
if 'children' in child:
for i in child['children']:
walk_tree(i, _c.id)

# We don't want to manage the Root category as we already create them it first
for i in categories['children']:
walk_tree(i, a.id)

+ 11
- 11
setup-data/categories.json View File

@@ -142,7 +142,7 @@
"leaf": true
}, {
"id": 59,
"name": "Sonstige",
"name": "Other",
"description": "",
"children": [],
"leaf": true
@@ -447,7 +447,7 @@
"description": "",
"children": [{
"id": 78,
"name": "Stufendrehschalter",
"name": "Rotary step switches",
"description": "",
"children": [],
"leaf": true
@@ -682,17 +682,17 @@
"leaf": true
}, {
"id": 66,
"name": "Widerstandsnetzwerke",
"name": "Resistor Networks",
"description": "",
"children": [],
"leaf": true
}, {
"id": 86,
"name": "Induktive Bauelemente",
"name": "Inductive Components",
"description": "",
"children": [{
"id": 87,
"name": "\u00dcbertrager",
"name": "Exchangers",
"description": "",
"children": [],
"leaf": true
@@ -804,13 +804,13 @@
"description": "",
"children": [{
"id": 53,
"name": "Unterlagscheibe",
"name": "Washer",
"description": "",
"children": [],
"leaf": true
}, {
"id": 54,
"name": "Mutter",
"name": "Mother",
"description": "",
"children": [],
"leaf": true
@@ -822,25 +822,25 @@
"leaf": true
}, {
"id": 63,
"name": "Abstandshalter",
"name": "Spacer",
"description": "",
"children": [],
"leaf": true
}, {
"id": 69,
"name": "Sonstiges",
"name": "Others",
"description": "",
"children": [],
"leaf": true
}, {
"id": 70,
"name": "Schrauben",
"name": "Screws",
"description": "",
"children": [],
"leaf": true
}, {
"id": 72,
"name": "Drahtstifte",
"name": "Brads",
"description": "",
"children": [],
"leaf": true

+ 1
- 1
static/js/global.js View File

@@ -12,7 +12,7 @@ $(function () {
"dataType": "json" // needed only if you do not supply JSON headers
}
},
"plugins": ["contextmenu"],
"plugins": ["contextmenu", "sort"],
contextmenu: {items: context_menu}
}).on("changed.jstree", function (e, data) {
var id = data.selected[data.selected.length-1];