Started using Bootstrap for styling, moved app factory, added tests, started work on login and registration templates, and more

This commit is contained in:
Andrew Bryant 2024-05-05 16:15:55 -04:00
parent 5d9ffffb79
commit 4503603915
19 changed files with 327 additions and 86 deletions

View File

@ -5,19 +5,34 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
## [0.0.7] - 2024-05-05
### Added
1. Models subpackage:
- User class:
* get_dogs() method
* __str__() method that returns name attribute
* status attribute
- Dog class:
* get_age() method to calculate age of dog from birth_date attribute
* __str__() method that returns name attribute
- Visit class:
* __str__() method that returns date_time attribute
* __repr__() method that returns to_dict() method
2. App factory now in app_factory.py
3. Development and testing now have different configurations.
- development_config.py
- tests/testing_config.py
4. Makefile:
- Default help target
5. Views submodule:
- users.py:
* user_dogs_panel() view for users dashboard to config dogs
6. Jinja templates:
- Users my dogs panel for users dashboard
7. Tests:
- test_dog_model.py to test Dog model
### Changed
@ -25,6 +40,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- User class:
* get_bookings() method renamed to get_visits()
* get_bookings_history() method renamed to get_visit_history()
- Dog class:
* age attribute renamed to birth_date
* status attribute now linked to User status
* __repr__() method now return to_dict() method
- Dog Submodule:
* BreedSize values are now specific
- Visit Submodule:
* VisitType values are now specific
2. Tests package:
- conftest.py:
* Updated to use new app_factory.py
@ -32,24 +55,58 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- test_user_model.py:
* Updated to use new app_factory.py
* Variables updated to reflect methods in User model
3. Views package:
* test number of dogs user owns
3. Views submodel:
- Users dashboard route:
* Update User model object usage to reflect renamed methods
* Renamed variable user_dogs to user_pets
* user_pets template variable renamed to user_dogs
- User API endpoints:
* user_pets() view renamed to user_dogs()
* user_dogs() endpoint renamed to /users/<username>/dogs
4. Makefile:
- Target commands are no longer echoed when run.
5. Jinja templates:
- Login and Registration:
* Initial form work
- Users Dashboard:
* Page title now includes "Account Dashboard"
* pets_overview.html renamed to dogs_overview.html
* dogs_overview.html started using Bootstrap for styling
* visits_overview.html started using Bootstrap for styling
### Removed
1. Models subpackage:
- User class:
* removed redundent import from get_visit_history() method
2. Nix shell:
- Exported environment variables
3. Tests:
- test_user_model.py:
* Unused imports
### Fixed
1. Fixed typo in CHANGELOG.md
2. Makefile:
- Comments used by help target now use correct symbol (##)
3. Models subpackage:
- Dog class:
* to_dict() method should now return dict representation of Dog object
- User class:
* get_visit_history() method renamed to get_visit_overview()
* to_dict() method should now return dict representation of User object
- Visit class:
* to_dict() method should now return dict representation of Visit object
4. Jinja templates:
- Users Dashboard:
* Page header changed to "Account Dashboard"
* Update dog overview include
5. App factory:
- DB dog test data updated to use birth_date attribute
- DB test data for the dog Rufus changed to reflect actual breed_size
## [0.0.6] - 2024-04-28

View File

@ -2,6 +2,7 @@
from datetime import datetime
from sqlalchemy import Enum
from sqlalchemy.ext.hybrid import hybrid_property
# Import db
from accounts.models import db
@ -13,23 +14,44 @@ class Dog(db.Model):
__tablename__ = 'dogs'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
owner = db.relationship('User', back_populates='dogs')
owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
name = db.Column(db.String, nullable=False)
breed_size = db.Column(Enum(BreedSize), default=BreedSize.MEDIUM)
age = db.Column(db.DateTime, default=datetime(1970, 1, 1, 0, 0, 0))
owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
owner = db.relationship('User', back_populates='dogs')
birth_date = db.Column(db.DateTime, default=datetime(1970, 1, 1))
visits = db.relationship('Visit', back_populates='dogs')
status = db.Column(Enum(Status), default=Status.ACTIVE)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow)
def __str__(self):
return self.name
def __repr__(self):
return f'{self.name}'
return self.to_dict()
@hybrid_property
def status(self):
return self.owner.status
def get_age(self):
current_date = datetime.now()
age = (current_date.year - self.birth_date.year
- ((current_date.month, current_date.day) < (self.birth_date.month, self.birth_date.day)))
return age
def to_dict(self):
return {
'id': self.id,
'owner': self.owner,
'owner_id': self.owner_id,
'name': self.name,
'breed': self.breed,
'owner': self.owner
'breed_size': self.breed_size,
'birth_date': self.birth_date,
'age': self.get_age(),
'visits': self.visits,
'status': self.status,
'created_at': self.created_at,
'updated_at': self.updated_at
}

