Skip to content

Commit

Permalink
cleaned up files
Browse files Browse the repository at this point in the history
  • Loading branch information
teddykoker committed May 2, 2017
1 parent 403ba50 commit aa866bf
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion server/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Base = declarative_base()
Base.query = db_session.query_property()


def init_db():
# import all modules here that might define models so that
# they will be registered properly on the metadata. Otherwise
Expand All @@ -24,4 +25,3 @@ def init_db():
@app.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()

4 changes: 1 addition & 3 deletions server/models/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@
from sqlalchemy.orm import relationship
from ..database import Base


class Book(Base):
__tablename__ = 'books'
id = Column(Integer, primary_key=True)
isbn = Column(String(13), unique=True)
title = Column(String(128))
listings = relationship("Listing", back_populates="book")


def __init__(self, isbn, title):
self.isbn = isbn
self.title = title


def __repr__(self):
return '<Book %r>' % (self.title)


def serialized(self):
return {'title': self.title, 'isbn': self.isbn}
2 changes: 1 addition & 1 deletion server/models/listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from sqlalchemy.orm import relationship
from ..database import Base


class Listing(Base):
__tablename__ = 'listings'
id = Column(Integer, primary_key=True)
Expand All @@ -15,6 +16,5 @@ def __init__(self, price):
def __repr__(self):
return '<Listing>'


def serialized(self):
return {'book': self.book.serialized(), 'price': self.price}
5 changes: 1 addition & 4 deletions server/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import bcrypt


class User(Base):

__tablename__ = 'users'
Expand All @@ -11,27 +12,23 @@ class User(Base):
email = Column(String(128), unique=True)
password = Column(Text)


def __init__(self, username, email, password):
self.username = username
self.email = email
self.password = bcrypt.hashpw(password.encode('utf-8'),
bcrypt.gensalt())


def check_password(self, password):
return bcrypt.checkpw(password.encode('utf-8'),
self.password.encode('utf-8'))


def __repr__(self):
return '<User %r>' % (self.name)

@staticmethod
def username_taken(username):
return db_session.query(exists().where(User.username == username)).scalar()


@staticmethod
def email_taken(email):
return db_session.query(exists().where(User.email == email)).scalar()
6 changes: 4 additions & 2 deletions server/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def signup():
user = User(request.json['username'], request.json['email'],
request.json['password'])

db_session.add(user);
db_session.add(user)
db_session.commit()

# registration was successful
Expand Down Expand Up @@ -77,7 +77,9 @@ def logout():
@app.route('/api/new-listing', methods=['POST'])
def new_listing():

book = Book.query.filter(Book.isbn == request.json['listing']['book']['isbn']).first()
book = Book.query.filter(
Book.isbn == request.json['listing']['book']['isbn']).first()

if book is None:
book = Book(request.json['listing']['book']['isbn'],
request.json['listing']['book']['title'])
Expand Down

0 comments on commit aa866bf

Please sign in to comment.