Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions HTTPcommands.txt

This file was deleted.

Empty file added aiohttpproject/__init__.py
Empty file.
4 changes: 1 addition & 3 deletions routes.py → aiohttpproject/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from aiohttp import web
from views import homepagehandler, gethandler, getidhandler,\
putidhandler, postidhandler, deleteidhandler
from customers.viewsORM import gethandler, getidhandler, putidhandler, postidhandler, deleteidhandler


def setup_routes(app):
app.add_routes([
web.get('/', homepagehandler),
web.get('/students', gethandler),
web.get('/students/{id}', getidhandler),
web.put('/students/{id}', putidhandler),
Expand Down
Empty file added customers/__init__.py
Empty file.
51 changes: 51 additions & 0 deletions customers/fictures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from customers.models import Base
from sqlalchemy.orm import sessionmaker
import sqlalchemy as sql


def to_jsonORM(studobject):
a = ['id', 'name', 'surname']
userdict = list()
userdict.append(studobject.id)
userdict.append(studobject.name)
userdict.append(studobject.surname)
data = dict(zip(a, userdict))
return data


def to_json(string):
a = ['id', 'name', 'surname']
string = string.split()
data = dict(zip(a, string))
return (data)


async def postinfo(request):
data = await request.post()
name = data['name']
surname = data['surname']
return name, surname


def opendb():
engine = sql.create_engine('sqlite:///studentsoncourse.db')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
return Session()


# Help lines

'''session = opendb()
session.add_all([
Students(name='11', surname='111'),
Students(name='22', surname='222'),
Students(name='333', surname='333'),
])
session.commit()

session = opendb()
a = session.query(Students).filter_by(id=3)
for i in a:
print(i.name)
session.close()'''
12 changes: 12 additions & 0 deletions customers/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from sqlalchemy.ext.declarative import declarative_base
import sqlalchemy as sql

Base = declarative_base()


class Students(Base):
__tablename__ = 'students'

id = sql.Column(sql.Integer, primary_key=True)
name = sql.Column(sql.String)
surname = sql.Column(sql.String)
103 changes: 103 additions & 0 deletions customers/test_viewsORM.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from aiohttp import web
from aiohttpproject.urls import setup_routes
from os import rename, remove, path
from customers.models import Students
from customers.fictures import opendb, to_jsonORM


def help_open():
testData = [Students(id=1, name='name1', surname='surname1'),
Students(id=2, name='name2', surname='surname2'),
Students(id=3, name='name3', surname='surname3')]
if path.isfile('studentsoncourse.db'):
rename('studentsoncourse.db', 'studentsoncourseConst.db')
session = opendb()
session.add_all(testData)
session.commit()
session.close()
app = web.Application()
setup_routes(app)
return app


def help_close():
remove('studentsoncourse.db')
if path.isfile('studentsoncourseConst.db'):
rename('studentsoncourseConst.db', 'studentsoncourse.db')


async def test_to_json():
st = Students(id=1, name='name1', surname='surname1')
assert to_jsonORM(st) == dict(id=1, name='name1', surname='surname1')


async def test_postinfo():
pass


async def test_gethandler(aiohttp_client):
app = help_open()
client = await aiohttp_client(app)
resp = await client.get('/students')
help_close()
assert resp.status == 200
text = await resp.text()
testtext1 = '[{"id": 1, "name": "name1", "surname": "surname1"}, '
testtext2 = '{"id": 2, "name": "name2", "surname": "surname2"}, '
testtext3 = '{"id": 3, "name": "name3", "surname": "surname3"}]'
testtext = testtext1 + testtext2 + testtext3
assert testtext == text


async def test_getidhandler(aiohttp_client):
app = help_open()
client = await aiohttp_client(app)
resp = await client.get('/students/2')
help_close()
assert resp.status == 200
text = await resp.text()
assert '[{"id": 2, "name": "name2", "surname": "surname2"}]' == text


async def test_postidhandler(aiohttp_client):
params = {'name': 'name4', 'surname': 'surname4'}
app = help_open()
client = await aiohttp_client(app)
resp = await client.post('/students/2', data=params)
help_close()
assert resp.status == 409
text = await resp.text()
assert 'user with this id exists' == text
app = help_open()
client = await aiohttp_client(app)
resp = await client.post('/students/4', data=params)
help_close()
assert resp.status == 200
text = await resp.text()
assert '[{"id": 4, "name": "name4", "surname": "surname4"}]' == text


async def test_deleteidhandler(aiohttp_client):
app = help_open()
client = await aiohttp_client(app)
resp = await client.delete('/students/3')
help_close()
assert resp.status == 204


async def test_putidhandler(aiohttp_client):
params = {'name': 'name5', 'surname': 'surname5'}
app = help_open()
client = await aiohttp_client(app)
resp = await client.put('/students/5', data=params)
help_close()
assert resp.status == 200
text = await resp.text()
assert '[{"id": 5, "name": "name5", "surname": "surname5"}]' == text
app = help_open()
client = await aiohttp_client(app)
resp = await client.post('/students/4', data=params)
help_close()
assert resp.status == 200
text = await resp.text()
assert '[{"id": 4, "name": "name5", "surname": "surname5"}]' == text
25 changes: 4 additions & 21 deletions views.py → customers/views.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
from aiohttp import web


def to_json(string):
a = ['id', 'name', 'surname']
string = string.split()
data = dict(zip(a, string))
return (data)


async def postinfo(request):
data = await request.post()
name = data['name']
surname = data['surname']
return name, surname


def homepagehandler(request):
return web.FileResponse('website/index.html')
from customers.fictures import to_json, postinfo


def gethandler(request):
Expand All @@ -42,9 +25,9 @@ async def postidhandler(request):
a += user
if user.split()[0] == request.match_info['id']:
return web.Response(
status=409,
text='user with this id exists'
)
status=409,
text='user with this id exists'
)
with open('DataBase.txt', 'w') as dbfilewr:
data = await postinfo(request)
newstud = request.match_info['id'] + ' ' + data[0] + ' ' + data[1]
Expand Down
58 changes: 58 additions & 0 deletions customers/viewsORM.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from aiohttp import web
from customers.models import Students
from customers.fictures import opendb
from customers.fictures import to_jsonORM, postinfo


def gethandler(request):
session = opendb()
data = list()
for user in session.query(Students).all():
data.append(to_jsonORM(user))
session.close()
return web.json_response(data)


def getidhandler(request):
session = opendb()
user = session.query(Students).filter_by(id=request.match_info['id']).one_or_none()
if user is not None:
data = [to_jsonORM(user)]
session.close()
return web.json_response(data)


async def postidhandler(request):
session = opendb()
user = session.query(Students).filter_by(id=request.match_info['id']).one_or_none()
if user is not None:
session.close()
return web.Response(status=409, text='user with this id exists')
data = await postinfo(request)
newstud = Students(id=request.match_info['id'], name=data[0], surname=data[1])
session.add(newstud)
session.commit()
session.close()
return getidhandler(request)


def deleteidhandler(request):
statuscode = 0
session = opendb()
user = session.query(Students).filter_by(id=request.match_info['id']).one_or_none()
if user is not None:
session.delete(user)
statuscode = 204
session.commit()
session.close()
return web.Response(status=statuscode)


async def putidhandler(request):
id = request.match_info['id']
session = opendb()
user = session.query(Students).filter_by(id=id).one_or_none()
session.close()
if user is not None:
deleteidhandler(request)
return await postidhandler(request)
9 changes: 5 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from aiohttp import web
from routes import setup_routes
from aiohttpproject.urls import setup_routes

app = web.Application()
setup_routes(app)
web.run_app(app, host='127.0.0.1')
if __name__ == '__main__':
app = web.Application()
setup_routes(app)
web.run_app(app, host='127.0.0.1')
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 120
6 changes: 0 additions & 6 deletions website/index.html

This file was deleted.