add activities
This commit is contained in:
29
app.py
29
app.py
@ -1,11 +1,36 @@
|
||||
from flask import Flask
|
||||
from flask import Flask, render_template, request, redirect, url_for
|
||||
|
||||
from data.data_provider import DataProvider
|
||||
from data.models import Activity
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
data_provider = DataProvider(
|
||||
"postgresql://yaric359:yaric359@localhost:5432/yaric359"
|
||||
)
|
||||
|
||||
@app.route('/')
|
||||
def hello_world():
|
||||
return 'Hello World!'
|
||||
return render_template('pages/index.html')
|
||||
|
||||
|
||||
@app.route('/activities/add/', methods=["GET"])
|
||||
def add_activity():
|
||||
return render_template('pages/activities/add_activity.html')
|
||||
|
||||
|
||||
@app.route('/activities/add/', methods=["POST"])
|
||||
def add_activity_post():
|
||||
name = request.form["name"]
|
||||
act = Activity(None, name)
|
||||
data_provider.create_activity(act)
|
||||
return redirect(url_for('add_activity'))
|
||||
|
||||
|
||||
@app.route('/activities/', methods=["GET"])
|
||||
def list_activities():
|
||||
return render_template('pages/activities/index.html',
|
||||
data=data_provider.get_activities())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@ -18,6 +18,7 @@ class DataProvider:
|
||||
"r")
|
||||
cur.execute(f.read())
|
||||
f.close()
|
||||
self._connection.commit()
|
||||
|
||||
def get_enterprises(self) -> List[Enterprise]:
|
||||
with self._connection.cursor() as cursor:
|
||||
@ -30,6 +31,7 @@ class DataProvider:
|
||||
name,
|
||||
short_name
|
||||
) for ent_id, name, short_name in cursor.fetchall()]
|
||||
self._connection.commit()
|
||||
return res
|
||||
|
||||
def get_enterprise_by_id(self, ent_id: int):
|
||||
@ -37,6 +39,7 @@ class DataProvider:
|
||||
cursor.execute("SELECT id, name, short_name "
|
||||
"FROM enterprise where id=%s;", (ent_id,))
|
||||
ent_id, name, short_name = cursor.fetchone()
|
||||
self._connection.commit()
|
||||
return Enterprise(
|
||||
ent_id,
|
||||
self.get_activities_by_enterprpise(ent_id),
|
||||
@ -53,6 +56,7 @@ class DataProvider:
|
||||
"right join do_activity da on activity.id = da.activity "
|
||||
"where enterprise = %s",
|
||||
(ent_id,))
|
||||
self._connection.commit()
|
||||
return [Activity(act_id, name) for act_id, name
|
||||
in cursor.fetchall()]
|
||||
|
||||
@ -61,6 +65,7 @@ class DataProvider:
|
||||
cur.execute(
|
||||
"SELECT id from division where enterprise = %s", (ent_id,)
|
||||
)
|
||||
self._connection.commit()
|
||||
return [Division(div_id,
|
||||
self.get_contact_info_by_division(div_id),
|
||||
self.get_activities_by_division(div_id)
|
||||
@ -74,6 +79,7 @@ class DataProvider:
|
||||
"FROM enterprise WHERE enterprise.id = %s)",
|
||||
(ent_id,))
|
||||
con_id, email = cur.fetchone()
|
||||
self._connection.commit()
|
||||
return ContactInfo(
|
||||
con_id,
|
||||
email,
|
||||
@ -90,6 +96,7 @@ class DataProvider:
|
||||
"FROM division WHERE division.id = %s)",
|
||||
(div_id,))
|
||||
con_id, email = cur.fetchone()
|
||||
self._connection.commit()
|
||||
return ContactInfo(
|
||||
con_id,
|
||||
email,
|
||||
@ -103,6 +110,7 @@ class DataProvider:
|
||||
cur.execute("SELECT id, date_of_start, date_of_end "
|
||||
"FROM phone_number where contact_info = %s",
|
||||
(con_id,))
|
||||
self._connection.commit()
|
||||
return [PhoneNumber(
|
||||
ph_id,
|
||||
date_start,
|
||||
@ -115,6 +123,7 @@ class DataProvider:
|
||||
"FROM head WHERE contact_info = %s",
|
||||
(con_id,))
|
||||
cur.fetchall()
|
||||
self._connection.commit()
|
||||
return [Head(id, date_start, date_end) for id, date_start, date_end
|
||||
in cur]
|
||||
|
||||
@ -125,6 +134,7 @@ class DataProvider:
|
||||
"date_of_start, date_of_end FROM location "
|
||||
"where contact_info = %s",
|
||||
(con_id,))
|
||||
self._connection.commit()
|
||||
return [
|
||||
Location(id, country, city, street, house, room, date_of_start,
|
||||
date_of_end) for
|
||||
@ -139,6 +149,7 @@ class DataProvider:
|
||||
"right join do_activity da on activity.id = da.activity "
|
||||
"where division = %s",
|
||||
(div_id,))
|
||||
self._connection.commit()
|
||||
return [Activity(act_id, name) for act_id, name
|
||||
in cursor.fetchall()]
|
||||
|
||||
@ -147,6 +158,7 @@ class DataProvider:
|
||||
cur.execute("INSERT INTO contact_info (email) VALUES (%s) "
|
||||
"RETURNING id", (info.email, ))
|
||||
info.id = cur.fetchone()[0]
|
||||
self._connection.commit()
|
||||
for head in info.head:
|
||||
self.create_head(head, info.id)
|
||||
for loc in info.locations:
|
||||
@ -161,6 +173,7 @@ class DataProvider:
|
||||
"VALUES (%s, %s, %s) RETURNING id",
|
||||
(head.date_of_start, head.date_of_end, con_id))
|
||||
head.id = cur.fetchone()[0]
|
||||
self._connection.commit()
|
||||
|
||||
def create_location(self, loc: Location, con_id: int):
|
||||
with self._connection.cursor() as cur:
|
||||
@ -170,6 +183,7 @@ class DataProvider:
|
||||
(loc.country, loc.city, loc.street, loc.house,
|
||||
loc.room, loc.date_of_start, loc.date_of_end, con_id))
|
||||
loc.id = cur.fetchone()[0]
|
||||
self._connection.commit()
|
||||
|
||||
def create_phone_number(self, phone: PhoneNumber, con_id: int):
|
||||
with self._connection.cursor() as cur:
|
||||
@ -178,6 +192,7 @@ class DataProvider:
|
||||
"(%s, %s, %s) RETURNING id",
|
||||
(con_id, phone.date_of_start, phone.date_of_end))
|
||||
phone.id = cur.fetchone()[0]
|
||||
self._connection.commit()
|
||||
|
||||
def create_division(self, division: Division, ent_id: int):
|
||||
with self._connection.cursor() as cur:
|
||||
@ -188,6 +203,7 @@ class DataProvider:
|
||||
division.id = cur.fetchone()[0]
|
||||
for act in division.activities:
|
||||
self.add_activity_to_division(division, act)
|
||||
self._connection.commit()
|
||||
|
||||
def create_enterprise(self, enterprise: Enterprise):
|
||||
with self._connection.cursor() as cur:
|
||||
@ -203,22 +219,33 @@ class DataProvider:
|
||||
self.add_activity_to_enterprise(enterprise, act)
|
||||
for division in enterprise.divisions:
|
||||
self.create_division(division, enterprise.id)
|
||||
self._connection.commit()
|
||||
|
||||
def create_activity(self, activity: Activity):
|
||||
with self._connection.cursor() as cur:
|
||||
cur.execute("INSERT INTO activity (name) VALUES (%s) RETURNING id",
|
||||
(activity.name, ))
|
||||
activity.id = cur.fetchone()[0]
|
||||
self._connection.commit()
|
||||
|
||||
def add_activity_to_division(self, division: Division, activity: Activity):
|
||||
with self._connection.cursor() as cur:
|
||||
cur.execute("INSERT INTO do_activity (activity, division) "
|
||||
"VALUES (%s, %s)", (activity.id, division.id))
|
||||
self._connection.commit()
|
||||
|
||||
def add_activity_to_enterprise(self, enterprise: Enterprise,
|
||||
activity: Activity):
|
||||
with self._connection.cursor() as cur:
|
||||
cur.execute("INSERT INTO do_activity (activity, enterprise) "
|
||||
"VALUES (%s, %s)", (activity.id, enterprise.id))
|
||||
self._connection.commit()
|
||||
|
||||
def get_activities(self):
|
||||
with self._connection.cursor() as cur:
|
||||
cur.execute("SELECT id, name FROM activity")
|
||||
res = [Activity(id, name) for id, name in cur.fetchall()]
|
||||
self._connection.commit()
|
||||
return res
|
||||
|
||||
|
||||
|
||||
47
templates/base.html
Normal file
47
templates/base.html
Normal file
@ -0,0 +1,47 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>{% block title %}yaric359{% endblock %}</title>
|
||||
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
|
||||
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
|
||||
crossorigin="anonymous"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
|
||||
integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
|
||||
crossorigin="anonymous"></script>
|
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
|
||||
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
|
||||
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
|
||||
integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
|
||||
crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<a class="navbar-brand" href="{{ url_for('hello_world') }}">Navbar</a>
|
||||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
|
||||
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav mr-auto">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ url_for('hello_world') }}">Главная</a>
|
||||
</li>
|
||||
<li class="nav-item dropdown">
|
||||
<a class="nav-link dropdown-toggle" href="{{ url_for('list_activities') }}" id="navbarDropdown" role="button" data-toggle="dropdown"
|
||||
aria-haspopup="true" aria-expanded="false">
|
||||
Виды деятельности
|
||||
</a>
|
||||
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
|
||||
<a class="dropdown-item" href="{{ url_for('list_activities') }}">Список</a>
|
||||
<a class="dropdown-item" href="{{ url_for('add_activity') }}">Добавить</a>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
{% block content %}
|
||||
{% endblock %}
|
||||
</body>
|
||||
</html>
|
||||
13
templates/pages/activities/add_activity.html
Normal file
13
templates/pages/activities/add_activity.html
Normal file
@ -0,0 +1,13 @@
|
||||
{% extends 'base.html' %}
|
||||
{% block content %}
|
||||
<div class="container">
|
||||
<h3>Добавить вид деятельности</h3>
|
||||
<form method="post">
|
||||
<div class="form-group">
|
||||
<label for="name">Название активности</label>
|
||||
<input class="form-control" id="name" name="name" required type="text">
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Отправить</button>
|
||||
</form>
|
||||
</div>
|
||||
{% endblock %}
|
||||
29
templates/pages/activities/index.html
Normal file
29
templates/pages/activities/index.html
Normal file
@ -0,0 +1,29 @@
|
||||
{% extends 'base.html' %}
|
||||
{% block content %}
|
||||
<div class="container">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">
|
||||
Id
|
||||
</th>
|
||||
<th scope="col">
|
||||
Имя
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for item in data %}
|
||||
<tr>
|
||||
<th scope="row">
|
||||
{{ item.id }}
|
||||
</th>
|
||||
<td>
|
||||
{{ item.name }}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% endblock %}
|
||||
4
templates/pages/index.html
Normal file
4
templates/pages/index.html
Normal file
@ -0,0 +1,4 @@
|
||||
{% extends 'base.html' %}
|
||||
{% block content %}
|
||||
<div>Hello, world</div>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user