Commit a20747309da7953660adba42ad46026597b11eb7

Authored by hitier
1 parent 454728d5

Feed projects labels

New project categories db models
Showing 3 changed files with 98 additions and 15 deletions   Show diff stats
app/models.py
... ... @@ -4,18 +4,44 @@ from sqlalchemy.orm import relationship
4 4 db = SQLAlchemy()
5 5  
6 6  
  7 +#
  8 +# Categorized projects
  9 +#
  10 +
  11 +class Project(db.Model):
  12 + id = db.Column(db.Integer, primary_key=True)
  13 + name = db.Column(db.String)
  14 + labels = relationship("ProjectLabel", back_populates="project")
  15 +
  16 +
7 17 class Category(db.Model):
8 18 id = db.Column(db.Integer, primary_key=True)
9   - name = db.Column(db.String(32))
10   - labels = relationship("CategoryLabel", back_populates='category')
  19 + name = db.Column(db.String)
  20 + labels = relationship("Label", back_populates="category")
  21 + projects = relationship("ProjectLabel", back_populates="category")
11 22  
12 23  
13   -class CategoryLabel(db.Model):
  24 +class Label(db.Model):
14 25 id = db.Column(db.Integer, primary_key=True)
15   - name = db.Column(db.String(32))
  26 + name = db.Column(db.String, unique=True)
16 27 category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
17   - category = relationship("Category", back_populates='labels')
  28 + category = relationship("Category", back_populates="labels")
  29 + projects = relationship("ProjectLabel", back_populates="label")
  30 +
  31 +
  32 +class ProjectLabel(db.Model):
  33 + project_id = db.Column(db.Integer, db.ForeignKey('project.id'), primary_key=True)
  34 + category_id = db.Column(db.Integer, db.ForeignKey('category.id'), primary_key=True)
  35 + label_id = db.Column(db.Integer, db.ForeignKey('label.id'))
  36 +
  37 + project = relationship("Project", back_populates="labels")
  38 + category = relationship("Category", back_populates="projects")
  39 + label = relationship("Label", back_populates="projects")
  40 +
18 41  
  42 +#
  43 +# Agents
  44 +#
19 45  
20 46 class AgentBap(db.Model):
21 47 id = db.Column(db.Integer, primary_key=True)
... ... @@ -49,22 +75,12 @@ class Agent(db.Model):
49 75 permanent = db.Column(db.Integer) # integer boolean
50 76  
51 77  
52   -class Project(db.Model):
53   - id = db.Column(db.Integer, primary_key=True)
54   - name = db.Column(db.String(100), unique=True)
55   -
56   -
57 78 class Service(db.Model):
58 79 id = db.Column(db.Integer, primary_key=True)
59 80 name = db.Column(db.String(100), unique=True)
60 81 abbr = db.Column(db.String(50), unique=True)
61 82  
62 83  
63   -class Function(db.Model):
64   - id = db.Column(db.Integer, primary_key=True)
65   - name = db.Column(db.String(100), unique=True)
66   -
67   -
68 84 class Capacity(db.Model):
69 85 id = db.Column(db.Integer, primary_key=True)
70 86 name = db.Column(db.String(100), unique=True)
... ...
tests/common_db_feed.py 0 → 100644
... ... @@ -0,0 +1,39 @@
  1 +from app.models import Category, db, Label, Project, ProjectLabel
  2 +
  3 +categorized_labels = {'pole': ['Spatial', 'Sol'], 'domaine': ['soleil-terre', 'atmosphere', 'r&t', 'géologie']}
  4 +projects_categories = {'ChemCam': {'pole': 'Spatial', 'domaine': 'géologie'},
  5 + 'Pilot': {'pole': 'Spatial', 'domaine': 'atmosphere'},
  6 + 'Ambre': {'pole': 'Spatial', 'domaine': 'soleil-terre'}}
  7 +
  8 +
  9 +def feed_projects():
  10 + for c_name, labels in categorized_labels.items():
  11 + n_c = Category(name=c_name)
  12 + db.session.add(n_c)
  13 + for l_name in labels:
  14 + n_l = Label(name=l_name, category=n_c)
  15 + db.session.add(n_l)
  16 +
  17 + db.session.commit()
  18 +
  19 + project_names = projects_categories.keys()
  20 +
  21 + for p_name in set(project_names):
  22 + n_p = Project(name=p_name)
  23 + db.session.add(n_p)
  24 + db.session.commit()
  25 +
  26 + for p, categories in projects_categories.items():
  27 + n_p = db.session.query(Project).filter(Project.name == p).one()
  28 + for c_name, l_name in categories.items():
  29 + n_c = db.session.query(Category).filter(Category.name == c_name).one()
  30 + n_l = db.session.query(Label).filter(Label.name == l_name).one()
  31 + n_pc = ProjectLabel(project=n_p, category=n_c, label=n_l)
  32 + db.session.add(n_pc)
  33 +
  34 + db.session.commit()
  35 +
  36 +
  37 +def show_categories():
  38 + for c in db.session.query(Category).all():
  39 + print(c.name + ": ", ",".join([l.name for l in c.categorized_labels]))
... ...
tests/db_tests.py 0 → 100644
... ... @@ -0,0 +1,28 @@
  1 +import unittest
  2 +
  3 +from app import create_app, db
  4 +from app.models import Project
  5 +from pdc_config import TestConfig
  6 +from tests.common_db_feed import feed_projects
  7 +
  8 +
  9 +class DbBaseTestCase(unittest.TestCase):
  10 + def setUp(self):
  11 + self.app = create_app(TestConfig)
  12 + # force db uri to sqlite memory
  13 + self.app.config.update(
  14 + SQLALCHEMY_DATABASE_URI='sqlite:///:memory:'
  15 + )
  16 + self.app_context = self.app.app_context()
  17 + self.app_context.push()
  18 + db.create_all()
  19 + feed_projects()
  20 +
  21 + def tearDown(self):
  22 + db.session.remove()
  23 + db.drop_all()
  24 + self.app_context.pop()
  25 +
  26 + def test_first(self):
  27 + projects = Project.query.all()
  28 + self.assertEqual(3, len(projects))
... ...