This commit is contained in:
Слободан Јелић 2025-07-17 13:52:47 +02:00
parent 4fc940b3a5
commit 71f2c9f914
13 changed files with 296 additions and 1 deletions

3
.env Normal file
View File

@ -0,0 +1,3 @@
FLASK_APP=app
FLASK_ENV=development
FLASK_DEBUG=1

8
.gitignore vendored
View File

@ -121,7 +121,6 @@ celerybeat.pid
*.sage.py
# Environments
.env
.venv
env/
venv/
@ -160,3 +159,10 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.rest-client
.vscode-server
.bash_history
.python_history
.dotnet
.dbclient
.gitconfig

3
DELETE.http Normal file
View File

@ -0,0 +1,3 @@
DELETE http://localhost:5000/student/18 HTTP/1.1
Accept: application/json
Content-Type: application/json

10
Dockerfile Normal file
View File

@ -0,0 +1,10 @@
FROM python:latest
WORKDIR /root
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN pip install --upgrade pip
COPY ./requirements.txt requirements.txt
RUN pip install -r requirements.txt

2
GET.http Normal file
View File

@ -0,0 +1,2 @@
GET http://localhost:5000 HTTP/1.1
Accept: application/json

3
GETALL.http Normal file
View File

@ -0,0 +1,3 @@
GET http://localhost:5000/student HTTP/1.1
Accept: application/json
Content-Type: application/json

9
POST.http Normal file
View File

@ -0,0 +1,9 @@
POST http://localhost:5000/student HTTP/1.1
Accept: application/json
Content-Type: application/json
{
"ime": "Marko",
"prezime": "Markovic",
"email": "marko.markovic@grf.bg.ac.rs"
}

9
PUT.http Normal file
View File

@ -0,0 +1,9 @@
PUT http://localhost:5000/student/16 HTTP/1.1
Accept: application/json
Content-Type: application/json
{
"ime": "Marko",
"prezime": "Markoviccccc",
"email": "marko.markovic@gmail.com"
}

28
compose.yaml Normal file
View File

@ -0,0 +1,28 @@
services:
app:
build: .
volumes:
- .:/root
expose:
- 5000
ports:
- 5000:5000
env_file:
- .env
entrypoint: /bin/bash
tty: true
user: root
depends_on:
- db
db:
image: postgres
volumes:
- postgres_data:/var/lib/postresql/data
expose:
- 5432
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD=pas123
volumes:
postgres_data:

8
requirements.txt Normal file
View File

@ -0,0 +1,8 @@
flask
sqlalchemy
flask-sqlalchemy
psycopg2
python-dotenv
flask-cors
sqlalchemy-utils
flask-migrate

119
src/app.py Normal file
View File

@ -0,0 +1,119 @@
# from flask import Flask, request
import datetime
from flask import Flask, request
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import DateTime, create_engine
from sqlalchemy_utils import database_exists, create_database
import logging
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:pas123@db/app'
db = SQLAlchemy(app)
cors = CORS(app)
app.app_context().push()
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
if not database_exists(engine.url):
create_database(engine.url)
class Student(db.Model):
id = db.Column(db.Integer, primary_key=True)
ime = db.Column(db.String(100), nullable=False)
prezime = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(100), nullable=True)
def __init__(self, ime, prezime, email = None):
self.ime = ime
self.prezime = prezime
self.email = email
def __repr__(self):
return f"Ime: {self.ime}, Prezime: {self.prezime}, Email: {self.email}"
db.create_all()
@app.route('/', methods=['GET'])
def pozdrav():
return {
'poruka': 'Pozdrav'
}
@app.route('/student', methods = ['GET'])
def dohvatiStudente():
studenti = Student.query.order_by(Student.id.asc()).all()
listaStudenata = [
{
'id': s.id,
'ime': s.ime,
'prezime': s.prezime,
'email': s.email
}
for s in studenti
]
return listaStudenata
@app.route('/student', methods = ['POST'])
def kreirajStudenta():
ime = request.json['ime']
prezime = request.json['prezime']
email = request.json['email']
student = Student(
ime=ime,
prezime=prezime,
email=email
)
db.session.add(student)
db.session.commit()
return {
'id': student.id,
'ime': student.ime,
'prezime': student.prezime,
'email': student.email
}
@app.route('/student/<id>', methods = ['PUT'])
def promeniStudenta(id):
student = Student.query.filter_by(id=id)
ime = request.json['ime']
prezime = request.json['prezime']
email = request.json['email']
student.update({
'ime': ime,
'prezime': prezime,
'email': email
})
db.session.commit()
student = student.one()
return {
'id': student.id,
'ime': student.ime,
'prezime': student.prezime,
'email': student.email
}
@app.route('/student/<id>', methods = ['DELETE'])
def obrisiStudenta(id):
student = Student.query.filter_by(id=id).one()
db.session.delete(student)
db.session.commit()
return f"Student (id: {student.id}) obrisan."
if __name__ == '__main__':
app.run()

18
src/read_db.py Normal file
View File

@ -0,0 +1,18 @@
from sqlalchemy import create_engine
engine = create_engine('postgresql://postgres:pas123@db/app')
conn = engine.connect()
result = conn.execute(
'SELECT * FROM student'
)
# for row in result:
# print(row)
# result.close()
# conn.close()

77
src/sqlalch.py Normal file
View File

@ -0,0 +1,77 @@
from typing import List
from sqlalchemy import create_engine, Integer, String, ForeignKey, Column, Date
from sqlalchemy_utils import database_exists, create_database
from sqlalchemy.orm import DeclarativeBase, mapped_column, Session, Mapped, relationship
engine = create_engine('postgresql://postgres:pas123@db/sqlalchdb')
if not database_exists(engine.url):
create_database(engine.url)
class Base(DeclarativeBase):
pass
class Student(Base):
__tablename__='student'
id = mapped_column(Integer, primary_key=True)
ime = mapped_column(String, nullable=False)
prezime = mapped_column(String, nullable=False)
email = mapped_column(String, nullable=True)
datum_rodjenja = mapped_column(Date, nullable=True)
studentska_grupa_id : Mapped[int] = mapped_column(ForeignKey("studentska_grupa.id"), nullable=True)
studentska_grupa: Mapped["StudentskaGrupa"] = relationship(back_populates="studenti")
def __init__(
self,
ime,
prezime,
email = None,
datum_rodjenja = None,
studentska_grupa = None
):
self.ime = ime
self.prezime = prezime
self.email = email
self.datum_rodjenja = datum_rodjenja
self.studentska_grupa = studentska_grupa
class StudentskaGrupa(Base):
__tablename__ = 'studentska_grupa'
id = mapped_column(Integer, primary_key=True)
naziv = mapped_column(String, nullable=False)
studenti: Mapped[List["Student"]] = relationship(back_populates="studentska_grupa")
def __init__(self,
naziv):
self.naziv = naziv
Base.metadata.create_all(engine)
sg = StudentskaGrupa(naziv='A grupa')
s = Student(
ime='Marko',
prezime='Markovic',
email='marko.markovic@gmail.com',
datum_rodjenja='1995-02-02',
studentska_grupa=sg
)
f = Student(
ime='Milan',
prezime='Milanovic',
email='milan.milanovic@grf.bg.ac.rs',
datum_rodjenja='1996-06-15',
studentska_grupa=sg
)
with Session(engine) as session:
session.add(s)
session.add(f)
session.commit()