diff --git a/admin.py b/admin.py new file mode 100644 index 0000000..c2632d8 --- /dev/null +++ b/admin.py @@ -0,0 +1,9 @@ +from flask_admin import Admin +from flask_admin.contrib.sqla import ModelView +from app import app, db +from models import User, SARCall, SARCategory + +admin = Admin(app, name='SAR Admin', template_mode='bootstrap3') +admin.add_view(ModelView(User, db.session)) +admin.add_view(ModelView(SARCall, db.session)) +admin.add_view(ModelView(SARCategory, db.session)) diff --git a/app.py b/app.py index 5d20a01..1a0e936 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,21 @@ from flask import Flask +from flask_sqlalchemy import SQLAlchemy +from flask_migrate import Migrate +from flask_login import LoginManager + app = Flask(__name__) +app.config['SECRET_KEY'] = 'secret_key' +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3' +db = SQLAlchemy(app) +migrate = Migrate(app,db) +login_manager = LoginManager(app) +import models +import admin +import login +import sar_calls +import dashboard @app.route('/') def hello_world(): # put application's code here diff --git a/create_db.py b/create_db.py new file mode 100644 index 0000000..a41d77c --- /dev/null +++ b/create_db.py @@ -0,0 +1,11 @@ +from flask import Flask, render_template, redirect, url_for, request, flash +from flask_sqlalchemy import SQLAlchemy + +app = Flask(__name__) +app.config['SECRET_KEY'] = 'secret_key' +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3' +db = SQLAlchemy(app) +from models import User, SARCall, SARCategory + +if __name__ == '__main__': + db.create_all() diff --git a/dashboard.py b/dashboard.py new file mode 100644 index 0000000..eaaf0af --- /dev/null +++ b/dashboard.py @@ -0,0 +1,8 @@ +from app import app +from flask import render_template +from flask_login import login_required, current_user +@app.route('/dashboard') +@login_required +def dashboard(): + return render_template('dashboard.html', name=current_user.full_name) + diff --git a/login.py b/login.py new file mode 100644 index 0000000..33163ab --- /dev/null +++ b/login.py @@ -0,0 +1,39 @@ +from flask import Flask, render_template, redirect, url_for, request, flash +from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user +from app import app, db, login_manager +from models import User +@login_manager.user_loader +def load_user(user_id): + return User.query.get(int(user_id)) + +@app.route('/login', methods=['GET', 'POST']) +def login(): + if request.method == 'POST': + username = request.form.get('username') + password = request.form.get('password') + user = User.query.filter_by(username=username).first() + if user and user.password == password: + login_user(user) + return redirect(url_for('dashboard')) + return render_template('login.html') + +@app.route('/register', methods=['GET', 'POST']) +def register(): + if request.method == 'POST': + username = request.form.get('username') + password = request.form.get('password') + email = request.form.get('email') + full_name = request.form.get('full_name') + phone_number = request.form.get('phone_number') + + new_user = User(username=username, password=password, email=email, full_name=full_name, phone_number=phone_number) + db.session.add(new_user) + db.session.commit() + return redirect(url_for('login')) + return render_template('register.html') + +@app.route('/logout') +@login_required +def logout(): + logout_user() + return redirect(url_for('login')) diff --git a/models.py b/models.py new file mode 100644 index 0000000..961adf3 --- /dev/null +++ b/models.py @@ -0,0 +1,26 @@ +from flask_login import UserMixin +from app import db + +class User(UserMixin, db.Model): + id = db.Column(db.Integer, primary_key=True) + username = db.Column(db.String(150), unique=True, nullable=False) + full_name = db.Column(db.String(300), nullable=False) + email = db.Column(db.String(150), unique=True, nullable=False) + phone_number = db.Column(db.String(50), nullable=True) + password = db.Column(db.String(50)) + + +class SARCall(db.Model): + id = db.Column(db.Integer, primary_key=True) + start_date = db.Column(db.DateTime, nullable=False) + finish_date = db.Column(db.DateTime, nullable=False) + category = db.Column(db.String(150), nullable=False) + latitude = db.Column(db.Float, nullable=False) + longitude = db.Column(db.Float, nullable=False) + search_manager_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) + gpx_data = db.Column(db.Text, nullable=True) # This will store GPX data as a text + + +class SARCategory(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(150), unique=True, nullable=False) \ No newline at end of file diff --git a/sar_calls.py b/sar_calls.py new file mode 100644 index 0000000..104d08a --- /dev/null +++ b/sar_calls.py @@ -0,0 +1,64 @@ +from app import app, db +from flask import request, redirect, flash, render_template, url_for +from flask_login import login_required, current_user +from dateutil import parser +from models import SARCall +@app.route('/create_sar', methods=['GET', 'POST']) +@login_required +def create_sar(): + if request.method == 'POST': + start_date = parser.parse(request.form.get('start_date')) + finish_date = parser.parse(request.form.get('finish_date')) + category = request.form.get('category') + latitude = request.form.get('latitude') + longitude = request.form.get('longitude') + gpx_data = request.form.get('gpx_data') + + new_sar_call = SARCall( + start_date=start_date, + finish_date=finish_date, + category=category, + latitude=latitude, + longitude=longitude, + search_manager_id=current_user.id, + gpx_data=gpx_data + ) + db.session.add(new_sar_call) + db.session.commit() + flash('SAR call created successfully!', 'success') + return redirect(url_for('dashboard')) + return render_template('create_sar.html') + + +@app.route('/list_sar') +@login_required +def list_sar(): + sar_calls = SARCall.query.all() + return render_template('list_sar.html', sar_calls=sar_calls) + + +@app.route('/edit_sar/', methods=['GET', 'POST']) +@login_required +def edit_sar(id): + sar_call = SARCall.query.get(id) + if request.method == 'POST': + sar_call.start_date = request.form.get('start_date') + sar_call.finish_date = request.form.get('finish_date') + sar_call.category = request.form.get('category') + sar_call.latitude = request.form.get('latitude') + sar_call.longitude = request.form.get('longitude') + sar_call.gpx_data = request.form.get('gpx_data') + db.session.commit() + flash('SAR call updated successfully!', 'success') + return redirect(url_for('list_sar')) + return render_template('edit_sar.html', sar_call=sar_call) + +@app.route('/delete_sar/') +@login_required +def delete_sar(id): + sar_call = SARCall.query.get(id) + db.session.delete(sar_call) + db.session.commit() + flash('SAR call deleted successfully!', 'success') + return redirect(url_for('list_sar')) + diff --git a/templates/create_sar.html b/templates/create_sar.html new file mode 100644 index 0000000..f8988c0 --- /dev/null +++ b/templates/create_sar.html @@ -0,0 +1,70 @@ + + + + + + Create SAR Call + + +

