Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[17]add contact us endpoint #32

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion backend/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from flask import Flask

from backend.extensions import db, migrate, jwt
from backend.extensions import db, mail, migrate, jwt
from backend.blueprints import auth
from backend.blueprints.contact import contact


def create_app():
Expand All @@ -22,9 +23,11 @@ def check_if_token_revoked(decoded_token):
def initialize_extensions(app):
"""Helper functions"""
db.init_app(app)
mail.init_app(app)
migrate.init_app(app, db, directory='migrations')
jwt.init_app(app)


def register_blueprints(app):
app.register_blueprint(auth.auth_blueprint)
app.register_blueprint(contact)
Empty file added backend/blueprints/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions backend/blueprints/contact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from flask import Blueprint, jsonify, request
from flask_mail import Message

from marshmallow import ValidationError

from backend.serializers.message_serializer import message_schema
from backend.extensions import mail

contact = Blueprint('contact', __name__)


@contact.route('/send-email/', methods=['POST'])
def send_message():
try:
new_msg = message_schema.load(request.json)
except ValidationError as err:
return jsonify(err.messages), 400
msg = Message(
subject='Email z cdf_v3 od {}'.format(new_msg["name"]),
sender=new_msg["email"],
reply_to=new_msg["email"],
recipients=["cfp_v3@cfp_v3.com"] # has to be change to valid email
)
msg.body = 'Nowa wiadomość od {}, nr tel: {} \nTreść:\n {}'.format(
new_msg["name"],
new_msg["phone"],
new_msg["content"])
mail.send(msg)

return jsonify({"message": "Contact message successfully sent"}), 200
4 changes: 3 additions & 1 deletion backend/extensions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from flask_jwt_extended import JWTManager
from flask_mail import Mail
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy

"""Create the instances of the Flask extensions in the global scope"""

migrate = Migrate()
db = SQLAlchemy()
jwt = JWTManager()
mail = Mail()
migrate = Migrate()
1 change: 1 addition & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ psycopg2==2.7.6
pytest==4.1.0
marshmallow==3.0.0rc1
flask-jwt-extended==3.14.0
Flask-Mail==0.9.1
Empty file added backend/serializers/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions backend/serializers/message_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from marshmallow import fields, Schema, validate


class MessageSchema(Schema):
class Meta:
fields = ('name', 'email', 'phone', 'content')

name = fields.Str(required=True)
email = fields.Email(
required=True,
error_messages={'required':
{'message': 'Valid email is required',
'code': 400}})
phone = fields.Str()
content = fields.Str(
required=True,
validate=[validate.Length(min=10)],
error_messages={'required':
{'message': 'Content of message is required',
'code': 400}})


message_schema = MessageSchema()