update places view and connect with backend
This commit is contained in:
		
							parent
							
								
									d90987ca39
								
							
						
					
					
						commit
						98a4635a12
					
				| @ -112,4 +112,42 @@ def set_traits(): | |||||||
| 
 | 
 | ||||||
|     db.session.commit() |     db.session.commit() | ||||||
| 
 | 
 | ||||||
|  |     return {"error": 0} | ||||||
|  | 
 | ||||||
|  | @routes_api_admin.route("/setPlaces", methods=["GET", "POST"]) | ||||||
|  | def set_places(): | ||||||
|  |     input_lang = flask.request.json["lang"] | ||||||
|  |     input_places = flask.request.json["places"] | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     db_places = db.session.query(Place).all() | ||||||
|  |      | ||||||
|  |     modified_db_places = [] | ||||||
|  |     for input_place in input_places: | ||||||
|  |         if input_place["id"]: | ||||||
|  |             # modify | ||||||
|  |             db_place = list(filter(lambda db_place: db_place.PLACE_ID == int(input_place["id"]), db_places))[0] | ||||||
|  |              | ||||||
|  |             db.session.delete(db_place.LOCALE.TEXTS[0]) | ||||||
|  |              | ||||||
|  |             db_place.LOCALE.TEXTS = [Text(None, None, input_lang, input_place["name"])] | ||||||
|  |              | ||||||
|  |             db.session.add(db_place) | ||||||
|  |             modified_db_places.append(db_place) | ||||||
|  |         else: | ||||||
|  |             # add | ||||||
|  |             new_place = Place(None, None) | ||||||
|  |              | ||||||
|  |             new_place.LOCALE = Locale(None) | ||||||
|  |             new_place.LOCALE.TEXTS = [Text(None, None, input_lang, input_place["name"])] | ||||||
|  |              | ||||||
|  |             db.session.add(new_place) | ||||||
|  | 
 | ||||||
|  |     # delete | ||||||
|  |     for db_place in db_places: | ||||||
|  |         if db_place not in modified_db_places: | ||||||
|  |             db.session.delete(db_place) | ||||||
|  | 
 | ||||||
|  |     db.session.commit() | ||||||
|  | 
 | ||||||
|     return {"error": 0} |     return {"error": 0} | ||||||
| @ -1,8 +1,53 @@ | |||||||
| <a href="/admin"> go Back </a> <br> | <a href="/admin"> go Back </a> <br> | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| <section>     | <style> | ||||||
| {%for place in places%} |     section section{ | ||||||
| <input name="{{place['id']}}" value="{{place['name']}}"> |         border: thin solid red; | ||||||
|  |         padding: 5px; | ||||||
|  |         margin-top: 20px; | ||||||
|  |     } | ||||||
|  |     section section input{ | ||||||
|  |         width: 100%; | ||||||
|  |     } | ||||||
|  | </style> | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | <section id="places"> | ||||||
|  |     {%for place in places%} | ||||||
|  |     <section id="{{place['id']}}"> | ||||||
|  |         <input value="{{place['name']}}"> | ||||||
|  |         <button onclick="deletePlace(this)">Delete place</button> | ||||||
|  |     </section> | ||||||
| {%endfor%} | {%endfor%} | ||||||
| </section> | </section> | ||||||
|  | <button onclick="addNewInput()">Add new</button> | ||||||
|  | <button onclick="saveForm()">Save changes</button> | ||||||
|  | 
 | ||||||
|  | <script src="/static/js/api.js"></script> | ||||||
|  | <script> | ||||||
|  | 
 | ||||||
|  | function addNewInput(){ | ||||||
|  |     let newPlace = places.lastElementChild.cloneNode(true); | ||||||
|  |     newPlace.id = ""; | ||||||
|  |     newPlace.querySelector("input").value = ""; | ||||||
|  |     places.appendChild(newPlace); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | function deletePlace(buttonNode){ | ||||||
|  |     let placeNode = buttonNode.parentNode; | ||||||
|  |     placeNode.parentNode.removeChild(placeNode); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | function saveForm(){ | ||||||
|  |     let data = []; | ||||||
|  |     for(let section of places.querySelectorAll("section")){ | ||||||
|  |         let place = {}; | ||||||
|  |         place["id"] = section.id | ||||||
|  |         place["name"] = section.querySelector("input").value | ||||||
|  |         data.push(place); | ||||||
|  |     } | ||||||
|  |     makeAPIRequest("admin/setPlaces", {"places": data, "lang": "FR"}, {"content": "json"}) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | </script> | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user