View File

@ -2,7 +2,8 @@ from enum import Enum
from enum import auto
class BreedSize(Enum):
SMALL = auto()
MEDIUM = auto()
LARGE = auto()
SMALL = 'Small'
MEDIUM = 'Medium'
LARGE = 'Large'
XLARGE = 'X-large'

View File

@ -1,8 +1,12 @@
from datetime import datetime
from sqlalchemy import Enum
# Import db
from accounts.models import db
from accounts.models.status import Status
class User(db.Model):
__tablename__ = 'users'
@ -14,11 +18,15 @@ class User(db.Model):
password = db.Column(db.String, nullable=False)
dogs = db.relationship('Dog', back_populates='owner')
visits = db.relationship('Visit', back_populates='owner')
status = db.Column(Enum(Status), default=Status.ACTIVE)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow)
def __str__(self):
return self.name
def __repr__(self):
return f'{self.username}'
return self.to_dict()
# Get all dogs currently set
def get_dogs(self):
@ -29,15 +37,23 @@ class User(db.Model):
return [visit for visit in self.visits]
# Retrieve past booked visits
def get_visit_history(self):
def get_visit_overview(self):
current_time = datetime.now()
return [visit for visit in self.visits if visit.date_time < current_time]
def to_dict(self):
return {
'name': self.name,
'username': self.username,
'email': self.email,
'address': self.address,
'password': self.password,
'dogs': self.get_dogs(),
'visits': self.get_visits(),
'status': self.status,
'created_at': self.created_at,
'updated_at': self.updated_at
}

View File

@ -17,12 +17,18 @@ class Visit(db.Model):
date_time = db.Column(db.DateTime, nullable=False)
owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
owner = db.relationship('User', back_populates='visits')
visit_type = db.Column(Enum(VisitType), default=VisitType.MEDIUM_MINUTE_WALK)
visit_type = db.Column(Enum(VisitType), default=VisitType.MEDIUM_WALK)
dog_id = db.Column(db.Integer, db.ForeignKey('dogs.id'), nullable=False)
dogs = db.relationship('Dog', back_populates='visits')
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow)
def __str__(self):
return self.date_time
def __repr__(self):
return self.to_dict()
def get_date(self):
year = self.date_time.year
month = self.date_time.month
@ -42,9 +48,14 @@ class Visit(db.Model):
def to_dict(self):
return {
'ID': self.id,
'Date-Time': self.date_time,
'Owner-ID': self.owner_id,
'Dog-ID': self.dog_id
'id': self.id,
'date_time': self.date_time,
'owner_id': self.owner_id,
'owner': self.owner,
'visit_type': self.visit_type,
'dog_id': self.dog_id,
'dogs': self.dogs,
'created_at': self.created_at,
'updated_at': self.update_at
}

View File

@ -2,8 +2,8 @@ from enum import Enum
from enum import auto
class VisitType(Enum):
SMALL_MINUTE_WALK = auto()
MEDIUM_MINUTE_WALK = auto()
LARGE_MINUTE_WALK = auto()
HOUSE_SITTING = auto()
SMALL_WALK = '15-minute walk'
MEDIUM_WALK = '30-minute walk'
LARGE_WALK = '45-minute walk'
HOUSE_SITTING = 'House sitting'

View File

@ -1,10 +1,21 @@
{% extends "base.html" %}
{% set title %}Account Login{% endset %}
{% set title %}Login{% endset %}
{% block content %}
Login here
---------------
Soon to come ;)
<h1 class="text-center h1">Login</h1>
<hr>
<form>
<div class="mb-3">
<label for="login-email" class="form-label">Email address</label>
<input type="email" class="form-control" id="login-email" placeholder="example@email.com">
</div>
<div class="mb-3">
<label for="login-password" class="form-label">Password</label>
<input type="password" class="form-control" id="login-password">
</div>
</form>
{% endblock %}

View File

