Web based Online Blood Donation System

This web based online blood donation system is created with following assumptions-

  • Display city wise availability of blood units in blood banks.
  • When a donor donates blood in blood bank of a specific city, her details are added in the system.
  • When a recipient is issues blood units from blood bank of a specific city, her details are saved in the system.
  • Users are able to check availability of units of all blood groups in a specific city.
  • When a donation details are added in the system, the units of that blood group currently available are updated by the count of units donated.
  • When recipient details are added in the system, the units of that blood group currently available are reduced by the count of units issued to the recipient.

This basic Web based Online Blood Donation System is developed using Flask, SQLalchemy and PostgreSQL as Database. The database is made up of these four tables-

Bloodbank

Blood Bank Table

Donors

Donors Table

Recipients

Recipients Table

City

City Table

Home page- Web based Online Blood Donation System

The home page displays city wise blood units available in tabular form. A user can check against city name and under his required blood group, if he can request for his required blood units.

Home page- Web based Online Blood Donation System

application.py

from flask import Flask, render_template, jsonify, request,redirect,flash
from models import *
folder_name="static"

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://postgres:password@localhost:5432/bloodbank"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.secret_key = b'hkahs3720/'
db.init_app(app)


@app.route("/")
def index():
    bldData=bloodbank.query.order_by("cityid").all()
    return render_template("index.html", bldData=bldData)    
    
    
@app.route("/adddonor")
def adddonor():
    cityData=city.query.all()
    return render_template("addDonor.html",cityData=cityData) 

@app.route("/details/<string:bg>/<int:cid>")
def details(bg,cid):
    donorlist=donors.query.filter_by(City=cid,dgroup=bg).all()
    recplist=recipient.query.filter_by(rcity=cid,bldgroup=bg).all()
    return render_template("details.html",donorlist=donorlist, recplist=recplist)     

@app.route("/saveDonor", methods=["POST"])
def saveDonor():
    nam = request.form.get("name")
    cty = request.form.get("city")
    uni = request.form.get("units")
    bgp = request.form.get("bldgrp")
    dod = request.form.get("dod")
    donor = donors(dname=nam,dgroup=bgp,ddate=dod,City=cty,dunits=uni)
    db.session.add(donor)
    bldbank = bloodbank.query.filter_by(cityid=cty).first()
    if bgp=="AP":
        newUnits= bldbank.ap
        bldbank.ap = int(newUnits)+int(uni)
    if bgp=="AN":
        newUnits= bldbank.an
        bldbank.an = int(newUnits)+int(uni) 
    if bgp=="BP":
        newUnits= bldbank.bp
        bldbank.bp = int(newUnits)+int(uni)        
    if bgp=="BN":
        newUnits= bldbank.bn
        bldbank.bn = int(newUnits)+int(uni)        
    if bgp=="ABP":
        newUnits= bldbank.abp
        bldbank.abp = int(newUnits)+int(uni)
    if bgp=="ABN":
        newUnits= bldbank.abn
        bldbank.abn = int(newUnits)+int(uni) 
    if bgp=="OP":
        newUnits= bldbank.op
        bldbank.op = int(newUnits)+int(uni)        
    if bgp=="ONEG":
        newUnits= bldbank.oneg
        bldbank.oneg = int(newUnits)+int(uni)         
    db.session.commit()
    
    bldData=bloodbank.query.all()
    return redirect("/")  
    
    
@app.route("/addrecp")
def addrecp():
    cityData=city.query.all()
    return render_template("addRecipient.html", cityData=cityData)    

