import os
from flask import Flask, request, redirect, url_for, render_template, jsonify
from flask_wtf import Form
from wtforms import StringField, TextField, TextAreaField, DateField, SelectField, RadioField, BooleanField
from wtforms.validators import DataRequired

app = Flask(__name__)


import smtplib
from email.mime.text import MIMEText


def send_mail(form):
    first = form.first.data
    last = form.last.data
    email = form.email.data
    description = form.description.data
    crawl_type = form.crawl_type.data
    recurring = form.recurring.data
    seed_ulrs = form.seed_ulrs.data
    crawling_config = form.crawling_config.data
    content_type = form.content_type.data
    how_much_data = form.how_much_data.data
    custom_metrics = form.custom_metrics.data
    extraction = form.extraction.data
    common_data_repository = form.common_data_repository.data
    raw_files = form.raw_files.data
    nutch_sequence_files = form.nutch_sequence_files.data
    custom_schema = form.custom_schema.data
    common_crawl_format = form.common_crawl_format.data
    warc = form.warc.data
    needed_by = form.needed_by.data
    client_email = "mail -s 'Memex Crawl Request' boustani@jpl.nasa.gov <<< 'Thank you for submitting your crawl data acquisition request to NASA JPL. Someone from the Crawl Team will contact you personally certainly within the next 24 hours. Our Crawl Infrastructure is already working on acquiring your requested data. If you have any issues, please do not hesitate to contact us on memex-crawl@jpl.nasa.gov. Thank you'"
    memex_email = "mail -s '[New Crawl Request]' boustani@jpl.nasa.gov <<< 'Request from {0} {1} and email: {2}' ".format(first, last, email)
    os.system(client_email)
    os.system(memex_email)

    
class MyForm(Form):
    first = StringField('First', validators=[DataRequired()])
    last = StringField('Last', validators=[DataRequired()])
    email = StringField('Email', validators=[DataRequired()])
    description = TextAreaField('Description of Use Case', validators=[DataRequired()])
    crawl_type = RadioField(u'Crawl Type', choices=[('exploratory', 'Exploratory'), ('defined', 'Defined'), ('particula_depth', 'Particula Depth')])
    recurring = StringField('Recurring', validators=[DataRequired()])
    seed_ulrs = TextAreaField('Seed Urls', validators=[DataRequired()])
    crawling_config = TextAreaField('Crawling configuration request', validators=[DataRequired()])
    content_type = RadioField('Content Type', choices=[('images', 'Images'), ('videos', 'Videos'), ('multimedia', 'Multimedia (images + videos)'), ('everything', 'Everything')])
    how_much_data = StringField('How much data', validators=[DataRequired()])
    custom_metrics = StringField('Custom Metrics', validators=[DataRequired()])
    extraction = TextAreaField('Extraction', validators=[DataRequired()])
    common_data_repository = BooleanField('Common Data Repository', validators=[DataRequired(False)])
    raw_files = BooleanField('Raw files', validators=[DataRequired(False)])
    nutch_sequence_files = BooleanField('Nutch Sequence Files', validators=[DataRequired(False)])
    custom_schema = BooleanField('Custom Schema', validators=[DataRequired(False)])
    common_crawl_format = BooleanField('Common Crawl Format', validators=[DataRequired(False)])
    warc = BooleanField('WARC', validators=[DataRequired(False)])
    needed_by = DateField('Needed by', validators=[DataRequired(False)])


    
@app.route('/', methods=('GET', 'POST'))
def index():
    message = "Welcome to the DARPA Memex crawl data request CrawlForm provided by NASA JPL. This form enables clients to submit jobs to be processed and delivered by the NASA JPL Team. A member of the Crawl Team will also reach out and ensure that the Crawl job meets your specifications and exceptions. We encourage you to provide as much input into the fields below as possible."
    form = MyForm()
    error = ""
    if form.validate_on_submit():
        send_mail(form)
        return redirect('/success')
    else:
        pass
    return render_template('index.html', form=form, error=error, message=message, backhome="")


@app.route("/success")
def success():
    backhome = "True"
    message = "Thank you for submitting your crawl data acquisition request to NASA JPL. Someone from the Crawl Team will contact you personally certainly within the next 24 hours. Our Crawl Infrastructure is already working on acquiring your requested data. If you have any issues, please do not hesitate to contact us on memex-crawl@jpl.nasa.gov. Thank you"
    return render_template('index.html', message=message, backhome=backhome)


if __name__ == "__main__":
    app.debug = True
    app.secret_key = 's3cr3t'
    app.run(host="0.0.0.0", port=4000, debug=True)

@@ -1562,32 +1562,35 @@
  memex_email = %22
+%22%22 
 mail -s '%5BNew Cr
@@ -1628,16 +1628,20 @@
 .gov %3C%3C%3C
+%0A   
  'Reques
@@ -1646,64 +1646,810 @@
 est 
-from %7B0%7D %7B1%7D and email: %7B2%7D' %22.format(first, last, email
+details:%0A        First Name:%7B0%7D%0A        Last Name:%7B1%7D%0A        Email: %7B2%7D%0A        Description: %7B3%7D%0A        Crawl Type: %7B4%7D%0A        Recurring: %7B5%7D%0A        Seed Urls: %7B6%7D%0A        Crawling Config: %7B7%7D%0A        Content Type: %7B8%7D%0A        How much data: %7B9%7D%0A        Custom Metrics: %7B10%7D%0A        Extraction: %7B11%7D%0A        Common Data Repository: %7B12%7D%0A        Raw Files: %7B13%7D%0A        Nutch Sequence: %7B14%7D%0A        Custom Schema: %7B15%7D%0A        Common Crawl Format: %7B16%7D%0A        WARC: %7B17%7D%0A        Needed by: %7B18%7D%0A%0A    Thanks%0A    Memex Crawl Team%0A    '%0A    %22%22%22.format(first, last, email, description, crawl_type, recurring, seed_ulrs, crawling_config, content_type, how_much_data, custom_metrics, extraction, common_data_repository, raw_files, nutch_sequence_files, custom_schema, common_crawl_format, warc, needed_by
 )%0A  

send request details to memex crawl team