@ -3,8 +3,42 @@
{% set title %}Account Registration{% endset %}
{% block content %}
Register for an account
-----------------------
Soon to come ;)
<h1 class="text-center h1">Register for an account</h1>
<hr>
<form>
<div class="row">
<div class="col mb-3">
<label for="user-first-name" class="form-label">First Name</label>
<input type="text" class="form-control" id="user-first-name" placeholder="John">
</div>
<div class="col mb-3">
<label for="user-last-name" class="form-label">Last Name</label>
<input type="text" class="form-control" id="user-last-name" placeholder="Doe">
</div>
</div>
<div class="row">
<div class="col mb-3">
<label for="user-email" class="form-label">Email address</label>
<input type="email" class="form-control" id="user-email" placeholder="example@email.com">
</div>
</div>
<div class="row">
<div class="col mb-3">
<label for="user-password" class="form-label">Password</label>
<input type="password" class="form-control" id="user-password">
</div>
</div>
<div class="row">
<div class="col mb-3">
<label for="user-password-confirm" class="form-label">Confirm Password</label>
<input type="password" class="form-control" id="user-password-confirm">
</div>
</div>
<div class="row">
<button type="submit" class="row btn btn-primary">Submit</button>
</div>
</form>
{% endblock %}

View File

@ -1,19 +1,16 @@
{% extends 'base.html' %}
{% set title %}{{ user.username }} - Dashboard{% endset %}
{% set title %}{{ user.username }} - Account Dashboard{% endset %}
{% block content %}
<h1>Account Overview<h1>
<h1 class="text-center h1">Account Dashboard</h1>
<hr>
<h3>Pets</h3>
{% include 'users/dashboard/pets_overview.html' %}
{% include 'users/dashboard/dogs_overview.html' %}
<hr>
<h3>Booking History</h3>
{% include 'users/dashboard/visits_overview.html' %}
{% endblock %}

View File

@ -0,0 +1,27 @@
<div class="table-responsive">
<table class="table table-sm table-responsive table-striped">
<thead>
<tr>
<th scope="colgroup" colspan="4" class="text-center h3">Dogs overview</th>
</tr>
<tr>
<th scope="col">Name</th>
<th scope="col">Breed size</th>
<th scope="col">Birth date</th>
<th scope="col">Age</th>
</tr>
</thead>
<tbody class="table-group-divider">
{% for dog in user_dogs %}
<tr>
<td>{{ dog.name }}</td>
<td>{{ dog.breed_size.value }}</td>
<td>{{ dog.birth_date.strftime('%Y-%m-%d') }}</td>
<td>{{ dog.get_age() }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>

View File

@ -1,17 +0,0 @@
<table>
<tr>
<td>Name</td>
<td>Breed</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>

View File

@ -1,18 +1,27 @@
<table>
<div class="table-responsive">
<table class="table table-sm table-striped">
<thead>
<tr>
<td>Type</td>
<td>Date</td>
<td>Time</td>
<td>Dog</td>
<th scope="colgroup" colspan="4" class="text-center h3">Visits overview</th>
</tr>
{% for visit in user.visits %}
<tr>
<td>{{ visit.type }}</td>
<th scope="col">Type</th>
<th scope="col">Date</th>
<th scope="col">Time</th>
<th scope="col">Pets</th>
</tr>
</thead>
<tbody class="table-group-divider">
{% for visit in user_book_history %}
<tr>
<td>{{ visit.visit_type.value }}</td>
<td>{{ visit.get_date() }}</td>
<td>{{ visit.get_time() }}</td>
<td>{{ visit.dog }}</td>
<td>{{ visit.dogs }}</td>
</tr>
{% endfor %}
</table>
</tbody>
</table>
</div>

View File

@ -0,0 +1,12 @@
{% extends 'base.html' %}
{% set title %}{{ user.username }} - My Dogs{% endset %}
{% block content %}
<h1>My Dogs</h1>
<hr>
{% include 'users/my_dogs/dogs_panel.html' %}
{% endblock %}

View File

@ -0,0 +1,27 @@
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th scope="colgroup" colspan="4" class="text-center h3">My dogs</th>
</tr>
<tr>
<th scope="col">Name</th>
<th scope="col">Breed size</th>
<th scope="col">Birth date</th>
<th scope="col">Age</th>
</tr>
</thead>
<tbody class="table-group-divider">
{% for dog in user_dogs %}
<tr>
<td>{{ dog.name }}</td>
<td>{{ dog.breed_size.value }}</td>
<td>{{ dog.birth_date.strftime('%Y-%m-%d') }}</td>
<td>{{ dog.get_age() }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>

View File

@ -18,20 +18,23 @@ def user_dashboard(username):
# Retrieve user data
user = db.session.execute(db.select(User).filter_by(username=username)).scalar_one()
return render_template('users/dashboard/base.html', user=user, user_pets=user.get_dogs(), user_book_history=user.get_visits())
return render_template('users/dashboard/base.html', user=user, user_dogs=user.get_dogs(), user_book_history=user.get_visits())
@accounts.route('/users/<username>/my_dogs', methods=['GET'])
def user_dogs_panel(username):
# Retrieve user data
user = db.session.execute(db.select(User).filter_by(username=username)).scalar_one()
return render_template('users/my_dogs/base.html', user=user, user_dogs=user.get_dogs())
@accounts.route('/users/<username>', methods=['POST'])
def user(username):
user = db.session.execute(db.select(User).filter_by(username=username)).scalar_one()
return jsonify(user.to_dict()), 201
@accounts.route('/users/<username>/pets', methods=['POST'])
def user_pets(username):
@accounts.route('/users/<username>/dogs', methods=['POST'])
def user_dogs(username):
user = db.session.execute(db.select(User).filter_by(username=username)).scalar_one()
dogs = list()
for dog in user.dogs:
dogs.append(dog.name)
return jsonify(dogs), 201
return jsonify(user.get_dogs()), 201

View File

@ -56,13 +56,17 @@ def add_test_dogs(db):
lassey = Dog(name='Lassey',
breed_size=BreedSize.LARGE,
birth_date=datetime(2010, 10, 4),
owner_id=man_man.id)
rufus = Dog(name='Rufus',
breed_size=BreedSize.SMALL,
breed_size=BreedSize.MEDIUM,
birth_date=datetime(2020, 4, 20),
owner_id=man_man.id)
air_bud = Dog(name='Air Bud',
breed_size=BreedSize.LARGE,
birth_date=datetime(2018, 7, 16),
owner_id=real_person.id)
db.session.add(lassey)

View File

@ -25,9 +25,5 @@ pkgs.mkShell {
sqlite-utils # utilities
]))
];
shellHook = ''
export FLASK_APP=tests.testing_app
'';
}

