Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
dudava committed May 8, 2023
1 parent ea3f88a commit b138c85
Show file tree
Hide file tree
Showing 17 changed files with 262 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Flask/1.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import requests

requests.post('http://localhost:5000/api/v1/users', json={}, headers={'Content-Type': 'application/json'})
requests.get('http://localhost:5000/api/v1/users').json()
1 change: 1 addition & 0 deletions Flask/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# GigaAsessor
Binary file added Flask/__pycache__/db.cpython-38.pyc
Binary file not shown.
Binary file added Flask/__pycache__/user_resources.cpython-38.pyc
Binary file not shown.
13 changes: 13 additions & 0 deletions Flask/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import sqlite3

con = sqlite3.connect('db.sqlite', check_same_thread=False)


def db_init(path):
global con

with con:
con.execute("""CREATE TABLE IF NOT EXISTS User (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
login TEXT
)""")
Binary file added Flask/db.sqlite
Binary file not shown.
22 changes: 22 additions & 0 deletions Flask/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from flask import Flask, render_template
from flask_restful import Api

from user_resources import UserListResource
from db import db_init, con

app = Flask(__name__)
app.config['DEBUG'] = True

api = Api(app)
api.add_resource(UserListResource, '/api/v1/users')


@app.route('/')
def index():
return render_template('index.html')


if __name__ == '__main__':
db_init('db.sqlite')
print(con)
app.run('127.0.0.1', 5000)
25 changes: 25 additions & 0 deletions Flask/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
aniso8601==9.0.1
blinker==1.6.2
certifi==2023.5.7
charset-normalizer==3.1.0
click==8.1.3
colorama==0.4.6
exceptiongroup==1.1.1
Flask==2.3.2
Flask-RESTful==0.3.9
idna==3.4
importlib-metadata==6.6.0
iniconfig==2.0.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.2
packaging==23.1
pluggy==1.0.0
pytest==7.3.1
pytz==2023.3
requests==2.30.0
six==1.16.0
tomli==2.0.1
urllib3==2.0.2
Werkzeug==2.3.3
zipp==3.15.0
Binary file added Flask/static/assets/background.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flask/static/assets/dc.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flask/static/assets/tg.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions Flask/static/css/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.banner {
background: url(../assets/background.jpg);
color: white;
margin: 15px 0;
}

.use {
color: white;
}

.bot {
max-width: 500px;
flex-grow: 1;
outline: solid #94b9f1 2px;

border-radius: 21px;
}

Binary file added Flask/static/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 96 additions & 0 deletions Flask/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GigaAsessor</title>
<link rel="icon" href="favicon.png">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous">
<link rel="stylesheet" href="../static/css/index.css">
</head>
<body>

<div class="container">
<header class="d-flex flex-wrap justify-content-center py-3 mb-4 border-bottom">
<a href="/" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto link-body-emphasis text-decoration-none">
<img class="me-2" height="48" src="static/favicon.png">
<span class="fs-4">GigaAsessor</span>
</a>


</header>
</div>
<main>
<div class="banner position-relative overflow-hidden p-3 text-center bg-body-tertiary">
<div class="col-md-5 p-lg-5 mx-auto my-3">
<h1 class="display-4 fw-normal mb-4">GigaAsessor</h1>
<p class="lead fw-normal mb-4">Оценка и улучшение запросов для Chat-GPT</p>
<a class="btn btn-primary use" href="#main">Начать использовать</a>
</div>
<!-- счетчик пользователей -->


<button style="font-size: 18px; color: white;" type="button" class="btn btn-outline-primary user_counter mb-3">
Нашим сайтом воспользовались уже <span class="badge px-0 counter"></span> человек.
</button>


</div>



<div id="main" class="d-flex row row-cols-md-1 row-cols-lg-2 justify-content-center">
<div class="bot bg-body-tertiary m-2 text-center overflow-hidden">
<div class="my-3 p-3">
<h2 class="display-5">Telegram</h2>
<p class="lead">Переходи по ссылке</p>
<a class="btn btn-outline-primary" target="_blank" href="https://t.me/GigaAsessor_bot">Перейти в чат-бот</a>
</div>
<div class="mb-4 shadow-sm mx-auto" style="background-color: white; color: white; width: 80%; height: 300px; border-radius: 21px;"><img src="static/assets/tg.jpeg" width="300" class="img" alt=""></div>

