Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>
Calculate delivery and purchase end times using configuration And also display the current cycle duration
src/blueprints/cycles.py | 9 +++++++-- src/static/stylesheet.css | 17 +++++++++++++++++ src/templates/cycles.html | 14 ++++++++++++++ src/types/configuration.py | 13 +++++++++++++
diff --git a/src/blueprints/cycles.py b/src/blueprints/cycles.py index 0bda01eff46ef11f9516551112598db57c0fbb2d..cc37957a4748df9281aa92a69fe1e3d1624a84ca 100644 --- a/src/blueprints/cycles.py +++ b/src/blueprints/cycles.py @@ -1,5 +1,6 @@ from flask import Blueprint, jsonify, url_for, request, render_template, redirect from flask_login import current_user +from datetime import datetime as dt import pdb cycles = Blueprint('cycles', __name__, url_prefix = '/cycles') @@ -47,7 +48,7 @@ return "Not found", 404 def create(params): start_at = params.get('start_at') - end_at = params.get('end_at') + configuration = Configuration.query.first() cycle = Cycle.query.filter_by(status = CycleStatus.published.value).first() @@ -55,13 +56,17 @@ # TODO: Better handling an ongoing cycle when create another one if cycle: return "there's an ongoing cycle" + end_at = (dt.strptime(params.get('start_at'), '%Y-%m-%d') + configuration.purchase_time_delta()) + + delivery_end_at = (dt.strptime(params.get('delivery_start_at'), '%Y-%m-%d') + configuration.delivery_time_delta()) + cycle = Cycle( name = params.get('name'), description = params.get('description'), start_at = start_at, end_at = end_at, delivery_start_at = params.get('delivery_start_at'), - delivery_end_at = params.get('delivery_end_at'), + delivery_end_at = delivery_end_at, status = params.get('status'), price_margin = params.get('price_margin') ) diff --git a/src/static/stylesheet.css b/src/static/stylesheet.css index 80b1602b50195bffbeab0fd7ff9a29459d60c05e..bc5d7b13c72a26434b099238b8f56cf680aab760 100644 --- a/src/static/stylesheet.css +++ b/src/static/stylesheet.css @@ -225,6 +225,16 @@ .m-5 { margin: 5px; } +.py-5 { + padding-left: 5px; + padding-right: 5px; +} + +.px-5 { + padding-top: 5px; + padding-bottom: 5px; +} + .current-cycle { background-color: #eee; } @@ -277,3 +287,10 @@ .f-right, float-right { float: right; } +.text-right { + text-align: right; +} + +.text-left { + text-align: left; +} diff --git a/src/templates/cycles.html b/src/templates/cycles.html index ca7ab39098147a2a4138f0e4e99191621802888b..b386b41a859ba8675baed7517967f609677b9766 100644 --- a/src/templates/cycles.html +++ b/src/templates/cycles.html @@ -2,6 +2,20 @@ {% extends "layout.html" %} {% block body %} <section class="cycles"> <h1>Ciclo em aberto</h1> + <section> + <div class="{{ "box" if current_cycle.status.value == 'published' }}"> + <strong>Compras:</strong> {{ current_cycle.start_at.strftime("%A, %d/%m") }} até {{ current_cycle.end_at.strftime("%A, %d/%m") }} + {% if current_cycle.status.value == 'published' %} + <strong class="pl-5 f-right"> ACONTECENDO </strong> + {% endif %} + </div> + <div class="{{ "box" if current_cycle.status.value == 'delivery' }}"> + <strong>Entrega:</strong> {{ current_cycle.delivery_start_at.strftime("%d/%m/%Y ") }} até {{ current_cycle.delivery_end_at.strftime("%d/%m/%Y") }} + {% if current_cycle.status.value == 'delivery' %} + <strong class="pl-5 f-right"> ACONTECENDO </strong> + {% endif %} + </div> + </section> <div class="cycle"> <h2>{{ current_cycle.title }}</h2> <p class="white-space-pre">{{ current_cycle.description }}</p> diff --git a/src/types/configuration.py b/src/types/configuration.py index e345e3ea97327b011c31314f8eb57c56f3d2905e..0f945dc9f60e9a33c5414dd3fd4d2472e6b40747 100644 --- a/src/types/configuration.py +++ b/src/types/configuration.py @@ -1,5 +1,6 @@ from . import db from datetime import datetime as dt +from datetime import timedelta class Configuration(db.Model): __tablename__ = "configurations" @@ -16,3 +17,15 @@ self.purchase_duration = purchase_duration self.purchase_duration_type = purchase_duration_type self.delivery_duration = delivery_duration self.delivery_duration_type = delivery_duration_type + + def purchase_time_delta(self): + d = dict() + d[self.purchase_duration_type] = self.purchase_duration + + return timedelta(**d) + + def delivery_time_delta(self): + d = dict() + d[self.delivery_duration_type] = self.delivery_duration + + return timedelta(**d)