Create SAR Call

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+ + +
+ + +Back to Dashboard + + diff --git a/templates/dashboard.html b/templates/dashboard.html new file mode 100644 index 0000000..a05b410 --- /dev/null +++ b/templates/dashboard.html @@ -0,0 +1,11 @@ + + + + Dashboard + + +

Welcome, {{ name }}!

+

This is the dashboard. More features will be added soon!

+ Logout + + diff --git a/templates/edit_sar.html b/templates/edit_sar.html new file mode 100644 index 0000000..9b03c48 --- /dev/null +++ b/templates/edit_sar.html @@ -0,0 +1,39 @@ + + + + + Title + + + + + +
+ + + + \ No newline at end of file diff --git a/templates/list_sar.html b/templates/list_sar.html new file mode 100644 index 0000000..d48a54e --- /dev/null +++ b/templates/list_sar.html @@ -0,0 +1,37 @@ + + + + List of SAR Calls + + +

List of SAR Calls

+ + + + + + + + + + + + + {% for sar in sar_calls %} + + + + + + + + + {% endfor %} + +
Start DateFinish DateCategoryLatitudeLongitudeActions
{{ sar.start_date }}{{ sar.finish_date }}{{ sar.category }}{{ sar.latitude }}{{ sar.longitude }} + Edit | + Delete +
+ Back to Dashboard + + diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..78a9999 --- /dev/null +++ b/templates/login.html @@ -0,0 +1,25 @@ + + + + Login + + +

Login

+
+
+ + +
+
+ + +
+
+ +
+
+
+ Don't have an account? Register +
+ + diff --git a/templates/register.html b/templates/register.html new file mode 100644 index 0000000..395f835 --- /dev/null +++ b/templates/register.html @@ -0,0 +1,37 @@ + + + + Register + + +

Register

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+
+ Already have an account? Login +
+ +