@app.route("/saveRecp", methods=["POST"])
def saveRecp():
    nam = request.form.get("name")
    phn = request.form.get("phone")
    cty = request.form.get("city")
    uni = int(request.form.get("units"))
    bgp = request.form.get("bldgrp")
    dor = request.form.get("dor")
    dod = request.form.get("dod")
    recp = recipient(rname=nam,bldgroup=bgp,rphone=phn,reqdate=dor, deldate=dod,rcity=cty,runits=uni)
    db.session.add(recp)
    bldbank = bloodbank.query.filter_by(cityid=cty).first()
    
    if bgp=="AP":
        newUnits= bldbank.ap
        if (newUnits>uni):
            bldbank.ap = int(newUnits)-int(uni)
        
    if bgp=="AN":
        newUnits= bldbank.an
        if (newUnits>uni):
            bldbank.an = int(newUnits)-int(uni)
        
    if bgp=="BP":
        newUnits= bldbank.bp
        if (newUnits>uni):
            bldbank.bp = int(newUnits)-int(uni)
            
    if bgp=="BN":
        newUnits= bldbank.bn
        if (newUnits>uni):
            bldbank.bn = int(newUnits)-int(uni)
              
    if bgp=="ABP":
        newUnits= bldbank.abp
        if (newUnits>uni):
            bldbank.abp = int(newUnits)-int(uni)
       
    if bgp=="ABN":
        newUnits= bldbank.abn
        if (newUnits>uni):
            bldbank.abn = int(newUnits)-int(uni)
        
    if bgp=="OP":
        newUnits= bldbank.op
        if (newUnits>uni):
            bldbank.op = int(newUnits)-int(uni)
             
    if bgp=="ONEG":
        newUnits= bldbank.oneg
        if (newUnits>uni):
            bldbank.oneg = int(newUnits)-int(uni)
               
    db.session.commit()
    bldData=bloodbank.query.all()
    return redirect("/")  

models.py

import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
   
class donors(db.Model):
    __tablename__="donors"
    did = db.Column(db.Integer, primary_key=True)
    dname =db.Column(db.String, nullable=False)
    dgroup =db.Column(db.String, nullable=False)
    ddate =db.Column(db.Date, nullable=True)
    dunits = db.Column(db.Integer, nullable=True)
    City = db.Column(db.String, nullable=True)

class recipient(db.Model):
    __tablename__="recipient"
    rid = db.Column(db.Integer, primary_key=True)
    rname =db.Column(db.String, nullable=False)
    rphone=db.Column(db.String, nullable=False)
    bldgroup =db.Column(db.String, nullable=False)
    reqdate =db.Column(db.Date, nullable=True)
    deldate =db.Column(db.Date, nullable=True)
    runits = db.Column(db.Integer, nullable=True)
    rcity = db.Column(db.String, nullable=True)    

class city(db.Model):
    __tablename__="city"
    cityid = db.Column(db.Integer, primary_key=True)
    cityName =db.Column(db.String, nullable=False)
    city = db.relationship("bloodbank", backref="city", lazy=True)
    
class bloodbank(db.Model):
    __tablename__="bloodbank"
    bid = db.Column(db.Integer, primary_key=True)
    ap = db.Column(db.Integer, nullable=False)
    an =db.Column(db.Integer, nullable=False)
    bp =db.Column(db.Integer, nullable=False)
    bn =db.Column(db.Integer, nullable=True)
    abn = db.Column(db.Integer, nullable=True)
    abp = db.Column(db.Integer, nullable=True)
    op = db.Column(db.Integer, nullable=True)
    oneg = db.Column(db.Integer, nullable=True)
    cityid = db.Column(db.Integer, db.ForeignKey("city.cityid"), nullable=False)
   

layout_reg.html

<!DOCTYPE html>
<html>
    <head>
        <title>{% block title %}{% endblock %}</title>
        
    </head>
    <body>
	
		{% block body %}
        {% endblock %}
	
    </body>
</html>

index.html

<style>
body{}
.btn{
height:50px;
width:180px;
background-color:red;
font-family:verdana;
font-size:20px;
color:white;
border-style:inset;
float:left;
margin:10px;
padding-top:15px;
padding-bottom:5px;
text-align:center;
}
.frm{height:70%;width:70%;border-style:ridge;margin:10%; padding-top:20px; 
	padding-left:10px; background-color:white;font-family:verdana;}
.lbl{margin:10px; width:100px; height:30px;float:left;text-align:right; font-size:16px;}
.ctrl{margin:10px; width:250px; height:30px; float:left;}
table, th, td {
  border: 1px solid gray;
  font-family:verdana;
  font-size:16px;
  padding:10px;
}

td
{
width:40px;
text-align:center;
}
</style>
{% extends "layout_reg.html" %}

{% block title %}
    Blood Donation Portal
{% endblock %}

