diff --git a/app.py b/app.py index 2aa0c82..d2c559e 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,7 @@ -from flask import Flask, render_template, request, redirect, url_for, flash +from flask import Flask, render_template, request, redirect, url_for, flash, send_from_directory from flask_pymongo import PyMongo from werkzeug.utils import secure_filename +from datetime import datetime import os app = Flask(__name__) @@ -10,51 +11,68 @@ app.config['SECRET_KEY'] = 'supersecretkey' app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'gif'} mongo = PyMongo(app) +#form = PinForm() from flask_wtf import FlaskForm -from wtforms import StringField, FileField, SubmitField +from wtforms import StringField, FileField, SubmitField, DateTimeField, SelectField from wtforms.validators import DataRequired class PinForm(FlaskForm): description = StringField('Description', validators=[DataRequired()]) - photo = FileField('Photo', validators=[DataRequired()]) - submit = SubmitField('Add Pin') + photo = FileField('Evidencia fotogénica', validators=[DataRequired()]) + timedate = DateTimeField(default=datetime.now()) + typeofpin = SelectField('Tipo de cosa', choices=['bache', 'coladera', 'obra sin terminar', 'escombro', 'robo-asalto']) + submit = SubmitField('Agregar') + def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS'] -@app.route('/') +@app.route('/', methods=['GET', 'POST']) def index(): - pins = mongo.db.pins.find() - return render_template('index.html', pins=pins) - -@app.route('/add_pin', methods=['GET', 'POST']) -def add_pin(): - form = PinForm() - if form.validate_on_submit(): - description = form.description.data - photo = form.photo.data + if request.method == 'GET': + form = PinForm() + pins = mongo.db.pins.find() + return render_template('index.html', pins=pins, form=form) + else: +#@app.route('/add_pin') +#def add_pin(): + form = request.form +# if form.validate_on_submit(): + #description = form.description.data + try: + photo = request.files["photo"] + except Exception as e: + print(e) if photo and allowed_file(photo.filename): - try: - filename = secure_filename(photo.filename) - filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) - photo.save(filepath) - - pin = { - 'description': description, - 'photo': filepath, - 'lat': request.form['lat'], - 'lng': request.form['lng'] - } - mongo.db.pins.insert_one(pin) - return redirect(url_for('index')) - except Exception as e: - flash(f'An error occurred: {e}') - return redirect(url_for('add_pin')) + #try: + filename = secure_filename(photo.filename) + filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) + photo.save(filepath) + + pin = { + #'description': description, + 'time': datetime.now(), + 'photo': filepath, + 'lat': request.form['lat'], + 'lng': request.form['lng'], + 'typeofpin': request.form['typeofpin'], + } + mongo.db.pins.insert_one(pin) + flash('¡Gracias por tu aportación!') + return redirect(url_for('index')) else: - flash('Invalid file type. Only images are allowed.') - return render_template('add_pin.html', form=form) - + return allowed_file(photo.filename), 404 + #render_template('index.html', pins=pins, form=form) + #except Exception as e: + # flash(f'An error occurred: {e}') + #return redirect(url_for('add_pin')) + #else: + # flash('Invalid file type. Only images are allowed.') + #return render_template('index.html', form=form) +@app.route('/uploads/') +def download_file(name): + return send_from_directory(app.config["UPLOAD_FOLDER"], name) if __name__ == '__main__': app.run(debug=True) diff --git a/app2.py b/app2.py deleted file mode 100644 index d2cd3e9..0000000 --- a/app2.py +++ /dev/null @@ -1,67 +0,0 @@ -from flask import Flask, render_template, request,make_response,jsonify, redirect, url_for -from pymongo import MongoClient -from flask_wtf import FlaskForm, CSRFProtect -from wtforms import StringField, FileField, DateTimeField -from werkzeug.utils import secure_filename - -class BacheForm(FlaskForm): - name = StringField('name') - photo = FileField() - -class mongo_connection: - conn = None - - def connect(self): - mongoclient = MongoClient("mongodb://localhost:27017/") - mongo_db = mongoclient["mapper"] - self.conn = mongo_db["baches"] - - def query(self, sql): - cursor = self.conn.find(sql) - return cursor - -db = mongo_connection() -db.connect() - -app = Flask(__name__) -csrf = CSRFProtect(app) - -#mongodb_client = MongoClient("mongodb://localhost:27017/") -#db = client['bachemap'] -#collection = db['bachecitos'] - - - - -@app.route("/", methods=['GET','POST']) -def index(): - if request.method == 'GET': - return render_template("map.html") - - if request.method == 'POST': - form=BacheForm() - if form.validate_on_submit(): - post_data = { - 'author': form.author.data, - 'img': form.img.data, - 'latitude': form.latitude.data, - 'longitude': form.longitide.data, - 'message': form.message.data - } - collection.insert_one(post_data) - flash('¡Gracias por tu aporte! A la ONU le gusta esto :3') - return redirect(url_for('map')) - return render_template('map.html', form=form) - - - -if __name__ == '__main__': - app.run(host="localhost", port=5000, debug=True) - - - - - - -# models schema: -#{'latitude':'', 'longitude':'', 'severity':''} \ No newline at end of file diff --git a/reference.py b/reference.py new file mode 100644 index 0000000..95a6359 --- /dev/null +++ b/reference.py @@ -0,0 +1,94 @@ +from flask import Flask, request, redirect, url_for, render_template, session +from flask_sqlalchemy import SQLAlchemy +from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user +from uuid import uuid4 +import click + +app = Flask(__name__) +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db' +app.config['SECRET_KEY'] = 'your_secret_key' +db = SQLAlchemy(app) +login_manager = LoginManager(app) + +class User(UserMixin, db.Model): + id = db.Column(db.Integer, primary_key=True) + username = db.Column(db.String(150), unique=True, nullable=False) + referral_code = db.Column(db.String(36), unique=True, nullable=False) + invited_by_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True) + is_admin = db.Column(db.Boolean, default=False) + invitees = db.relationship('User', backref=db.backref('inviter', remote_side=[id])) + +@login_manager.user_loader +def load_user(user_id): + return User.query.get(int(user_id)) + +@app.route('/register/', methods=['GET', 'POST']) +def register(referral_code): + inviter = User.query.filter_by(referral_code=referral_code).first_or_404() + if request.method == 'POST': + username = request.form['username'] + new_user = User(username=username, referral_code=str(uuid4()), invited_by_id=inviter.id) + db.session.add(new_user) + db.session.commit() + return redirect(url_for('login')) + return render_template('register.html') + +@app.route('/login', methods=['GET', 'POST']) +def login(): + if request.method == 'POST': + username = request.form['username'] + user = User.query.filter_by(username=username).first() + if user: + login_user(user) + return redirect(url_for('home')) + return render_template('login.html') + +@app.route('/logout') +@login_required +def logout(): + logout_user() + return redirect(url_for('login')) + +@app.route('/remove_user/') +@login_required +def remove_user(user_id): + if not current_user.is_admin: + return redirect(url_for('home')) + user_to_remove = User.query.get_or_404(user_id) + invitees = User.query.filter_by(invited_by_id=user_to_remove.id).all() + for invitee in invitees: + db.session.delete(invitee) + db.session.delete(user_to_remove) + db.session.commit() + return redirect(url_for('admin_dashboard')) + +@app.cli.command('remove_user') +@click.argument('username') +def remove_user_cli(username): + user_to_remove = User.query.filter_by(username=username).first() + if user_to_remove: + invitees = User.query.filter_by(invited_by_id=user_to_remove.id).all() + for invitee in invitees: + db.session.delete(invitee) + db.session.delete(user_to_remove) + db.session.commit() + print(f'User {username} and their invitees have been removed.') + else: + print(f'User {username} not found.') + +@app.route('/home') +@login_required +def home(): + return "Home page" + +@app.route('/admin_dashboard') +@login_required +def admin_dashboard(): + if not current_user.is_admin: + return redirect(url_for('home')) + users = User.query.all() + return render_template('admin_dashboard.html', users=users) + +if __name__ == '__main__': + db.create_all() + app.run(debug=True) diff --git a/static/js/script.js b/static/js/script.js deleted file mode 100644 index e69de29..0000000 diff --git a/static/style.css b/static/style.css index 3c7c2a2..705bcda 100644 --- a/static/style.css +++ b/static/style.css @@ -1 +1,5 @@ body {padding: 0;margin: 0;}html, body, #map {height: 100%;width: 100vw;} +div.leaflet-popup-content { + width: 400px; +} + diff --git a/static/styles.css b/static/styles.css index 316c1da..ae7adad 100644 --- a/static/styles.css +++ b/static/styles.css @@ -1,7 +1,8 @@ body { font-family: Arial, sans-serif; margin: 0; - padding: 20px; + padding: 0px; + overflow: hidden; } h1 { @@ -22,6 +23,35 @@ form p { #map { width: 100%; - height: 600px; - margin: 20px 0; + height: 100vh; + margin: 0 0; } + +main { + margin:0; + padding:0; + position: absolute; +} + +div.leaflet-popup-content { + width: 400px; +} + +div.pinner-modal { + position: absolute; + + top: 10vh; + left: 0; + width: 100%; + background-color: #007BFF; + color: white; + padding: 1rem; + text-align: center; + transition: bottom 0.5s ease; + box-shadow: 0px -4px 15px rgba(0, 0, 0, 0.2); + z-index: 999; +} + +div.pinner-modal.active { + bottom: 0; +} \ No newline at end of file diff --git a/templates/add_pin.html b/templates/add_pin.html index afb0db7..268fff1 100644 --- a/templates/add_pin.html +++ b/templates/add_pin.html @@ -1,45 +1,17 @@ -{% extends 'base.html' %} - -{% block content %} -

Add a New Pin

-
+ {{ form.hidden_tag() }} -

+ + {{ form.photo.label }}
{{ form.photo() }} -

-

-
+ {{ form.typeofpin }} + +
-

-

-
+
-

{{ form.submit() }}

- Back to map - -
- - - -{% endblock %} \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index 21aaa4d..363c86b 100644 --- a/templates/base.html +++ b/templates/base.html @@ -19,23 +19,54 @@ -