diff --git a/app.py b/app.py new file mode 100644 index 0000000..2aa0c82 --- /dev/null +++ b/app.py @@ -0,0 +1,60 @@ +from flask import Flask, render_template, request, redirect, url_for, flash +from flask_pymongo import PyMongo +from werkzeug.utils import secure_filename +import os + +app = Flask(__name__) +app.config["MONGO_URI"] = "mongodb://localhost:27017/mapPinsDB" +app.config['UPLOAD_FOLDER'] = 'uploads' +app.config['SECRET_KEY'] = 'supersecretkey' +app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'gif'} + +mongo = PyMongo(app) + +from flask_wtf import FlaskForm +from wtforms import StringField, FileField, SubmitField +from wtforms.validators import DataRequired + +class PinForm(FlaskForm): + description = StringField('Description', validators=[DataRequired()]) + photo = FileField('Photo', validators=[DataRequired()]) + submit = SubmitField('Add Pin') + +def allowed_file(filename): + return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS'] + +@app.route('/') +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 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')) + else: + flash('Invalid file type. Only images are allowed.') + return render_template('add_pin.html', form=form) + + +if __name__ == '__main__': + app.run(debug=True) diff --git a/app2.py b/app2.py new file mode 100644 index 0000000..d2cd3e9 --- /dev/null +++ b/app2.py @@ -0,0 +1,67 @@ +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/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8db452b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,13 @@ +blinker==1.8.1 +click==8.1.7 +dnspython==2.6.1 +Flask==3.0.3 +Flask-PyMongo==2.3.0 +Flask-WTF==1.2.1 +itsdangerous==2.2.0 +Jinja2==3.1.3 +leaflet==0.0.3 +MarkupSafe==2.1.5 +pymongo==4.7.1 +Werkzeug==3.0.2 +WTForms==3.1.2 diff --git a/static/action.js b/static/action.js new file mode 100644 index 0000000..131472e --- /dev/null +++ b/static/action.js @@ -0,0 +1 @@ +var map = L.map('map').fitWorld();L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {attribution: '© OpenStreetMap contributors'}).addTo(map); diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..3c7c2a2 --- /dev/null +++ b/static/style.css @@ -0,0 +1 @@ +body {padding: 0;margin: 0;}html, body, #map {height: 100%;width: 100vw;} diff --git a/static/styles.css b/static/styles.css new file mode 100644 index 0000000..316c1da --- /dev/null +++ b/static/styles.css @@ -0,0 +1,27 @@ +body { + font-family: Arial, sans-serif; + margin: 0; + padding: 20px; +} + +h1 { + text-align: center; +} + +form { + max-width: 400px; + margin: 0 auto; + padding: 20px; + border: 1px solid #ccc; + border-radius: 5px; +} + +form p { + margin: 10px 0; +} + +#map { + width: 100%; + height: 600px; + margin: 20px 0; +} diff --git a/templates/add_pin.html b/templates/add_pin.html new file mode 100644 index 0000000..49c23f5 --- /dev/null +++ b/templates/add_pin.html @@ -0,0 +1,49 @@ + + + + + Add Pin + + + + +

Add a New Pin

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

+ {{ form.description.label }}
+ {{ form.description(size=32) }} +

+

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

+

+
+ +

+

+
+ +

+

{{ form.submit() }}

+
+ Back to map + +
+ + + + diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..16ad201 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,16 @@ + + + + + + + + + + +{% block head %} +{% endblock %} +{% block content %} + +{% endblock %} \ No newline at end of file diff --git a/templates/form.html b/templates/form.html new file mode 100644 index 0000000..0fed9f6 --- /dev/null +++ b/templates/form.html @@ -0,0 +1,19 @@ +
+ + +
+ +
+ {{ form.hidden_tag() }} +
+ {{form.author.label}} {{form.author(class="form-control")}} + {{form.img.label}} {{form.img()}} + {{form.latitude()}} + {{form.longitude()}} + {{form.message.label}}{{form.message(class="form-control")}} + + {{form.submit(class="submit-btn")}} +
+ {{ form.csrf_token }} + {{ form.name.label }} {{ form.name(size=20) }} +
\ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..3be777a --- /dev/null +++ b/templates/index.html @@ -0,0 +1,26 @@ + + + + + Interactive Map + + + + +

Interactive Map

+
+ + + + diff --git a/templates/map.html b/templates/map.html new file mode 100644 index 0000000..586baf9 --- /dev/null +++ b/templates/map.html @@ -0,0 +1,40 @@ +{% extends 'base.html' %} + +{% block header %} +

{% block title %}Agregar bache o algo{% endblock title %}

+{% endblock header %} + +{% block content %} +
+

Mapa de urbanismo chingón

+
+
+ + +{% include 'form.html' %} + + +{% endblock content %}