{% block body %}
	
	</div>
    <div style="margin-left:10%; margin-top:10%;border:1px; float:left; width:45%">
	<a href={{url_for('adddonor')}}><div class="btn">Donor</div></a>
	<a href={{url_for('addrecp')}}><div class="btn">Recipient</div></a>
	</div>
	<br>
	<div style="padding-left:40px;padding-top:40px;font-family:verdana;" >
	<table style="border:1px solid black;">
	<tr>
	<td></td>
	<td>A+</td>
	<td>A-</td>
	<td>B+</td>
	<td>B-</td>
	<td>AB+</td>
	<td>AB-</td>
	<td>O+</td>
	<td>O-</td>
	</tr>
	{% for b in bldData %}
	<tr>
	<td>{{b.city.cityName}}</td> 
	<td><a href="{{url_for('details',bg='AP', cid=b.cityid)}}">{{b.ap}}</a></td>
	<td><a href="{{url_for('details',bg='AN', cid=b.cityid)}}">{{b.an}}</a></td>
	<td><a href="{{url_for('details',bg='BP', cid=b.cityid)}}">{{b.bp}}</a></td>
	<td><a href="{{url_for('details',bg='BN', cid=b.cityid)}}">{{b.bn}}</a></td>
	<td><a href="{{url_for('details',bg='ABP', cid=b.cityid)}}">{{b.abp}}</a></td>
	<td><a href="{{url_for('details',bg='ABN', cid=b.cityid)}}">{{b.abn}}</a></td>
	<td><a href="{{url_for('details',bg='OP', cid=b.cityid)}}">{{b.op}}</a></td>
	<td><a href="{{url_for('details',bg='ONEG', cid=b.cityid)}}">{{b.oneg}}</a></td>
	</tr>
	{% endfor %}
	</table>
	
	</div>
{% endblock %}

The home page also contains two buttons “Donor” and “Recipient”. The blood group units presented in the city-wise are links. on clicking any number a page will open a page with details of donors and recipient of the specific blood group in specific city.

Details of units
<html>
<style>
table{
  font-family:verdana;
  font-size:18px;
}
th 
{
border-color:gray;
border-bottom-style:solid;
padding-top:10px;
padding-bottom:10px;
text-align:center;
}
td 
{
border-color:gray;
border-bottom-style:solid;
padding-top:10px;
padding-bottom:10px;
text-align:center;
}

</style>
<body>
<div style="margin-left:5%; margin-top:2%;border:1px; float:left; width:45%">
{% block body %}
<table>
	<tr>
	<td colspan="4">Donors Details</td>
	</tr>
	<tr>
	<td>Donor ID</td>
	<td>Donor Name</td>
	<td>Date of Donation</td>
	<td>Units Donated</td>
	
	</tr>
	{% for d in donorlist %}
	<tr>
	<td>{{d.did}}</td> 
	<td>{{d.dname}}</td>
	<td>{{d.ddate}}</td>
	<td>{{d.dunits}}</td>
	
	</tr>
	{% endfor %}
	</table>

</div>
<div style="margin-top:2%;border:1px; float:left; width:45%">

<table >
	<tr>
	<td colspan="6">Recipients Details</td>
	</tr>
	<tr>
	<td>Recipient ID</td>
	<td>Recipient Name</td>
	<td>Recipient Phone</td>
	<td>Date of Request</td>
	<td>Date of Delivery</td>
	<td>Units given</td>
	
	</tr>
	{% for r in recplist %}
	<tr>
	<td>{{r.rid}}</td> 
	<td>{{r.rname}}</td>
	<td>{{r.rname}}</td>
	<td>{{r.deldate}}</td>
	<td>{{r.reqdate}}</td>
	<td>{{r.runits}}</td>
	
	</tr>
	{% endfor %}
	</table>
{% endblock %}	
</div>
</body>
</html>

Donor Page

On clicking “Donor” button home page, donor page opens. The donor page accepts details like donors name and city, number of units donated, date of donation and the city where he made donation. This entry adds the number of units in the home page’s table against the city and blood group.

