Compare commits

..

1 commit

Author SHA1 Message Date
Inex Code 6aa4d6c098 Students 2022-10-30 11:29:52 +00:00
8 changed files with 129 additions and 194 deletions

View file

@ -3,7 +3,7 @@
import os import os
from . import db from . import db
from .contacts import Contacts, Contact from .students import Students, Student
from flask import Flask, request, jsonify from flask import Flask, request, jsonify
from flask_restful import Api from flask_restful import Api
@ -15,8 +15,8 @@ def create_app(test_config=None):
db.init_app(app) db.init_app(app)
api = Api(app) api = Api(app)
CORS(app, resources={r"/api/*": {"origins": "*"}}) CORS(app, resources={r"/api/*": {"origins": "*"}})
api.add_resource(Contacts, '/api/contacts') api.add_resource(Students, '/api/students')
api.add_resource(Contact, '/api/contacts/<int:uid>') api.add_resource(Student, '/api/students/<int:uid>')
return app return app

View file

@ -2,7 +2,7 @@ DROP TABLE IF EXISTS main;
DROP TABLE IF EXISTS first_name; DROP TABLE IF EXISTS first_name;
DROP TABLE IF EXISTS last_name; DROP TABLE IF EXISTS last_name;
DROP TABLE IF EXISTS middle_name; DROP TABLE IF EXISTS middle_name;
DROP TABLE IF EXISTS street; DROP TABLE IF EXISTS discipline;
CREATE TABLE first_name ( CREATE TABLE first_name (
first_name_id SERIAL PRIMARY KEY, first_name_id SERIAL PRIMARY KEY,
first_name_val VARCHAR(255) NOT NULL first_name_val VARCHAR(255) NOT NULL
@ -15,23 +15,20 @@ CREATE TABLE middle_name (
middle_name_id SERIAL PRIMARY KEY, middle_name_id SERIAL PRIMARY KEY,
middle_name_val VARCHAR(255) NOT NULL middle_name_val VARCHAR(255) NOT NULL
); );
CREATE TABLE street ( CREATE TABLE discipline (
street_id SERIAL PRIMARY KEY, discipline_id SERIAL PRIMARY KEY,
street_val VARCHAR(255) NOT NULL discipline_val VARCHAR(255) NOT NULL
); );
CREATE TABLE main ( CREATE TABLE main (
uid serial, uid serial,
last_name int, last_name int,
first_name int, first_name int,
middle_name int, middle_name int,
street int, discipline int,
build VARCHAR(255), mark VARCHAR(255),
build_k VARCHAR(255),
apartment VARCHAR(255),
telephone VARCHAR(255) NOT NULL,
CONSTRAINT fk_last_name FOREIGN KEY (last_name) REFERENCES last_name(last_name_id), CONSTRAINT fk_last_name FOREIGN KEY (last_name) REFERENCES last_name(last_name_id),
CONSTRAINT fk_first_name FOREIGN KEY (first_name) REFERENCES first_name(first_name_id), CONSTRAINT fk_first_name FOREIGN KEY (first_name) REFERENCES first_name(first_name_id),
CONSTRAINT fk_middle_name FOREIGN KEY (middle_name) REFERENCES middle_name(middle_name_id), CONSTRAINT fk_middle_name FOREIGN KEY (middle_name) REFERENCES middle_name(middle_name_id),
CONSTRAINT fk_street FOREIGN KEY (street) REFERENCES street(street_id), CONSTRAINT fk_discipline FOREIGN KEY (discipline) REFERENCES discipline(discipline_id),
PRIMARY KEY (uid) PRIMARY KEY (uid)
); );

View file

@ -8,31 +8,28 @@ from flask_restful import Resource, Api, reqparse
from .db import get_db from .db import get_db
class Contacts(Resource): class Students(Resource):
def get(self): def get(self):
db = get_db() db = get_db()
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT uid, last_name_val, first_name_val, middle_name_val, street_val, build, build_k, apartment, telephone" cur.execute("SELECT uid, last_name_val, first_name_val, middle_name_val, discipline_val, mark"
" FROM main" " FROM main"
" join last_name on main.last_name = last_name_id" " join last_name on main.last_name = last_name_id"
" join first_name on main.first_name = first_name_id" " join first_name on main.first_name = first_name_id"
" join middle_name on main.middle_name = middle_name_id" " join middle_name on main.middle_name = middle_name_id"
" join street on main.street = street_id") " join discipline on main.discipline = discipline_id")
contacts = cur.fetchall() students = cur.fetchall()
def convert(data): def convert(data):
return { return {
'uid': data[0], 'uid': data[0],
'last_name': data[1], 'last_name': data[1],
'first_name': data[2], 'first_name': data[2],
'middle_name': data[3], 'middle_name': data[3],
'street': data[4], 'discipline': data[4],
'build': data[5], 'mark': data[5]
'build_k': data[6],
'apartment': data[7],
'telephone': data[8]
} }
contacts = [convert(contact) for contact in contacts] students = [convert(student) for student in students]
return contacts return students
def post(self): def post(self):
db = get_db() db = get_db()
@ -41,13 +38,10 @@ class Contacts(Resource):
parser.add_argument('last_name') parser.add_argument('last_name')
parser.add_argument('first_name') parser.add_argument('first_name')
parser.add_argument('middle_name') parser.add_argument('middle_name')
parser.add_argument('street') parser.add_argument('discipline')
parser.add_argument('build') parser.add_argument('mark')
parser.add_argument('build_k')
parser.add_argument('apartment')
parser.add_argument('telephone', required=True)
args = parser.parse_args() args = parser.parse_args()
cur.execute("SELECT uid FROM main ORDER BY uid DESC LIMIT 1") cur.execute("SELECT uid FROM main ORDER BY uid DESC LIMIT 1")
uid = cur.fetchone() uid = cur.fetchone()
if uid is None: if uid is None:
@ -84,83 +78,77 @@ class Contacts(Resource):
middle_name = middle_name[0] middle_name = middle_name[0]
else: else:
middle_name = middle_name[0] middle_name = middle_name[0]
cur.execute("SELECT street_id FROM street WHERE street_val = %s", (args['street'],))
street = cur.fetchone()
if street is None:
cur.execute("INSERT INTO street (street_val) VALUES (%s)", (args['street'],))
cur.execute("SELECT street_id FROM street WHERE street_val = %s", (args['street'],))
street = cur.fetchone()
street = street[0]
else:
street = street[0]
cur.execute("INSERT INTO main (uid, last_name, first_name, middle_name, street, build, build_k, apartment, telephone)" cur.execute("SELECT discipline_id FROM discipline WHERE discipline_val = %s", (args['discipline'],))
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", discipline = cur.fetchone()
(uid, last_name, first_name, middle_name, street, args['build'], args['build_k'], args['apartment'], args['telephone'])) if discipline is None:
cur.execute("INSERT INTO discipline (discipline_val) VALUES (%s)", (args['discipline'],))
cur.execute("SELECT discipline_id FROM discipline WHERE discipline_val = %s", (args['discipline'],))
discipline = cur.fetchone()
discipline = discipline[0]
else:
discipline = discipline[0]
cur.execute("INSERT INTO main (uid, last_name, first_name, middle_name, discipline, mark)"
" VALUES (%s, %s, %s, %s, %s, %s)",
(uid, last_name, first_name, middle_name, discipline, args['mark']))
db.commit() db.commit()
return {'uid': uid}, 201 return {'uid': uid}, 201
class Contact(Resource): class Student(Resource):
def get(self, uid): def get(self, uid):
db = get_db() db = get_db()
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT uid, last_name_val, first_name_val, middle_name_val, street_val, build, build_k, apartment, telephone" cur.execute("SELECT uid, last_name_val, first_name_val, middle_name_val, discipline_val, mark"
" FROM main" " FROM main"
" join last_name on main.last_name = last_name_id" " join last_name on main.last_name = last_name_id"
" join first_name on main.first_name = first_name_id" " join first_name on main.first_name = first_name_id"
" join middle_name on main.middle_name = middle_name_id" " join middle_name on main.middle_name = middle_name_id"
" join street on main.street = street_id" " join discipline on main.discipline = discipline_id"
" WHERE uid = %s", (uid,)) " WHERE uid = %s", (uid,))
contact = cur.fetchone() student = cur.fetchone()
if contact is None: if student is None:
return {'message': 'Contact not found'}, 404 return {'message': 'Student not found'}, 404
def convert(data): def convert(data):
return { return {
'uid': data[0], 'uid': data[0],
'last_name': data[1], 'last_name': data[1],
'first_name': data[2], 'first_name': data[2],
'middle_name': data[3], 'middle_name': data[3],
'street': data[4], 'discipline': data[4],
'build': data[5], 'mark': data[5]
'build_k': data[6],
'apartment': data[7],
'telephone': data[8]
} }
contact = convert(contact) student = convert(student)
return contact return student
def delete(self, uid): def delete(self, uid):
db = get_db() db = get_db()
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT uid FROM main WHERE uid = %s", (uid,)) cur.execute("SELECT uid FROM main WHERE uid = %s", (uid,))
contact = cur.fetchone() student = cur.fetchone()
if contact is None: if student is None:
return {'message': 'Contact not found'}, 404 return {'message': 'Student not found'}, 404
cur.execute("DELETE FROM main WHERE uid = %s", (uid,)) cur.execute("DELETE FROM main WHERE uid = %s", (uid,))
db.commit() db.commit()
return {'message': 'Contact deleted'} return {'message': 'Student deleted'}
def put(self, uid): def put(self, uid):
parser = reqparse.RequestParser() parser = reqparse.RequestParser()
parser.add_argument('last_name') parser.add_argument('last_name')
parser.add_argument('first_name') parser.add_argument('first_name')
parser.add_argument('middle_name') parser.add_argument('middle_name')
parser.add_argument('street') parser.add_argument('discipline')
parser.add_argument('build') parser.add_argument('mark')
parser.add_argument('build_k')
parser.add_argument('apartment')
parser.add_argument('telephone')
args = parser.parse_args() args = parser.parse_args()
db = get_db() db = get_db()
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT uid FROM main WHERE uid = %s", (uid,)) cur.execute("SELECT uid FROM main WHERE uid = %s", (uid,))
contact = cur.fetchone() student = cur.fetchone()
if contact is None: if student is None:
return {'message': 'Contact not found'}, 404 return {'message': 'Student not found'}, 404
if args['last_name'] is not None: if args['last_name'] is not None:
cur.execute("SELECT last_name_id FROM last_name WHERE last_name_val = %s", (args['last_name'],)) cur.execute("SELECT last_name_id FROM last_name WHERE last_name_val = %s", (args['last_name'],))
last_name = cur.fetchone() last_name = cur.fetchone()
@ -172,7 +160,7 @@ class Contact(Resource):
else: else:
last_name = last_name[0] last_name = last_name[0]
cur.execute("UPDATE main SET last_name = %s WHERE uid = %s", (last_name, uid)) cur.execute("UPDATE main SET last_name = %s WHERE uid = %s", (last_name, uid))
if args['first_name'] is not None: if args['first_name'] is not None:
cur.execute("SELECT first_name_id FROM first_name WHERE first_name_val = %s", (args['first_name'],)) cur.execute("SELECT first_name_id FROM first_name WHERE first_name_val = %s", (args['first_name'],))
first_name = cur.fetchone() first_name = cur.fetchone()
@ -184,7 +172,7 @@ class Contact(Resource):
else: else:
first_name = first_name[0] first_name = first_name[0]
cur.execute("UPDATE main SET first_name = %s WHERE uid = %s", (first_name, uid)) cur.execute("UPDATE main SET first_name = %s WHERE uid = %s", (first_name, uid))
if args['middle_name'] is not None: if args['middle_name'] is not None:
cur.execute("SELECT middle_name_id FROM middle_name WHERE middle_name_val = %s", (args['middle_name'],)) cur.execute("SELECT middle_name_id FROM middle_name WHERE middle_name_val = %s", (args['middle_name'],))
middle_name = cur.fetchone() middle_name = cur.fetchone()
@ -196,30 +184,21 @@ class Contact(Resource):
else: else:
middle_name = middle_name[0] middle_name = middle_name[0]
cur.execute("UPDATE main SET middle_name = %s WHERE uid = %s", (middle_name, uid)) cur.execute("UPDATE main SET middle_name = %s WHERE uid = %s", (middle_name, uid))
if args['street'] is not None:
cur.execute("SELECT street_id FROM street WHERE street_val = %s", (args['street'],))
street = cur.fetchone()
if street is None:
cur.execute("INSERT INTO street (street_val) VALUES (%s)", (args['street'],))
cur.execute("SELECT street_id FROM street WHERE street_val = %s", (args['street'],))
street = cur.fetchone()
street = street[0]
else:
street = street[0]
cur.execute("UPDATE main SET street = %s WHERE uid = %s", (street, uid))
if args['build'] is not None:
cur.execute("UPDATE main SET build = %s WHERE uid = %s", (args['build'], uid))
if args['build_k'] is not None: if args['discipline'] is not None:
cur.execute("UPDATE main SET build_k = %s WHERE uid = %s", (args['build_k'], uid)) cur.execute("SELECT discipline_id FROM discipline WHERE discipline_val = %s", (args['discipline'],))
discipline = cur.fetchone()
if args['apartment'] is not None: if discipline is None:
cur.execute("UPDATE main SET apartment = %s WHERE uid = %s", (args['apartment'], uid)) cur.execute("INSERT INTO discipline (discipline_val) VALUES (%s)", (args['discipline'],))
cur.execute("SELECT discipline_id FROM discipline WHERE discipline_val = %s", (args['discipline'],))
if args['telephone'] is not None: discipline = cur.fetchone()
cur.execute("UPDATE main SET telephone = %s WHERE uid = %s", (args['telephone'], uid)) discipline = discipline[0]
else:
discipline = discipline[0]
cur.execute("UPDATE main SET discipline = %s WHERE uid = %s", (discipline, uid))
if args['mark'] is not None:
cur.execute("UPDATE main SET mark = %s WHERE uid = %s", (args['mark'], uid))
db.commit() db.commit()
return {'message': 'Contact updated'} return {'message': 'Student updated'}

