-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User Management backend - Base - DB support #38
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7ba1ebc
Added config file
DoRTaL94 934df87
Added db support
DoRTaL94 a43367c
Updated requirements
DoRTaL94 2d56d44
Updated Vagrantfile
DoRTaL94 e0e2129
Added db models
DoRTaL94 2acd8a7
Added db auto creation on vagrant up
DoRTaL94 ea6a71a
Merge branch 'config' of https://github.com/DoRTaL94/edison into db-s…
DoRTaL94 77d67cf
Merge branch 'master' of https://www.github.com/knowabli/edison into …
DoRTaL94 384a58c
Updated .gitignore
ahinoamta 02fa0d2
Imported DB Models to app.py so the DB tables will be created
ahinoamta 1c34a90
Deleted Token model and some of User model to minimize the PR
ahinoamta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import os | ||
|
||
from flask import Flask | ||
from flask_sqlalchemy import SQLAlchemy | ||
from edison.config import get_config_object | ||
|
||
|
||
# Put app here so the entire app could import it. | ||
app = Flask(__name__) | ||
app.config.from_object(get_config_object(app.config["ENV"])) | ||
basedir = os.path.abspath(os.path.dirname(__file__)) | ||
db = SQLAlchemy(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,11 @@ def get_config_object(env_keyword: str): | |
class Config: | ||
ENV_KEYWORD = "" | ||
DEBUG = False | ||
# Turns off the Flask-SQLAlchemy event system | ||
SQLALCHEMY_TRACK_MODIFICATIONS = False | ||
SQLALCHEMY_DATABASE_URI = 'postgresql://postgres:[email protected]/edison' | ||
|
||
# PostgreSQL connection string should be updated once an actual production environment is established. | ||
class ProductionConfig(Config): | ||
ENV_KEYWORD = "production" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .token import Token | ||
from .user import User |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from edison import db | ||
|
||
|
||
class Token(db.Model): | ||
__tablename__ = 'token_blacklist' | ||
|
||
id = db.Column(db.Integer, primary_key=True) | ||
jti = db.Column(db.String(150), nullable=False, unique=True) | ||
creation_timestamp = db.Column(db.TIMESTAMP(timezone=False), nullable=False) | ||
|
||
def __repr__(self): | ||
return f"<Token: jti: {self.jti}, creation time: {self.creation_timestamp}>" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from edison import db | ||
|
||
|
||
class User(db.Model): | ||
__tablename__ = 'users' | ||
__table_args__ = {'extend_existing': True} | ||
|
||
id = db.Column(db.Integer, primary_key=True) | ||
username = db.Column(db.String(50), nullable=False, unique=True) | ||
password = db.Column(db.String(150), nullable=False) | ||
first_name = db.Column(db.String(50), nullable=False) | ||
last_name = db.Column(db.String(50), nullable=False) | ||
email = db.Column(db.String(150), nullable=False) | ||
|
||
def to_json(self): | ||
return { | ||
"username": self.username, | ||
"first_name": self.first_name, | ||
"last_name": self.last_name, | ||
"email": self.email | ||
} | ||
|
||
def __repr__(self): | ||
return f"<User: id = {self.id}, first_name = {self.first_name}, " \ | ||
f"last_name = {self.last_name}, username = {self.username}, email = {self.email}>" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
Flask==1.1.1 | ||
flask-sqlalchemy==2.4.1 | ||
psycopg2-binary==2.8.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure it really makes sense to keep the database data if you destroy the environment. And it also looks like not doing that would enable you to remove alot of code...
You do probably need to make sure that if you run
vagrant up
after you didvagrant halt
(withoutdestroy
), you don't end up clearing the DB by mistake...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we want to be on the safe side by saving the DB data ? what if by mistake someone performs
vagrant destroy
?I tested it and the data inside the DB remained unchanged.
I also removed db.sql content to see if it's doing anything, then (after
vagrant halt
) I executedvagrant up
and still the data remained.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really - Vagrant is your development environment, I don't think being too careful there is worth the extra effort and code. Also consider that if you're going to run tests with the database, you'll probably want it to be empty to begin with so the tests get a predictable initial state.
ok, you need to make sure this remains the case if/when you remove the backup and restore code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sense. We'll remove that code :)
OK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ifireball
I agree that we don't gain much for saving the DB before 'vagrant destroy', but I don't understand how does it affect the tests?
I thought we need to create separate DB for the tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, using a separate DB is probably the safest way to use a DB with tests, but:
Some often a "close approximation" where you get ~80% of the benefit with ~20% of the effort is worthwhile. So its possible to have the development DB be shared between the tests as long as the initial conditions are controlled and the tests know how to cleanup after themselves.