I'm just join the stackoverflow community right now because I trying to complete my first Python-flask application and I got some problem to fetch and compare a single date for single user with sqlalchemy & sqlite.
Here my imports:
from flask import Flask, request, jsonify, render_template, url_for, redirect, flash
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine, MetaData, func
import datetime
Here my table database model:
class BloodDonation(db.Model):
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String, nullable=False)
age = db.Column(db.Integer, nullable=False)
blood_groups = db.Column(db.String, nullable=False)
city = db.Column(db.String, nullable=False)
phone_no = db.Column(db.String, nullable=False)
latest_donation = db.Column(db.Date, nullable=False)
next_donation = db.Column(db.Date)
donation_counter = db.Column(db.Integer, default=1)
This is a easy CRUD application where I should store blood donations inside a database with 3 months threshold donation per user. To do that I use the mobile number as user identifier and I use datetime module, so when an user try to do more than an donation before 90 days the application have to show the message donation forbidden.
Here my code:
@app.route('/blood_donation', methods=['GET','POST'])
def blood_donation():
if request.method == 'GET':
blood_groups = ['A+', 'A-', 'B+', 'B-', 'AB+', 'AB-', 'O+', 'O-']
return render_template('donate.html', blood_groups = blood_groups)
else:
donation_day = datetime.date.today()
next_donation_date = threshold_time(donation_day) # this func return donation date + 90 days
data_fields = ['name','age','blood_groups','city','phone_no']
data_dict = {}
data_dict['latest_donation'] = donation_day
data_dict['next_donation'] = next_donation_date
for field in data_fields:
data_dict[field] = request.form.get(field).lower()
for value in data_dict.values():
if value == "":
return "Please enter all the details."
counter = db.session.query(BloodDonation).filter(BloodDonation.phone_no == data_dict['phone_no']).count()
next_bd_date = db.session.query(BloodDonation).filter(func.DATE(BloodDonation.latest_donation) >= 0)
if counter >= 0 and next_bd_date and\
db.session.query(BloodDonation).filter(func.DATE(BloodDonation.latest_donation)\
<= data_dict['latest_donation']):
counter += 1
data_dict['donation_counter'] = counter
blood_donation = BloodDonation(**data_dict)
db.session.add(blood_donation)
db.session.commit()
return redirect(url_for('home'))
return "Donation forbidden!!!"
I think the problem is in the if statement to compare dates and mobile number that I use as donation counter and user identifier since the application continue update the database without return donation forbidden.
Here the wrong code:
if counter >= 0 and next_bd_date and \
db.session.query(BloodDonation).filter(func.DATE(BloodDonation.latest_donation) <=
data_dict['latest_donation']):
I hope somebody can help me since I am not a pro but a beginner
Thanks in advance!