diff --git a/app/__init__.py b/app/__init__.py index 864d7e4..b7b7140 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -31,5 +31,7 @@ def create_app(config_class=Config): app.register_blueprint(main_bp) from app.users import bp as users_bp app.register_blueprint(users_bp, url_prefix="/users") + from app.pets import bp as pets_bp + app.register_blueprint(pets_bp, url_prefix="/pets") return app \ No newline at end of file diff --git a/app/pets/__init__.py b/app/pets/__init__.py new file mode 100644 index 0000000..84a91a9 --- /dev/null +++ b/app/pets/__init__.py @@ -0,0 +1,4 @@ +from flask import Blueprint + +bp = Blueprint('pets', __name__) +from app.pets import routes \ No newline at end of file diff --git a/app/pets/routes.py b/app/pets/routes.py new file mode 100644 index 0000000..95e479f --- /dev/null +++ b/app/pets/routes.py @@ -0,0 +1,7 @@ +from flask import request, render_template +from app.pets import bp +from app.services.pet_service import PetService + +@bp.route('/') +def index(): + return render_template("pets/index.html", options=PetService.get_options(request)) \ No newline at end of file diff --git a/app/services/pet_service.py b/app/services/pet_service.py new file mode 100644 index 0000000..463b067 --- /dev/null +++ b/app/services/pet_service.py @@ -0,0 +1,93 @@ +from flask import Request +from app.extensions import db +from app.models.pet_kind import PetKind + +class PetService: + + @staticmethod + def get_options(request: Request): + type = request.args.get('type') + sex = request.args.get('sex') + age_from = request.args.get('age-from') + options = dict() + options["type"] = PetService.get_pets_kind_options(type) + options["sex"] = PetService.get_sex_options(sex) + options["age_from"] = PetService.get_age_from_options(age_from) + return options + + @staticmethod + def get_pets_kind_options(selected_kind=None): + pet_kinds = db.session.execute(db.select(PetKind)).scalars() + options = [] + options.append({ + "value": "", + "text": "Type", + "selected": selected_kind == None + }) + options.append({ + "value": "0", + "text": "Any", + "selected": selected_kind == "0" + }) + for pet_kind in pet_kinds: + options.append({ + "value": str(pet_kind.id), + "text": pet_kind.name, + "selected": selected_kind == str(pet_kind.id) + }) + PetService.selected_safeguard(selected_kind,options[0],len(options)) + return options + + @staticmethod + def get_age_from_options(selected_year=None): + options = [] + options.append({ + "value": "0", + "text": "0", + "selected": selected_year == "0" or selected_year == None + }) + for i in range(1,7): + options.append({ + "value": str(i), + "text": str(i), + "selected": selected_year == str(i) + }) + options.append({ + "value": "7", + "text": "7+", + "selected": selected_year == "7" + }) + PetService.selected_safeguard(selected_year,options[0],len(options)) + return options + + @staticmethod + def get_sex_options(selected_sex=None): + options = [ + { + "value": "", + "text": "Sex", + "selected": selected_sex == None + }, + { + "value": "0", + "text": "Any", + "selected": selected_sex == "0" + }, + { + "value": "1", + "text": "Female", + "selected": selected_sex == "1" + }, + { + "value": "2", + "text": "Male", + "selected": selected_sex == "2" + }, + ] + PetService.selected_safeguard(selected_sex,options[0],len(options)) + return options + + @staticmethod + def selected_safeguard(selected_arg, select_default, options_length): + if selected_arg is not isinstance(selected_arg, int) or selected_arg < 0 or selected_arg > len(options_length): + select_default["selected"] = True \ No newline at end of file diff --git a/app/static/js/pets-filter.js b/app/static/js/pets-filter.js new file mode 100644 index 0000000..aab0706 --- /dev/null +++ b/app/static/js/pets-filter.js @@ -0,0 +1,47 @@ +(() => { + 'use strict' + const fromYearsFilter = document.querySelector('#from-years-filter'); + const toYearsFilter = document.querySelector('#to-years-filter'); + const toTextSpan = document.querySelector('#to-text'); + fromYearsFilter.addEventListener('change', updateToYearsFilter); + fromYearsFilter.dispatchEvent(new Event('change')); + updateToYearsFilterWithQueryParams(); + + function updateToYearsFilter(event) { + const startYear = event.target.value; + toYearsFilter.replaceChildren(null); + if(startYear == 7) { + toYearsFilter.removeAttribute('name'); + toYearsFilter.classList.add('d-none'); + toTextSpan.classList.add('d-none'); + } else { + toYearsFilter.setAttribute('name', 'age-to'); + toYearsFilter.classList.remove('d-none'); + toTextSpan.classList.remove('d-none'); + } + + const option = document.createElement('option'); + option.value = "7"; + option.innerText = "7+"; + option.selected = true; + toYearsFilter.appendChild(option); + + for(let i = 6; i > startYear; i--) { + const option = document.createElement('option'); + const value = i.toString(); + option.value = value; + option.innerText = value; + toYearsFilter.appendChild(option); + } + + } + + function updateToYearsFilterWithQueryParams() { + let address = window.location.search; + let parameterList = new URLSearchParams(address) + let ageToParam = parameterList.get('age-to'); + for (const option of toYearsFilter.children) { + if (option.value === ageToParam) option.setAttribute("selected","") + } + } +})() \ No newline at end of file diff --git a/app/templates/pets/index.html b/app/templates/pets/index.html new file mode 100644 index 0000000..4a0cbcd --- /dev/null +++ b/app/templates/pets/index.html @@ -0,0 +1,77 @@ +{% extends "layout/layout.html" %} +{% block title %} Home {% endblock %} +{% block head %} + {{ super() }} +{% endblock %} +{% block content %} +
+
+
+
+
+
+
+ +
+
+ +
+
+
+ From + + to + + yrs old +
+
+
+ +
+
+
+
+
+ +
+
+

This is pets!

+
+
+ +{% endblock %} \ No newline at end of file