30
tests/test_dog_model.py Normal file
View File

@ -0,0 +1,30 @@
# Import libraries
import pytest
# Import db
from accounts.models import db
# Import db models
from accounts.models.dog import Dog
def test_user_model(app, db):
lassey = db.session.execute(db.select(Dog).where(Dog.name == 'Lassey')).scalar_one()
rufus = db.session.execute(db.select(Dog).where(Dog.name == 'Rufus')).scalar_one()
air_bud = db.session.execute(db.select(Dog).where(Dog.name == 'Air Bud')).scalar_one()
# Assert something
assert lassey.id == 1
assert lassey.name == 'Lassey'
assert lassey.breed_size.value == 'Large'
assert lassey.owner_id == 1
assert rufus.id == 2
assert rufus.name == 'Rufus'
assert rufus.breed_size.value == 'Medium'
assert rufus.owner_id == 1
assert air_bud.id == 3
assert air_bud.name == 'Air Bud'
assert air_bud.breed_size.value == 'Large'
assert air_bud.owner_id == 2

View File

@ -4,18 +4,17 @@ import pytest
# Import db
from accounts.models import db
# Import db models
from accounts.models.dog import Dog
from accounts.models.dog.breed_size import BreedSize
# Import user db model
from accounts.models.user import User
from accounts.models.visit.visit_type import VisitType
def test_user_model(app, db):
man_man = db.session.execute(db.select(User).where(User.name == 'Man Man')).scalar_one()
man_man_visits = man_man.get_visits()
man_man_dogs = man_man.get_dogs()
real_person = db.session.execute(db.select(User).where(User.name == 'Real Person')).scalar_one()
real_person_visits = real_person.get_visits()
real_person_dogs = real_person.get_dogs()
# Assert something
assert man_man.id == 1
@ -25,6 +24,7 @@ def test_user_model(app, db):
assert man_man.address == '123 Home Ln. City, AA 11223'
assert man_man.password == 'manword'
assert len(man_man_visits) == 2
assert len(man_man_dogs) == 2
assert real_person.id == 2
assert real_person.name == 'Real Person'
@ -33,4 +33,5 @@ def test_user_model(app, db):
assert real_person.address == '113 Park St. City, AA 13433'
assert real_person.password == 'realpassword'
assert len(real_person_visits) == 1
assert len(real_person_dogs) == 1