Donor Page
<html>
<style>
.btn{
height:30px;
width:100px;
background-color:red;
font-family:verdana;
font-size:16px;
color:white;
border-style:solid;
align:center;
padding:5px;
text-align:center;
}
.frm{height:70%;width:70%;border-style:ridge;margin:10%; padding-top:20px; 
	padding-left:10px; background-color:white;font-family:verdana;}
.lbl{margin:10px; width:100px; height:30px;float:left;text-align:right; font-size:16px;}
.ctrl{margin:10px; width:250px; height:30px; float:left;}
table, th, td {
  border: 1px solid black;
}
</style>
<body>
{% block body %}
<form action="{{ url_for('saveDonor') }}" method="post">
	<div style="background-color:lightblue; border:1px; margin-left:30%;float:left; width:45% ;height:100%">
	<div class="frm">
	<div class="lbl">Name</div><input class="ctrl" type="text" name="name"><br>
	<div class="lbl">City</div><BR>
	<select class="ctrl" name="city">
	{% for c in cityData %}
        <option value={{c.cityid}}>{{c.cityName}}</option>
	{% endfor %}	
	</select>
	<br>
	<div class="lbl">Blood Group</div><BR>
	<select class="ctrl" name="bldgrp">
        <option value="OP">O+</option>
		<option value="ONEG">O-</option>
		<option value="AP">A+</option>
		<option value="AN">A-</option>
		<option value="BP">B+</option>
		<option value="BN">B-</option>
		<option value="ABP">AB+</option>
		<option value="ABN">AB-</option>
	</select>
	<br>
	<div class="lbl">Date of Donation</div><input class="ctrl" type="date" name="dod"><br>
	<div class="lbl">Units</div><input class="ctrl" type="number" name="units"><br><br>
	<div class="lbl"><button class="btn">Save</button></div>
	</div>
	</div>
	</form>
{% endblock %}	

</body>
</html>

Recipient Page

When you click “Recipient” button in home page of the Web based Online Blood Donation System, recipient page opens. The Recipient page accepts details like Recipient’s name, phone and city. Blood group, number of units given to the percipient, date of request and delivery are also accepted here. This entry reduces the count of units entered here from the home page’s table against the city and blood group.

Recipient Page
<html>
<style>
.btn{
height:30px;
width:100px;
background-color:red;
font-family:verdana;
font-size:16px;
color:white;
border-style:solid;
align:center;
padding:5px;
text-align:center;
}
.frm{height:70%;width:70%;border-style:ridge;margin:10%; padding-top:20px; 
	padding-left:10px; background-color:white;font-family:verdana;}
.lbl{margin:10px; width:100px; height:30px;float:left;text-align:right; font-size:16px;}
.ctrl{margin:10px; width:250px; height:30px; float:left;}
table, th, td {
  border: 1px solid black;
}
</style>
<body>
{% block body %}
<form action="{{ url_for('saveRecp') }}" method="post">
	<div style="background-color:lightblue; border:1px; margin-left:30%;float:left; width:45% ;height:100%">
	<div class="frm">
	<div class="lbl">Recipient Name</div><input class="ctrl" type="text" name="name"><br>
	<div class="lbl">Phone</div><input class="ctrl" type="text" name="phone"><br>
	<div class="lbl">City</div><BR>
	<select class="ctrl" name="city">
	{% for c in cityData %}
        <option value={{c.cityid}}>{{c.cityName}}</option>
	{% endfor %}	
	</select>
	<br>
	<div class="lbl">Blood Group</div><BR>
	<select class="ctrl" name="bldgrp">
        <option value="OP">O+</option>
		<option value="ONEG">O-</option>
		<option value="AP">A+</option>
		<option value="AN">A-</option>
		<option value="BP">B+</option>
		<option value="BN">B-</option>
		<option value="ABP">AB+</option>
		<option value="ABN">AB-</option>
	</select>
	<br>
	<div class="lbl">Date of Request</div><input class="ctrl" type="date" name="dor"><br>
	<div class="lbl">Date of Delivery</div><input class="ctrl" type="date" name="dod"><br>
	<div class="lbl">Units</div><input class="ctrl" type="number" name="units"><br><br>
	<div class="lbl"><button class="btn">Save</button></div>
	</div>
	</div>
	</form>
{% endblock %}	
<p>{{errmsg}}</p>
</body>
</html>

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *