main.py 9.76 KB
from __future__ import print_function

import datetime
import os

import pytz
import voeventparse as vp

import jinja2
import uuid

from flask import Flask, render_template, request, flash
from forms import ContactForm , ContactFormLunar, ContactFormComet

app =Flask(__name__)
app.secret_key = 'development key'

@app.route('/')
def home():
  return render_template('home.html')

@app.route('/fireball', methods=['GET', 'POST'])
def index():
  form = ContactForm()
  if request.method == 'POST':
     if form.validate() == False:
       flash('An error occured : the form was not validated.')
       return render_template('index.html', form=form)
     else:
       voevent_id = uuid.uuid4()
       # Get the date
       date=str(datetime.datetime.strptime(request.form['DateTime'],"%Y-%m-%d %H:%M:%S"))
       my_datetime=date[0:10]+"T"+date[11:19]

      # Set the basic packet ID and Author details

       templateLoader = jinja2.FileSystemLoader( searchpath="/" )
       templateEnv = jinja2.Environment( loader=templateLoader )

       TEMPLATE_FILE = "/tmp/voevent-fireball.xml"
       template = templateEnv.get_template( TEMPLATE_FILE )

       # using dictionary to fill information
       # case of what list [value, datatype, ucd]
       my_list_what={"event_type":['giant planet fireball','string','meta.flag'],
              "event_status":['finished','string','meta.flag']}
       my_groupname='target'
       my_list_groupname = [{"longitude":[request.form['Llongitude'],'float','pos.bodyrc.lon','deg'],
                      "latitude":[request.form['Llatitude'],'float','pos.bodyrc.lat','deg']}]

       my_groupname_source='source'
       my_list_groupname_source = [{"target_class":['comet','string','meta.id'],
                      "target_name":[request.form['comet'],'string','meta.id'],
                      "target_region":['trail','string','meta.id']}]

       my_list_wherewhen_target={"target_name":"67P",
                          "target_class":"comet",
                          "target_region":"tail"}
       my_list_wherewhen_time={"time_obs":"2017-02-24T11:07:28.1Z",
                        "time_scale":"UTC",
                        "target_origin":"Earth"}
       my_list_how_instrument={"instrument_description":request.form['instrument']}

       # one dictionary for all field description
       my_list_description = {"what":request.form['description']}

       output_filename = '/var/tmp/new_voevent.xml'
       new_voevent = open(output_filename,'w')
       new_voevent.write(template.render(voevent_id=voevent_id,
                       my_shortname=request.form['shortname'],
                       my_contactname=request.form['observer'],
                       my_contactphone=request.form['phone'],
                       my_contactemail=request.form['mail'],
                       my_datetime=my_datetime,
                       my_city=request.form['city'],
                       my_country=request.form['country'],
                       my_list_what=my_list_what,
                       my_list_wherewhen_target=my_list_wherewhen_target,
                       my_list_wherewhen_time=my_list_wherewhen_time,
                       my_list_how_instrument=my_list_how_instrument,
                       my_list_description=my_list_description,
                       my_groupname_source=my_groupname_source,
                       my_list_groupname_source=my_list_groupname_source,
                       my_groupname=my_groupname,
                       my_list_groupname=my_list_groupname))
       print("Wrote your voevent to ", os.path.abspath(output_filename))
       return render_template('success.html')
  elif request.method == 'GET':
   return render_template('index.html', form=form)

