Commit a53e2fff7b0d4f55c7d06e98b0ddf14c510aff32

Authored by hitier
1 parent e20cd5d4

New category/labels orm models

update lesia import command
and resulting lesia  sqlite db
app/commands/commands.py
... ... @@ -10,7 +10,7 @@ from sqlalchemy.exc import IntegrityError
10 10 from sqlalchemy.sql import func
11 11  
12 12 from app.models import db, Agent, Service, Project, Capacity, Period, Charge, AgentStatus, Company, AgentBap, \
13   - AgentGrade, Category, Label, ProjectLabel
  13 + AgentGrade, Category, Label, ProjectLabel, CategoryLabel
14 14 # TODO: rename to methods and add get_roles()
15 15 from app.auth.models import User, _nameToRole, _roleToName
16 16  
... ... @@ -313,8 +313,9 @@ def feed_from_lesia():
313 313 domain_category = Category(name="Domaine")
314 314 domains = lesia_session.query(lesia_domains)
315 315 for d in domains:
316   - n_l = Label(name=d.nom, category=domain_category)
317   - db.session.add(n_l)
  316 + n_l = Label(name=d.nom)
  317 + n_cl = CategoryLabel(category=domain_category, label=n_l )
  318 + db.session.add(n_cl)
318 319 db.session.commit()
319 320  
320 321 # Feed all lesia 'pôle' names and link to new category "Pôle"
... ... @@ -322,8 +323,9 @@ def feed_from_lesia():
322 323 pole_category = Category(name="Pôle")
323 324 poles = lesia_session.query(lesia_poles)
324 325 for p in poles:
325   - n_l = Label(name=p.nom, category=pole_category)
326   - db.session.add(n_l)
  326 + n_l = Label(name=p.nom)
  327 + n_cl = CategoryLabel(category=pole_category, label=n_l)
  328 + db.session.add(n_cl)
327 329 db.session.commit()
328 330  
329 331 # Feed lesia project with proper "pôle"
... ... @@ -338,7 +340,7 @@ def feed_from_lesia():
338 340 pole_name = lesia_session.query(lesia_poles).filter(lesia_poles.id == p.pole_id).one().nom
339 341 # search corresponding Label and store in ProjectLabel table
340 342 n_l = Label.query.filter(Label.name == pole_name).one()
341   - n_pl = ProjectLabel(project=n_p, category=n_l.category, label=n_l)
  343 + n_pl = ProjectLabel(project=n_p, label=n_l)
342 344 db.session.add(n_pl)
343 345 db.session.commit()
344 346  
... ... @@ -350,7 +352,7 @@ def feed_from_lesia():
350 352 domain_name = lesia_session.query(lesia_domains).filter(lesia_domains.id == dp.domaine_id).one().nom
351 353 n_p = Project.query.filter(Project.name == project_name).one()
352 354 n_l = Label.query.filter(Label.name == domain_name).one()
353   - n_pl = ProjectLabel(project=n_p, category=n_l.category, label=n_l)
  355 + n_pl = ProjectLabel(project=n_p, label=n_l)
354 356 db.session.add(n_pl)
355 357 # Some projects have 2 domain labels in lesia db
356 358 # That is not allowed any more in the new model.
... ...
app/models.py
... ... @@ -41,36 +41,82 @@ class Formable:
41 41 #
42 42 # Categorized projects
43 43 #
  44 +# There is one label list,
  45 +# each label belongs to one or more categories.
  46 +#
  47 +# The projects are labelled by one or more label.
  48 +#
  49 +# Thus this is modeled with classes
  50 +# Project, Label and Category
  51 +# And many_to_many association are done through
  52 +# ProjectLabel and CategoryLabel
  53 +#
44 54  
45   -class Project(db.Model):
  55 +class Project(db.Model, Formable):
46 56 id = db.Column(db.Integer, primary_key=True)
47 57 name = db.Column(db.String)
48 58 labels = relationship("ProjectLabel", back_populates="project")
49 59  
  60 + export_keys = ['name']
50 61  
51   -class Category(db.Model):
  62 + def to_struct(self):
  63 + """
  64 + overide parent method to include one key: agent.fullname
  65 +
  66 + Mainly we add the 'labels' element containing a dict
  67 + where category key contains labels for that project.
  68 + :return:
  69 + """
  70 + _struct = super(Project, self).to_struct()
  71 + _struct['labels'] = {}
  72 + for _c in Category.query.all():
  73 + category_labels = []
  74 + for _l in self.labels:
  75 + if _l.label in [_cl.label for _cl in _c.labels]:
  76 + category_labels.append(_l.label.name)
  77 + _struct['labels'][_c.name] = category_labels
  78 + return _struct
  79 +
  80 +
  81 +class ProjectLabel(db.Model):
  82 + """
  83 + Labelling projects.
  84 + On project can have many labels.
  85 + And one label will be set to many projects
  86 + """
52 87 id = db.Column(db.Integer, primary_key=True)
53   - name = db.Column(db.String)
54   - labels = relationship("Label", back_populates="category")
55   - projects = relationship("ProjectLabel", back_populates="category")
  88 + project_id = db.Column(db.Integer, db.ForeignKey('project.id'))
  89 + label_id = db.Column(db.Integer, db.ForeignKey('label.id'))
  90 + project = relationship("Project", back_populates="labels")
  91 + label = relationship("Label", back_populates="projects")
56 92  
57 93  
58 94 class Label(db.Model):
59 95 id = db.Column(db.Integer, primary_key=True)
60 96 name = db.Column(db.String, unique=True)
61 97 category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
62   - category = relationship("Category", back_populates="labels")
63 98 projects = relationship("ProjectLabel", back_populates="label")
  99 + categories = relationship("CategoryLabel", back_populates="label")
64 100  
65 101  
66   -class ProjectLabel(db.Model):
67   - project_id = db.Column(db.Integer, db.ForeignKey('project.id'), primary_key=True)
68   - category_id = db.Column(db.Integer, db.ForeignKey('category.id'), primary_key=True)
  102 +class CategoryLabel(db.Model):
  103 + """
  104 + Categorizing labels:
  105 + one label can be added to many categories
  106 + one category hosts many labels
  107 + """
  108 + id = db.Column(db.Integer, primary_key=True)
  109 + category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
69 110 label_id = db.Column(db.Integer, db.ForeignKey('label.id'))
  111 + category = relationship("Category", back_populates="labels")
  112 + label = relationship("Label", back_populates="categories")
70 113  
71   - project = relationship("Project", back_populates="labels")
72   - category = relationship("Category", back_populates="projects")
73   - label = relationship("Label", back_populates="projects")
  114 +
  115 +class Category(db.Model):
  116 + id = db.Column(db.Integer, primary_key=True)
  117 + name = db.Column(db.String)
  118 + labels = relationship("Label", back_populates="category")
  119 + labels = relationship("CategoryLabel", back_populates="category")
74 120  
75 121  
76 122 #
... ...
resources/lesia-btp.sqlite
No preview for this file type