View file

@ -4,38 +4,31 @@ import { Container, Dialog, Typography, IconButton, Tooltip } from '@mui/materia
import MUIDataTable from "mui-datatables"; import MUIDataTable from "mui-datatables";
import { useAxios } from './axios'; import { useAxios } from './axios';
import { useMutation, useQuery, useQueryClient } from 'react-query'; import { useMutation, useQuery, useQueryClient } from 'react-query';
import ContactDialog from './ContactDialog'; import StudentDialog from './StudentDialog';
import AddIcon from '@mui/icons-material/Add'; import AddIcon from '@mui/icons-material/Add';
function App() { function App() {
const client = useAxios(); const client = useAxios();
const queryClient = useQueryClient() const queryClient = useQueryClient()
const [open, setOpen] = React.useState(false); const [open, setOpen] = React.useState(false);
const [openedContact, setOpenedContact] = React.useState({}); const [openedStudent, setOpenedStudent] = React.useState({});
const { data, status } = useQuery('contacts', async () => { const { data, status } = useQuery('students', async () => {
const {data} = await client.get('/api/contacts'); const {data} = await client.get('/api/students');
return data.map(({ return data.map(({
uid, uid,
first_name, first_name,
last_name, last_name,
middle_name, middle_name,
telephone, discipline,
street, mark,
build,
build_k,
apartment
}) => { }) => {
return { return {
'first_name': first_name, 'first_name': first_name,
'last_name': last_name, 'last_name': last_name,
'middle_name': middle_name, 'middle_name': middle_name,
'telephone': telephone, 'discipline': discipline,
'street': street, 'mark': mark,
'build': build,
'build_k': build_k,
'apartment': apartment,
'display_name': `${first_name} ${last_name} ${middle_name}`, 'display_name': `${first_name} ${last_name} ${middle_name}`,
'display_address': `${street} ${build} ${build_k} ${apartment}`,
'uid': uid 'uid': uid
}; };
}) })
@ -50,23 +43,23 @@ function App() {
name: "display_name", label: "Имя" name: "display_name", label: "Имя"
}, },
{ {
name: "telephone", label: "Телефон" name: "mark", label: "Оценка"
}, },
{ {
name: "display_address", label: "Адрес" name: "discipline", label: "Дисциплина"
}, },
]; ];
const commitDelete = useMutation(async (uid) => { const commitDelete = useMutation(async (uid) => {
await client.delete(`/api/contacts/${uid}`); await client.delete(`/api/students/${uid}`);
}, { }, {
onSuccess: () => { onSuccess: () => {
queryClient.invalidateQueries('contacts'); queryClient.invalidateQueries('students');
} }
}); });
const handleRowClick = (rowData) => { const handleRowClick = (rowData) => {
setOpenedContact(data.find(({uid}) => uid === rowData[0])); setOpenedStudent(data.find(({uid}) => uid === rowData[0]));
setOpen(true); setOpen(true);
console.log(data.find(({uid}) => uid === rowData[0])); console.log(data.find(({uid}) => uid === rowData[0]));
}; };
@ -88,7 +81,7 @@ function App() {
return ( return (
<Container maxWidth="md"> <Container maxWidth="md">
<MUIDataTable <MUIDataTable
title={"Контактная книга"} title={"Зачётная книжка"}
data={data} data={data}
columns={columns} columns={columns}
options={{ options={{
@ -103,19 +96,19 @@ function App() {
onRowsDelete: handleRowsDelete onRowsDelete: handleRowsDelete
}} }}
/> />
<ContactDialog <StudentDialog
open={open} open={open}
onClose={handleClose} onClose={handleClose}
contact={openedContact} student={openedStudent}
/> />
</Container> </Container>
); );
function CustomToolbar() { function CustomToolbar() {
if (data !== undefined) return ( if (data !== undefined) return (
<Tooltip title='Новый контакт'> <Tooltip title='Новая запись'>
<IconButton onClick={() => { <IconButton onClick={() => {
setOpenedContact({}); setOpenedStudent({});
setOpen(true); setOpen(true);
}}> }}>
<AddIcon /> <AddIcon />

View file

@ -6,35 +6,32 @@ import { useAxios } from './axios';
import { useMutation, useQuery, useQueryClient } from 'react-query'; import { useMutation, useQuery, useQueryClient } from 'react-query';
function ContactDialog({ function StudentDialog({
open, open,
onClose, onClose,
contact, student,
}) { }) {
const client = useAxios(); const client = useAxios();
const queryClient = useQueryClient() const queryClient = useQueryClient()
const [uid, setUid] = React.useState(contact ? contact.uid : null); const [uid, setUid] = React.useState(student ? student.uid : null);
const [firstName, setFirstName] = React.useState(contact['first_name'] ?? ''); const [firstName, setFirstName] = React.useState(student['first_name'] ?? '');
const [lastName, setLastName] = React.useState(contact['last_name'] ?? ''); const [lastName, setLastName] = React.useState(student['last_name'] ?? '');
const [middleName, setMiddleName] = React.useState(contact['middle_name'] ?? ''); const [middleName, setMiddleName] = React.useState(student['middle_name'] ?? '');
const [telephone, setTelephone] = React.useState(contact['telephone'] ?? ''); const [discipline, setDiscipline] = React.useState(student['discipline'] ?? '');
const [street, setStreet] = React.useState(contact['street'] ?? ''); const [mark, setMark] = React.useState(student['mark'] ?? '');
const [build, setBuild] = React.useState(contact['build'] ?? '');
const [buildK, setBuildK] = React.useState(contact['build_k'] ?? '');
const [apartment, setApartment] = React.useState(contact['apartment'] ?? '');
const commitCreate = useMutation(async (payload) => client.post('/api/contacts', payload), { const commitCreate = useMutation(async (payload) => client.post('/api/students', payload), {
onSuccess: () => { onSuccess: () => {
queryClient.invalidateQueries('contacts'); queryClient.invalidateQueries('students');
onClose(); onClose();
} }
}); });
const commitUpdate = useMutation(async (payload) => client.put(`/api/contacts/${contact['uid']}`, payload), { const commitUpdate = useMutation(async (payload) => client.put(`/api/students/${student['uid']}`, payload), {
onSuccess: () => { onSuccess: () => {
queryClient.invalidateQueries('contacts'); queryClient.invalidateQueries('students');
queryClient.invalidateQueries(['contact', contact['uid']]); queryClient.invalidateQueries(['student', student['uid']]);
onClose(); onClose();
} }
}); });
@ -45,13 +42,10 @@ function ContactDialog({
first_name: firstName, first_name: firstName,
last_name: lastName, last_name: lastName,
middle_name: middleName, middle_name: middleName,
telephone: telephone, discipline: discipline,
street: street, mark: mark,
build: build,
build_k: buildK,
apartment: apartment
}; };
if (contact['uid']) { if (student['uid']) {
commitUpdate.mutate(data); commitUpdate.mutate(data);
} }
else { else {
@ -61,16 +55,13 @@ function ContactDialog({
}; };
const loadData = () => { const loadData = () => {
console.log(contact); console.log(student);
setUid(contact['uid'] ?? null); setUid(student['uid'] ?? null);
setFirstName(contact['first_name'] ?? ''); setFirstName(student['first_name'] ?? '');
setLastName(contact['last_name'] ?? ''); setLastName(student['last_name'] ?? '');
setMiddleName(contact['middle_name'] ?? ''); setMiddleName(student['middle_name'] ?? '');
setTelephone(contact['telephone'] ?? ''); setDiscipline(student['discipline'] ?? '');
setStreet(contact['street'] ?? ''); setMark(student['mark'] ?? '');
setBuild(contact['build'] ?? '');
setBuildK(contact['build_k'] ?? '');
setApartment(contact['apartment'] ?? '');
}; };
return ( return (
@ -83,7 +74,7 @@ function ContactDialog({
onEnter: loadData onEnter: loadData
}} }}
> >
<DialogTitle>Контакт</DialogTitle> <DialogTitle>Студент</DialogTitle>
<form onSubmit={handleFormSubmit}> <form onSubmit={handleFormSubmit}>
<DialogContent dividers> <DialogContent dividers>
<Grid container spacing={3}> <Grid container spacing={3}>
@ -116,47 +107,20 @@ function ContactDialog({
</Grid> </Grid>
<Grid item xs={12}> <Grid item xs={12}>
<TextField <TextField
id="telephone" id="discipline"
label="Телефон" label="Дисциплина"
value={telephone} value={discipline}
onChange={(event) => setTelephone(event.target.value)} onChange={(event) => setDiscipline(event.target.value)}
fullWidth fullWidth
required required
/> />
</Grid> </Grid>
<Grid item xs={6}> <Grid item xs={12}>
<TextField <TextField
id="street" id="mark"
label="Улица" label="Оценка"
value={street} value={mark}
onChange={(event) => setStreet(event.target.value)} onChange={(event) => setMark(event.target.value)}
fullWidth
/>
</Grid>
<Grid item xs={2}>
<TextField
id="build"
label="Дом"
value={build}
onChange={(event) => setBuild(event.target.value)}
fullWidth
/>
</Grid>
<Grid item xs={2}>
<TextField
id="build_k"
label="Корпус"
value={buildK}
onChange={(event) => setBuildK(event.target.value)}
fullWidth
/>
</Grid>
<Grid item xs={2}>
<TextField
id="apartment"
label="Квартира"
value={apartment}
onChange={(event) => setApartment(event.target.value)}
fullWidth fullWidth
/> />
</Grid> </Grid>
@ -172,4 +136,4 @@ function ContactDialog({
</Dialog> </Dialog>
); );
} }
export default ContactDialog; export default StudentDialog;

View file

@ -1,5 +1,7 @@
flask flask
flask_restful flask_restful
flask_socketio flask_socketio
flask_cors
pytz pytz
click
psycopg2 psycopg2