Commit aadf17905cc3440d62b71a2e75682dd9f05ba0fb

Authored by hitier
1 parent 15a97bc4

Ensure uniq project name

app/main/routes.py
... ... @@ -268,6 +268,11 @@ def project_edit(project_id=None):
268 268 this_project = Project.query.get(int(project_id))
269 269 done_string = "mis à jour."
270 270 else:
  271 + # check name doesnt exist yet
  272 + existing_project = Project.query.filter(Project.name == request.form.get('name')).one_or_none()
  273 + if existing_project:
  274 + flash(f"Le projet >{existing_project.name}< existe déjà.", 'warning')
  275 + return redirect(url_for('main.project_edit'))
271 276 # or create from scratch
272 277 this_project = Project()
273 278 done_string = "ajouté."
... ...
app/models.py
... ... @@ -54,7 +54,7 @@ class Formable:
54 54  
55 55 class Project(db.Model, Formable):
56 56 id = db.Column(db.Integer, primary_key=True)
57   - name = db.Column(db.String)
  57 + name = db.Column(db.String, unique=True)
58 58 labels = relationship("ProjectLabel", back_populates="project")
59 59  
60 60 # add keys to import/export
... ... @@ -161,7 +161,7 @@ class CategoryLabel(db.Model):
161 161  
162 162 class Category(db.Model, Formable):
163 163 id = db.Column(db.Integer, primary_key=True)
164   - name = db.Column(db.String)
  164 + name = db.Column(db.String, unique=True)
165 165 labels = relationship("CategoryLabel", back_populates="category")
166 166  
167 167 export_keys = ['name']
... ... @@ -198,25 +198,25 @@ class Category(db.Model, Formable):
198 198  
199 199 class AgentBap(db.Model):
200 200 id = db.Column(db.Integer, primary_key=True)
201   - name = db.Column(db.String(16))
  201 + name = db.Column(db.String(16), unique=True)
202 202 agents = relationship("Agent", back_populates="bap")
203 203  
204 204  
205 205 class AgentGrade(db.Model):
206 206 id = db.Column(db.Integer, primary_key=True)
207   - name = db.Column(db.String(16))
  207 + name = db.Column(db.String(16), unique=True)
208 208 agents = relationship("Agent", back_populates="grade")
209 209  
210 210  
211 211 class AgentStatus(db.Model):
212 212 id = db.Column(db.Integer, primary_key=True)
213   - name = db.Column(db.String(16))
  213 + name = db.Column(db.String(16), unique=True)
214 214 agents = relationship("Agent", back_populates="status")
215 215  
216 216  
217 217 class Company(db.Model):
218 218 id = db.Column(db.Integer, primary_key=True)
219   - name = db.Column(db.String(16))
  219 + name = db.Column(db.String(16), unique=True)
220 220 agents = relationship("Agent", back_populates="company")
221 221  
222 222  
... ...
resources/lesia-btp.sqlite
No preview for this file type
tests/frontend_tests.py
... ... @@ -199,6 +199,41 @@ class FormsTestCase(BaseFrontTestCase):
199 199 latest_agent = Project.query.filter(Project.id == 1).one()
200 200 self.assertEqual(project_name, latest_agent.name)
201 201  
  202 + # Test project form
  203 + def test_project_add_twice(self):
  204 + project_name = "My Project Name"
  205 + # load the form
  206 + target_url = self.get_server_url() + url_for('main.project_edit')
  207 + self.driver.get(target_url)
  208 + # fill it in
  209 + name_input = self.driver.find_elements_by_xpath("//input[@id='name']")[0]
  210 + name_input.send_keys(project_name)
  211 + # submit
  212 + submit_button = self.driver.find_elements_by_xpath("//input[@type='submit']")[0]
  213 + submit_button.send_keys(Keys.ENTER)
  214 + #
  215 + # check page return
  216 + message = self.driver.find_element_by_xpath("//div[@role='alert']")
  217 + self.assertTrue(project_name in message.text)
  218 + #
  219 + # check on database
  220 + latest_agent = Project.query.order_by(Project.id.desc()).first()
  221 + self.assertEqual(project_name, latest_agent.name)
  222 + #
  223 + # Submit again
  224 + target_url = self.get_server_url() + url_for('main.project_edit')
  225 + self.driver.get(target_url)
  226 + # fill it in
  227 + name_input = self.driver.find_elements_by_xpath("//input[@id='name']")[0]
  228 + name_input.send_keys(project_name)
  229 + # submit
  230 + submit_button = self.driver.find_elements_by_xpath("//input[@type='submit']")[0]
  231 + submit_button.send_keys(Keys.ENTER)
  232 + #
  233 + # check warning message
  234 + message = self.driver.find_element_by_xpath("//div[@role='alert']")
  235 + self.assertTrue(project_name + "< existe déjà" in message.text)
  236 +
202 237 # Test charge form add new
203 238 def test_charge_add(self):
204 239 # load the form
... ...