Skip to content

Commit 6274f60

Browse files
committed
Users model, orm 적용
1 parent 8fcd697 commit 6274f60

23 files changed

Lines changed: 120 additions & 672 deletions

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
.venv
33

44
# vscode
5+
.DS_Store
56
.vscode
67

78
# python
89
__pycache__
910

10-
# database
11+
# testcode
12+
.pytest_cache

Pipfile

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,12 @@ autopep8 = "*"
1111

1212
[packages]
1313
flask = "*"
14+
flask-script = "*"
1415
flask-sqlalchemy = "*"
16+
flask-marshmallow = "*"
1517
flask-migrate = "*"
1618
pymysql = "*"
1719
flask-restplus = "*"
18-
flask-marshmallow = "*"
19-
marshmallow-jsonapi = "*"
20-
flask-script = "*"
21-
flask-basicauth = "*"
22-
pyjwt = "*"
23-
flask-bcrypt = "*"
2420

2521
[requires]
2622
python_version = "3.7"

Pipfile.lock

Lines changed: 1 addition & 91 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/.DS_Store

-6 KB
Binary file not shown.

app/__init__.py

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,27 @@
11
"""
2-
app init
2+
APP을 실행하기 위해 config file
33
"""
4-
from flask import Flask, render_template, jsonify
5-
from flask_restplus import Resource, Api, fields, reqparse
6-
from flask_sqlalchemy import SQLAlchemy
7-
from sqlalchemy.exc import SQLAlchemyError
8-
from sqlalchemy.sql import text
9-
from flask_marshmallow import Marshmallow
10-
from app.api.database import DB
11-
from app.api import REST_API
124

13-
SQLALCHEMY_DATABASE_URI = \
14-
("mysql+pymysql://{USER}:{PASSWORD}@{ADDR}:{PORT}/{NAME}?charset=utf8")
5+
from flask import Flask
6+
from app.api.database import DB, MA
7+
from app.constants import SQLALCHEMY_DATABASE_URI_FORMAT
158

16-
# 설명할 API에 대한 것
17-
MA = Marshmallow()
189

19-
def create_app() -> (Flask):
20-
""" create_app() 함수를 호출해 앱을 초기화 """
21-
22-
""" app config part """
23-
# 나중에 config는 다 빼야 할 것 같다.
10+
def create_app()->(Flask):
11+
""" create_app()을 호출하여 app을 초기화 """
2412
app = Flask(__name__)
2513
app.app_context().push()
26-
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI.format(
27-
USER="root",
28-
PASSWORD="1234",
29-
ADDR="127.0.0.1",
30-
PORT=3306,
31-
NAME="board"
32-
)
14+
15+
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI_FORMAT
3316
app.config['SQLALCHEMY_ECHO'] = True
3417
app.config['DEBUG'] = True
18+
3519
DB.init_app(app)
36-
REST_API.init_app(app)
3720
MA.init_app(app)
3821

39-
""" return part """
22+
@app.route('/')
23+
def root():
24+
""" main page """
25+
return "Hello World!"
26+
4027
return app

app/api/__init__.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

app/api/auth_type.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

app/api/database.py

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,9 @@
1-
from flask import jsonify
2-
from flask import make_response
1+
"""
2+
Create db
3+
"""
4+
35
from flask_sqlalchemy import SQLAlchemy
4-
from sqlalchemy.exc import IntegrityError
5-
from marshmallow import ValidationError
6-
from app.constants import STATUS_CODE
7-
from flask_restplus import reqparse
6+
from flask_marshmallow import Marshmallow
87

98
DB = SQLAlchemy()
10-
11-
class CRUD:
12-
body = ''
13-
status_code = STATUS_CODE.NOT_IMPLEMENTED
14-
def add(self, resource, schema):
15-
try:
16-
DB.session.add(resource)
17-
DB.session.commit()
18-
query = resource.query.get(resource.id)
19-
self.body = jsonify(schema.dump(resource).data)
20-
self.status_code = STATUS_CODE.CREATED
21-
except IntegrityError as error:
22-
DB.session.rollback()
23-
error_message = str(error)
24-
self.body = jsonify({"error": error_message, "type":"IntegrityError"})
25-
if "Duplicate entry" in error_message:
26-
self.status_code = 404
27-
else:
28-
self.status_code = 400
29-
finally:
30-
response = (self.body, self.status_code.value)
31-
response = make_response(response)
32-
33-
return response
34-
35-
def update(self, args, schema):
36-
try:
37-
for key, value in args.items():
38-
setattr(self, key, value)
39-
DB.session.commit()
40-
self.body = jsonify(schema.dump(self).data)
41-
self.status_code = STATUS_CODE.OK
42-
except IntegrityError as error:
43-
DB.session.rollback()
44-
error_message = str(error)
45-
self.body = jsonify({"error": error_message, "type":"IntegrityError"})
46-
if "Duplicate entry" in error_message:
47-
self.status_code = STATUS_CODE.CONFLICT
48-
else:
49-
self.status_code = STATUS_CODE.BAD_REQUEST
50-
finally:
51-
response = (self.body, self.status_code.value)
52-
response = make_response(response)
53-
return response
54-
55-
def delete(self, resource, schema):
56-
DB.session.delete(resource)
57-
DB.session.commit()
58-
self.body = jsonify({"message":"success"})
59-
self.status_code = STATUS_CODE.OK
60-
response = (self.body, self.status_code.value)
61-
response = make_response(response)
62-
return response
63-
64-
def select(self, name, password):
65-
66-
return "test"
9+
MA = Marshmallow()

app/constants.py

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,12 @@
1-
from enum import Enum
1+
"""
2+
상수 클래스
3+
"""
24

3-
class STATUS_CODE(Enum):
4-
OK = 200
5-
CREATED = 201
6-
NO_CONTENT = 204
7-
BAD_REQUEST = 400
8-
UNAUTHORIZED = 401
9-
FORBIDDEN = 403
10-
NOT_FOUND = 404
11-
METHOD_NOT_ALLOWED = 405
12-
CONFLICT = 409
13-
INTERNAL_SERVER_ERROR = 500
14-
NOT_IMPLEMENTED = 501
15-
BAD_GATEWAY = 502
16-
17-
responses = {item.value: item.name for item in STATUS_CODE}
18-
19-
def support_codes(unsupport_codes):
20-
unsupport_codes = [code.value for code in unsupport_codes]
21-
return {code: name for code, name in responses.items() if code not in unsupport_codes}
22-
23-
GET = support_codes(unsupport_codes=[
24-
STATUS_CODE.CREATED, STATUS_CODE.NO_CONTENT,
25-
STATUS_CODE.CONFLICT,
26-
STATUS_CODE.METHOD_NOT_ALLOWED,
27-
STATUS_CODE.NOT_IMPLEMENTED])
28-
POST = support_codes(unsupport_codes=[
29-
STATUS_CODE.OK, STATUS_CODE.NO_CONTENT,
30-
STATUS_CODE.METHOD_NOT_ALLOWED,
31-
STATUS_CODE.NOT_IMPLEMENTED])
32-
PATCH = support_codes(unsupport_codes=[
33-
STATUS_CODE.CREATED,
34-
STATUS_CODE.NO_CONTENT,
35-
STATUS_CODE.METHOD_NOT_ALLOWED,
36-
STATUS_CODE.NOT_IMPLEMENTED])
37-
DELETE = support_codes(unsupport_codes=[
38-
STATUS_CODE.CREATED,
39-
STATUS_CODE.CONFLICT,
40-
STATUS_CODE.METHOD_NOT_ALLOWED,
41-
STATUS_CODE.NOT_IMPLEMENTED])
5+
SQLALCHEMY_DATABASE_URI = ("mysql+pymysql://{USER}:{PASSWORD}@{ADDR}:{PORT}/{NAME}?charset=utf8")
6+
SQLALCHEMY_DATABASE_URI_FORMAT = SQLALCHEMY_DATABASE_URI.format(
7+
USER="root",
8+
PASSWORD="1234",
9+
ADDR="127.0.0.1",
10+
PORT=3306,
11+
NAME="board"
12+
)

app/posts/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)