@app.route('/lunarflash', methods=['GET', 'POST'])
def indexlunar():
  form = ContactFormLunar()
  if request.method == 'POST':
     if form.validate() == False:
       flash('An error occured : the form was not validated.')
       return render_template('index-lunar.html', form=form)
     else:
       voevent_id = uuid.uuid4()
       # Get the date
       date=str(datetime.datetime.strptime(request.form['DateTime'],"%Y-%m-%d %H:%M:%S"))
       my_datetime=date[0:10]+"T"+date[11:19]

      # Set the basic packet ID and Author details

       templateLoader = jinja2.FileSystemLoader( searchpath="/" )
       templateEnv = jinja2.Environment( loader=templateLoader )

       TEMPLATE_FILE = "/tmp/voevent-lunar.xml"
       template = templateEnv.get_template( TEMPLATE_FILE )

       # using dictionary to fill information
       # case of what list [value, datatype, ucd]
       my_list_what={"event_type":['detection','string','meta.flag'],
              "event_status":['finished','string','meta.flag']}
       my_groupname='target'
       my_list_groupname = [{"longitude":[request.form['Llongitude'],'float','pos.bodyrc.lon','deg'],
                      "latitude":[request.form['Llatitude'],'float','pos.bodyrc.lat','deg']}]

       my_groupname_source='source'
       my_list_groupname_source = [{"target_class":['asteroid','string','meta.id'],
                      "target_name":[request.form['comet'],'string','meta.id'],
                      "target_subclass":['neo','string','meta.id']}]

       my_list_wherewhen_target={"target_name":"67P",
                          "target_class":"comet",
                          "target_region":"tail"}
       my_list_wherewhen_time={"time_obs":"2017-02-24T11:07:28.1Z",
                        "time_scale":"UTC",
                        "target_origin":"Earth"}
       my_list_how_instrument={"instrument_description":request.form['instrument']}

       # one dictionary for all field description
       my_list_description = {"what":request.form['description']}

       output_filename = '/var/tmp/new_voevent.xml'
       new_voevent = open(output_filename,'w')
       new_voevent.write(template.render(voevent_id=voevent_id,
                       my_shortname=request.form['shortname'],
                       my_contactname=request.form['observer'],
                       my_contactphone=request.form['phone'],
                       my_contactemail=request.form['mail'],
                       my_datetime=my_datetime,
                       my_city=request.form['city'],
                       my_country=request.form['country'],
                       my_list_what=my_list_what,
                       my_list_wherewhen_target=my_list_wherewhen_target,
                       my_list_wherewhen_time=my_list_wherewhen_time,
                       my_list_how_instrument=my_list_how_instrument,
                       my_list_description=my_list_description,
                       my_groupname_source=my_groupname_source,
                       my_list_groupname_source=my_list_groupname_source,
                       my_groupname=my_groupname,
                       my_list_groupname=my_list_groupname))

       print("Wrote your voevent to ", os.path.abspath(output_filename))
       return render_template('success.html')
  elif request.method == 'GET':
   return render_template('index-lunar.html', form=form)

@app.route('/comettail', methods=['GET', 'POST'])
def indexcomet():
  form = ContactFormComet()
  if request.method == 'POST':
     if form.validate() == False:
       flash('An error occured : the form was not validated.')
       return render_template('index-comet.html', form=form)
     else:
       voevent_id = uuid.uuid4()
       # Get the date
       date=str(datetime.datetime.strptime(request.form['DateTime'],"%Y-%m-%d %H:%M:%S"))
       my_datetime=date[0:10]+"T"+date[11:19]

      # Set the basic packet ID and Author details

       templateLoader = jinja2.FileSystemLoader( searchpath="/" )
       templateEnv = jinja2.Environment( loader=templateLoader )

       TEMPLATE_FILE = "/tmp/voevent-comet.xml"
       template = templateEnv.get_template( TEMPLATE_FILE )

       # using dictionary to fill information
       # case of what list [value, datatype, ucd]
       my_list_what={"event_type":['cometary tail disconection','string','meta.flag'],
              "event_status":['onset','string','meta.flag']}
       my_groupname='target'
       my_list_groupname = [{"target_class":['comet','string','meta.id'],
                      "target_name":['67P','string','meta.id'],
                      "target_region":['Tail','string','meta.id']}]
       my_list_wherewhen_target={"target_name":"67P",
                          "target_class":"comet",
                          "target_region":"tail"}
       my_list_wherewhen_time={"time_obs":"2017-02-24T11:07:28.1Z",
                        "time_scale":"UTC",
                        "target_origin":"Earth"}

       # one dictionary for all field description
       my_list_description = {"what":"Tail disconnection of comet 67P"}

       output_filename = '/var/tmp/new_voevent.xml'
       new_voevent = open(output_filename,'w')
       new_voevent.write(template.render(voevent_id=voevent_id,
                       my_shortname=request.form['shortname'],
                       my_contactname=request.form['observer'],
                       my_contactphone=request.form['phone'],
                       my_contactemail=request.form['mail'],
                       my_datetime=my_datetime,
                       my_city=request.form['city'],
                       my_country=request.form['country'],
                       my_list_what=my_list_what,
                       my_list_wherewhen_target=my_list_wherewhen_target,
                       my_list_description=my_list_description,
                       my_groupname=my_groupname,
                       my_list_groupname=my_list_groupname))
       print("Wrote your voevent to ", os.path.abspath(output_filename))
       return render_template('success.html')
  elif request.method == 'GET':
   return render_template('index-comet.html', form=form)

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True, port=80)