Add models and Add fake data
This commit is contained in:
parent
d5e95e25d0
commit
766c0d868f
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
migrations/
|
||||||
|
database.sqlite
|
1
Pipfile
1
Pipfile
@ -19,3 +19,4 @@ flask-mail = "*"
|
|||||||
python-dotenv = "*"
|
python-dotenv = "*"
|
||||||
flask-script = "*"
|
flask-script = "*"
|
||||||
flask-migrate = "*"
|
flask-migrate = "*"
|
||||||
|
faker = "*"
|
||||||
|
14
app.py
14
app.py
@ -1,11 +1,20 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Librarys
|
||||||
import os
|
import os
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from flask_restplus import Resource, Api
|
from flask_restplus import Resource, Api
|
||||||
from dotenv import load_dotenv, find_dotenv
|
from dotenv import load_dotenv, find_dotenv
|
||||||
|
from models import db, User, News, Comment
|
||||||
|
|
||||||
load_dotenv(find_dotenv())
|
load_dotenv(find_dotenv())
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
# Config Flask
|
||||||
|
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
|
||||||
|
|
||||||
|
# Config API
|
||||||
api = Api(app)
|
api = Api(app)
|
||||||
PRE_URL = '/api/v1/'
|
PRE_URL = '/api/v1/'
|
||||||
|
|
||||||
@ -32,10 +41,11 @@ class Logout(Resource):
|
|||||||
|
|
||||||
|
|
||||||
@api.route(PRE_URL + 'news')
|
@api.route(PRE_URL + 'news')
|
||||||
class News(Resource):
|
class News_all(Resource):
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
return {'hello': 'world'}
|
my_news = News.query.all()
|
||||||
|
return ([i.serialize for i in my_news])
|
||||||
|
|
||||||
def post(self):
|
def post(self):
|
||||||
return {'hello': 'world'}
|
return {'hello': 'world'}
|
||||||
|
48
fake_data.py
Normal file
48
fake_data.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
from werkzeug.security import generate_password_hash
|
||||||
|
from models import db, User, News, Comment
|
||||||
|
from faker import Factory
|
||||||
|
from random import randint
|
||||||
|
|
||||||
|
# Spanish
|
||||||
|
fake = Factory.create('es_ES')
|
||||||
|
|
||||||
|
# Reload tables
|
||||||
|
db.drop_all()
|
||||||
|
db.create_all()
|
||||||
|
|
||||||
|
# Make 100 fake users
|
||||||
|
for num in range(100):
|
||||||
|
profile = fake.simple_profile()
|
||||||
|
username = profile['username']
|
||||||
|
mail = profile['mail']
|
||||||
|
password = generate_password_hash('123')
|
||||||
|
# Save in database
|
||||||
|
my_user = User(username=username, mail=mail, password=password)
|
||||||
|
db.session.add(my_user)
|
||||||
|
|
||||||
|
print('Users created')
|
||||||
|
|
||||||
|
# Make 1000 fake news
|
||||||
|
for num in range(1000):
|
||||||
|
title = fake.sentence()
|
||||||
|
link = fake.uri()
|
||||||
|
user_id = randint(1, 100)
|
||||||
|
# Save in database
|
||||||
|
my_news = News(title=title, link=link, user_id=user_id)
|
||||||
|
db.session.add(my_news)
|
||||||
|
|
||||||
|
print('News created')
|
||||||
|
|
||||||
|
# Make 10000 fake comments
|
||||||
|
for num in range(10000):
|
||||||
|
text = fake.text()
|
||||||
|
news_id = randint(1, 1000)
|
||||||
|
user_id = randint(1, 100)
|
||||||
|
# Save in database
|
||||||
|
my_comment = Comment(text=text, news_id=news_id, user_id=user_id)
|
||||||
|
db.session.add(my_comment)
|
||||||
|
|
||||||
|
print('Comments created')
|
||||||
|
|
||||||
|
# Save
|
||||||
|
db.session.commit()
|
32
models.py
32
models.py
@ -1,15 +1,19 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Librarys
|
# Librarys
|
||||||
|
import os
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from flask_script import Manager
|
from flask_script import Manager
|
||||||
from flask_migrate import Migrate, MigrateCommand
|
from flask_migrate import Migrate, MigrateCommand
|
||||||
|
from dotenv import load_dotenv, find_dotenv
|
||||||
|
|
||||||
|
load_dotenv(find_dotenv())
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
# Settings
|
# Settings
|
||||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.sqlite'
|
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URI')
|
||||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||||
|
|
||||||
# Variables
|
# Variables
|
||||||
@ -28,7 +32,7 @@ class User(db.Model):
|
|||||||
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
username = db.Column(db.String(100))
|
username = db.Column(db.String(100))
|
||||||
email = db.Column(db.String(200))
|
mail = db.Column(db.String(200))
|
||||||
password = db.Column(db.String(106))
|
password = db.Column(db.String(106))
|
||||||
created_at = db.Column(
|
created_at = db.Column(
|
||||||
db.DateTime, nullable=False, default=datetime.utcnow)
|
db.DateTime, nullable=False, default=datetime.utcnow)
|
||||||
@ -45,14 +49,25 @@ class News(db.Model):
|
|||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
title = db.Column(db.String(100))
|
title = db.Column(db.String(100))
|
||||||
link = db.Column(db.String(500))
|
link = db.Column(db.String(500))
|
||||||
|
user_id = db.Column(
|
||||||
|
db.Integer, db.ForeignKey('users.id'), nullable=False)
|
||||||
created_at = db.Column(
|
created_at = db.Column(
|
||||||
db.DateTime, nullable=False, default=datetime.utcnow)
|
db.DateTime, nullable=False, default=datetime.utcnow)
|
||||||
|
|
||||||
user_id = db.Column(
|
# Relations
|
||||||
db.Integer, db.ForeignKey('user.id'), nullable=False)
|
|
||||||
user = db.relationship(
|
user = db.relationship(
|
||||||
'User', backref=db.backref('News', lazy=True))
|
'User', backref=db.backref('News', lazy=True))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serialize(self):
|
||||||
|
"""Return object data in easily serializeable format"""
|
||||||
|
return {
|
||||||
|
'id': self.id,
|
||||||
|
'title': self.title,
|
||||||
|
'link': self.link,
|
||||||
|
'user_id': self.user_id
|
||||||
|
}
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<News Table {0}>'.format(self.title)
|
return '<News Table {0}>'.format(self.title)
|
||||||
|
|
||||||
@ -64,11 +79,16 @@ class Comment(db.Model):
|
|||||||
__tablename__ = 'comments'
|
__tablename__ = 'comments'
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
text = db.Column(db.String(1000))
|
text = db.Column(db.String(1000))
|
||||||
|
news_id = db.Column(
|
||||||
|
db.Integer, db.ForeignKey('news.id'), nullable=False)
|
||||||
|
user_id = db.Column(
|
||||||
|
db.Integer, db.ForeignKey('users.id'), nullable=False)
|
||||||
created_at = db.Column(
|
created_at = db.Column(
|
||||||
db.DateTime, nullable=False, default=datetime.utcnow)
|
db.DateTime, nullable=False, default=datetime.utcnow)
|
||||||
|
|
||||||
news_id = db.Column(
|
# Relations
|
||||||
db.Integer, db.ForeignKey('news.id'), nullable=False)
|
user = db.relationship(
|
||||||
|
'User', backref=db.backref('Comment', lazy=True))
|
||||||
news = db.relationship(
|
news = db.relationship(
|
||||||
'News', backref=db.backref('Comment', lazy=True))
|
'News', backref=db.backref('Comment', lazy=True))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user