</div>
<div class="bot bg-body-tertiary m-2 text-center overflow-hidden">
<div class="my-3 p-3">
<h2 class="display-5">Discord</h2>
<p class="lead">Переходи по ссылке</p>
<a class="btn btn-outline-primary" target="_blank" href="https://discord.com/api/oauth2/authorize?client_id=1104431313013985330&permissions=137439332416&scope=bot%20applications.commands">Перейти в чат-бот</a>
</div>
<div class="mb-4 shadow-sm mx-auto" style="background-color: white; color: white; width: 80%; height: 300px; border-radius: 21px;"><img src="static/assets/dc.jpeg" width="300" class="img" alt=""></div>

</div>
</div>
</main>



<div class="container">
<footer class="py-3 my-4">

<p class="text-center text-body-secondary">© 2023 GigaAsessor, Inc</p>
</footer>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe" crossorigin="anonymous"></script>
<script>

user_counter = document.querySelector('.user_counter .counter');
console.log(user_counter)
async function get_users_count() {
let response = await fetch('/api/v1/users');

if (response.ok) { // если HTTP-статус в диапазоне 200-299
// получаем тело ответа (см. про этот метод ниже)
let json = await response.json();
user_counter.innerHTML = json['users_count']
console.log(json)
} else {
alert("Ошибка HTTP: " + response.status);
}
setTimeout(get_users_count, 1000);
}

get_users_count()

</script>
</body>
</html>
6 changes: 6 additions & 0 deletions Flask/test_post_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import requests

for _ in range(3):
requests.post('http://localhost:5000/api/v1/users', json={'login': 'dudavik', 'ginger': 123}, headers={'Content-Type': 'application/json'}).json()
response = requests.get('http://localhost:5000/api/v1/users').json()
print(response)
43 changes: 43 additions & 0 deletions Flask/test_rest_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import requests
import unittest

from db import con


def post_test(*args, **kwargs):
return requests.post(*args, **kwargs).json()



class RESTApiTestPost(unittest.TestCase):
def test_null_post(self):
response = post_test('http://localhost:5000/api/v1/users', json={}, headers={'Content-Type': 'application/json'})
self.assertEqual(response['success'], 'OK')

def test_post_with_data(self):
response = post_test('http://localhost:5000/api/v1/users', json={'login': 'dudavik'}, headers={'Content-Type': 'application/json'})
self.assertEqual(response['success'], 'OK')

def test_post_with_many_data(self):
response = post_test('http://localhost:5000/api/v1/users', json={'login': 'dudavik', 'ginger': 123}, headers={'Content-Type': 'application/json'})
self.assertEqual(response['success'], 'OK')


class RESTApiTestGet(unittest.TestCase):
def test_get(self):
response = requests.get('http://localhost:5000/api/v1/users').json()
self.assertTrue(response['users_count'] >= 0)

def test_many_get(self):
with con:
con.execute('DELETE FROM User')
for _ in range(3):
post_test('http://localhost:5000/api/v1/users', json={'login': 'dudavik', 'ginger': 123}, headers={'Content-Type': 'application/json'})
response = requests.get('http://localhost:5000/api/v1/users').json()
self.assertEqual(response['users_count'], 3)


if __name__ == '__main__':
with con:
con.execute('DELETE FROM User')
unittest.main()
34 changes: 34 additions & 0 deletions Flask/user_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from flask import jsonify
from flask_restful import reqparse, abort, Resource
from db import con
from loguru import logger

def abort_if_user_not_found(user_id):
abort(404, message=f'User {user_id} not found')

class UserResource(Resource):
pass


class UserListResource(Resource):
parser = reqparse.RequestParser()
parser.add_argument('login')

def get(self):
with con:
users_count = con.execute('SELECT COUNT(*) FROM User').fetchone()[0]
return jsonify({
'success': 'OK',
'users_count': users_count
})

def post(self):
args = self.parser.parse_args()
with con:
sql = 'INSERT INTO User (login) VALUES (?)'
data = (args['login'], )
con.execute(sql, data)
return jsonify({
'success': 'OK'
})

0 comments on commit b138c85

Please sign in to comment.