|
| 1 | +from src.services.userservice import UserService |
| 2 | +from flask import Flask, jsonify, request |
| 3 | +from flask_restful import Api |
| 4 | +from src.models.user import Users, User |
| 5 | +from src.models.post import Posts |
| 6 | +from src.models.message import MessageService |
| 7 | +from src.models.image_post import ImagePost |
| 8 | +from flask_jwt_extended import ( |
| 9 | + JWTManager, |
| 10 | + jwt_required, |
| 11 | + get_jwt_identity |
| 12 | +) |
| 13 | +import smtplib |
| 14 | +from email.mime.text import MIMEText |
| 15 | +from email.mime.multipart import MIMEMultipart |
| 16 | +from flask_cors import CORS |
| 17 | + |
| 18 | +app = Flask(__name__) |
| 19 | +CORS(app) |
| 20 | + |
| 21 | +app.config['JWT_SECRET_KEY'] = 'GRUWIN-ADMIN-ACCOUNT' |
| 22 | + |
| 23 | +app.config['JWT_ACCESS_TOKEN_EXPIRES'] = False |
| 24 | + |
| 25 | +jwt = JWTManager(app) |
| 26 | + |
| 27 | +api = Api(app) |
| 28 | + |
| 29 | +uservice = UserService() |
| 30 | + |
| 31 | +msg_service = MessageService() |
| 32 | + |
| 33 | +@app.route('/') |
| 34 | +def welcome(): |
| 35 | + return jsonify({'Flask restful': 'conected'}) |
| 36 | + |
| 37 | + |
| 38 | +@app.route('/signup', methods=['POST']) |
| 39 | +def signup(): |
| 40 | + return uservice.signup() |
| 41 | + |
| 42 | + |
| 43 | +@app.route('/login', methods=['POST']) |
| 44 | +def login(): |
| 45 | + return uservice.login() |
| 46 | + |
| 47 | + |
| 48 | +@app.route('/users', methods=['GET']) |
| 49 | +@jwt_required() |
| 50 | +def getUsers(): |
| 51 | + users = Users().get() |
| 52 | + return users |
| 53 | + |
| 54 | + |
| 55 | +@app.route('/users', methods=['POST']) |
| 56 | +def addUsers(): |
| 57 | + users = Users() |
| 58 | + return users.post() |
| 59 | + |
| 60 | + |
| 61 | +@app.route('/user/<string:id>') |
| 62 | +@jwt_required() |
| 63 | +def getUserById(id): |
| 64 | + user = User().get(id) |
| 65 | + return jsonify(user) |
| 66 | + |
| 67 | + |
| 68 | +@app.route('/post', methods=['GET']) |
| 69 | +@jwt_required() |
| 70 | +def getPost(): |
| 71 | + posts = Posts().get() |
| 72 | + return posts |
| 73 | + |
| 74 | + |
| 75 | +@app.route('/post', methods=['POST']) |
| 76 | +def createPost(): |
| 77 | + post = Posts() |
| 78 | + return post.post() |
| 79 | + |
| 80 | + |
| 81 | +@app.route('/imagepost/<int:postid>', methods=['GET']) |
| 82 | +@jwt_required() |
| 83 | +def getImagePost(postid): |
| 84 | + Image = ImagePost() |
| 85 | + return Image.get(postid) |
| 86 | + |
| 87 | + |
| 88 | +@app.route('/imagepost', methods=['POST']) |
| 89 | +def createImagePost(): |
| 90 | + image_post = ImagePost() |
| 91 | + return image_post.post() |
| 92 | + |
| 93 | + |
| 94 | +@app.route('/messages/<string:sender_id>/<string:recipient_id>', methods=['GET']) |
| 95 | +@jwt_required() |
| 96 | +def getMessages(sender_id, recipient_id): |
| 97 | + messages = msg_service.get_messages(sender_id, recipient_id) |
| 98 | + return jsonify(messages) |
| 99 | + |
| 100 | + |
| 101 | +@app.route('/messages', methods=['POST']) |
| 102 | +@jwt_required() |
| 103 | +def createMessage(): |
| 104 | + data = request.get_json() |
| 105 | + message = msg_service.create_message(data['sender_id'], data['recipient_id'], data['content']) |
| 106 | + return jsonify(message) |
| 107 | + |
| 108 | + |
| 109 | +@app.route('/messages/<string:id>', methods=['PUT']) |
| 110 | +@jwt_required() |
| 111 | +def updateMessage(id): |
| 112 | + data = request.get_json() |
| 113 | + message = msg_service.update_message(id, data) |
| 114 | + return jsonify(message) |
| 115 | + |
| 116 | + |
| 117 | +@app.route('/messages/<string:id>', methods=['DELETE']) |
| 118 | +@jwt_required() |
| 119 | +def deleteMessage(id): |
| 120 | + msg_service.delete_message(id) |
| 121 | + return '', 204 |
| 122 | + |
| 123 | + |
| 124 | +@app.route('/protected') |
| 125 | +@jwt_required() |
| 126 | +def protected(): |
| 127 | + current_user_id = get_jwt_identity() |
| 128 | + return jsonify({'message': f'¡Bienvenido, usuario con id {current_user_id}!'}), 200 |
| 129 | + |
| 130 | +@app.route('/email', methods=['POST']) |
| 131 | +@jwt_required() |
| 132 | +def sendEmail(): |
| 133 | + try: |
| 134 | + # Obtener los datos del cuerpo del mensaje |
| 135 | + data = request.get_json() |
| 136 | + |
| 137 | + if data: |
| 138 | + name_client = data.get('name_client') |
| 139 | + email_client = data.get('email_client') |
| 140 | + phone_client = data.get('phone_client') |
| 141 | + email_address = data.get('email_address') |
| 142 | + message_subject = data.get('message_subject') |
| 143 | + message_send = data.get('message_send') |
| 144 | + |
| 145 | + sender_email = "[email protected]" |
| 146 | + password = "ibdzsrjmsfkjhdbj" |
| 147 | + receiver_email = email_client |
| 148 | + |
| 149 | + message = MIMEMultipart("alternative") |
| 150 | + message["Subject"] = message_subject |
| 151 | + message["From"] = "Intermediario <{}>".format(sender_email) |
| 152 | + message["To"] = receiver_email |
| 153 | + |
| 154 | + # Crear el cuerpo del correo electrónico con los datos |
| 155 | + text = """<html> |
| 156 | + <body> |
| 157 | + <span style="font-size:15px;">Ey! Te estamos enviando este correo para informarte que tienes un nuevo mensaje:</span> |
| 158 | + <br> |
| 159 | + <br> |
| 160 | + <b style="font-size:15px;">Cliente:</b><br><span style="font-size:15px;">{}</span><br><br> |
| 161 | + <b style="font-size:15px;">Telefono:</b><br><span style="font-size:15px;">{}</span><br><br> |
| 162 | + <b style="font-size:15px;">Correo del cliente:</b><br><span style="font-size:15px;">{}</span><br><br> |
| 163 | + <b style="font-size:15px;">Mensaje del cliente:</b><br><span style="font-size:15px;">{}</span><br><br> |
| 164 | + </body> |
| 165 | + </html>""".format(name_client, phone_client, email_address, message_send) |
| 166 | + |
| 167 | + message.attach(MIMEText(text, "html")) |
| 168 | + |
| 169 | + # Iniciar sesión en el servidor SMTP y enviar el correo electrónico |
| 170 | + with smtplib.SMTP("smtp.gmail.com", 587) as server: |
| 171 | + server.starttls() |
| 172 | + server.login(sender_email, password) |
| 173 | + server.sendmail(sender_email, receiver_email, message.as_string()) |
| 174 | + |
| 175 | + return jsonify({"message": "Email sent successfully"}) |
| 176 | + else: |
| 177 | + return jsonify({"message": "No data received"}), 400 |
| 178 | + |
| 179 | + except Exception as e: |
| 180 | + return jsonify({"message": "An error occurred: {}".format(str(e))}), 500 |
| 181 | + |
| 182 | + |
| 183 | +@app.errorhandler(404) |
| 184 | +def not_found(error): |
| 185 | + return jsonify({'message': 'La URL solicitada no se encontró en el servidor.'}), 404 |
| 186 | + |
| 187 | + |
| 188 | +if __name__ == '__main__': |
| 189 | + app.run() |
0 commit comments