Skip to content

Instantly share code, notes, and snippets.

@ThiefMaster
Created September 20, 2013 23:32
Show Gist options
  • Save ThiefMaster/6645303 to your computer and use it in GitHub Desktop.
Save ThiefMaster/6645303 to your computer and use it in GitHub Desktop.
#encoding: UTF-8
import re
from flask import Flask, jsonify, render_template, request, redirect
from flask.ext.mail import Mail, Message
app = Flask(__name__)
mail=Mail(app)
app.config.update(
DEBUG=True,
#EMAIL SETTINGS
MAIL_SERVER='smtp.sendgrid.net',
MAIL_PORT=465,
MAIL_USE_SSL=True,
MAIL_USERNAME = 'lol',
MAIL_PASSWORD = 'lolz'
)
mail=Mail(app)
def request_is_mobile(request):
is_mobile = False;
if request.headers.has_key('User-Agent'):
user_agent = request.headers.get('User-Agent')
# Test common mobile values.
pattern = "(up.browser|up.link|iphone|mmp|symbian|smartphone|midp|wap|phone|windows ce|pda|mobile|mini|palm|netfront)"
prog = re.compile(pattern, re.IGNORECASE)
match = prog.search(user_agent)
print user_agent
if match:
is_mobile = True;
if not is_mobile:
# Now we test the user_agent from a big list.
user_agents_test = ("w3c ", "acs-", "alav", "alca", "amoi", "audi",
"avan", "benq", "bird", "blac", "blaz", "brew",
"cell", "cldc", "cmd-", "dang", "doco", "eric",
"hipt", "inno", "ipaq", "java", "jigs", "kddi",
"keji", "leno", "lg-c", "lg-d", "lg-g", "lge-",
"maui", "maxo", "midp", "mits", "mmef", "mobi",
"mot-", "moto", "mwbp", "nec-", "newt", "noki",
"xda", "palm", "pana", "pant", "phil", "play",
"port", "prox", "qwap", "sage", "sams", "sany",
"sch-", "sec-", "send", "seri", "sgh-", "shar",
"sie-", "siem", "smal", "smar", "sony", "sph-",
"symb", "t-mo", "teli", "tim-", "tosh", "tsm-",
"upg1", "upsi", "vk-v", "voda", "wap-", "wapa",
"wapi", "wapp", "wapr", "webc", "winw", "winw",
"xda-",)
test = user_agent[0:4].lower()
if test in user_agents_test:
is_mobile = True
return is_mobile
@app.route("/")
def landing():
return render_template('index.html',is_mobile= request_is_mobile(request))
@app.route('/contact_form', methods=['POST'])
def contact_form():
'''
Sends email
'''
email_sent = 'true'
if request.method == 'POST':
message = u'%s escribió con el mensaje: %s'%(request.form['contact_info'],request.form['message'])
email_recipient = 'hello@bemena.me'
subject = 'Desde la forma de contacto de bemena.com'
msg = Message(
subject,
sender='hola@bandtastic.me',
recipients=[email_recipient]
)
msg.body = message
try:
mail.send(msg)
except:
email_sent = 'false'
return jsonify(status=email_sent)
@app.route("/<path:all_other_urls>")
def missing_url(all_other_urls):
'''
Redirect and be done with it
'''
return redirect('/')
if __name__ == "__main__":
app.debug = True
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment