<commit_before>from api import app, db
from flask import (render_template, jsonify, request, redirect, url_for,
                   Response, make_response)
from models import Event
from flask.ext.security import login_required
import json
from datetime import datetime
from os import environ
import requests

e = Event()  # Global Instance of Event

@app.route('/')
def api_admin_panel_home():
    return redirect('/admin')


@app.route('/api/events/')
def get_events():
    '''
    returns all the events with GET request
    '''
    time = request.args.get('time')
    limit = request.args.get('limit')
    if not limit:
        # Default limit 3 for HomePage
        limit = 3
    if limit:
        try:
            limit = int(limit)
        except:
            return make_response(jsonify({"Message":
                                          "Limit should be an integer"}), 400)
    if time == "past":
        allEvents = Event.objects.filter(
            event_start_date__lte=datetime.now()).limit(limit)
    elif time == "future":
        allEvents = Event.objects.filter(
            event_start_date__gt=datetime.now()).limit(limit)
    elif (time) and not (time == "past" or time == "future"):
        return make_response(jsonify({"Message":
                                      "Time should be past or future"}), 400)
    else:
        allEvents = Event.objects.all().limit(limit)
    result = create_dict(allEvents)
    return Response(json.dumps(result, cls=PythonJSONEncoder), status=200,
                    content_type="application/json")


@app.route('/api/events/<int:event_id>')
@app.route('/api/events/<int:event_id>/')
def get_single_event(event_id):
    singleEvent = Event.objects.get(eid=event_id)
    result = create_single_event_dict(singleEvent)
    return Response(json.dumps(result, cls=PythonJSONEncoder), status=200,
                    content_type="application/json")


@app.route('/api/send-contact-us-form/', methods=['GET', 'POST'])
def send_simple_message():
    if request.method == 'POST':
        json_response = json.loads(request.data)
        name = json_response['name']
        email = json_response['email']
        message = json_response['message']
        subject = 'Contact Us | MozPacers : Response from ' + name
        requests.post(
            environ["MAIL_ADDRESS"],
            auth=("api", environ["API_KEY"]),
            data={"from": environ["MAIL_FROM"],
                  "to": environ["MAIL_TO"],
                  "subject": 'Contact Us | MozPacers : Response from ' + name,
                  "text": name + ' with mail id: ' + email + ' just filled the' + \
                          ' contact-us form on MozPacers.org with message: ' + message})
        return Response(json.dumps({"Message": "Email Sent successfully"}), status=200,
                            content_type="application/json")<commit_msg>Handled DoesNotExist exception for SingleEvent<commit_after>from api import app, db
from flask import (render_template, jsonify, request, redirect, url_for,
                   Response, make_response)
from models import Event
from flask.ext.security import login_required
import json
from datetime import datetime
from os import environ
import requests
from mongoengine.queryset import DoesNotExist

e = Event()  # Global Instance of Event

@app.route('/')
def api_admin_panel_home():
    return redirect('/admin')


@app.route('/api/events/')
def get_events():
    '''
    returns all the events with GET request
    '''
    time = request.args.get('time')
    limit = request.args.get('limit')
    if not limit:
        # Default limit 3 for HomePage
        limit = 3
    if limit:
        try:
            limit = int(limit)
        except:
            return make_response(jsonify({"Message":
                                          "Limit should be an integer"}), 400)
    if time == "past":
        allEvents = Event.objects.filter(
            event_start_date__lte=datetime.now()).limit(limit)
    elif time == "future":
        allEvents = Event.objects.filter(
            event_start_date__gt=datetime.now()).limit(limit)
    elif (time) and not (time == "past" or time == "future"):
        return make_response(jsonify({"Message":
                                      "Time should be past or future"}), 400)
    else:
        allEvents = Event.objects.all().limit(limit)
    result = create_dict(allEvents)
    return Response(json.dumps(result, cls=PythonJSONEncoder), status=200,
                    content_type="application/json")


@app.route('/api/events/<int:event_id>')
@app.route('/api/events/<int:event_id>/')
def get_single_event(event_id):
    try:
        singleEvent = Event.objects.get(eid=event_id)
        result = create_single_event_dict(singleEvent)
    except DoesNotExist:
        return Response(json.dumps({"Message": "Event not found"}), status=404,
                        content_type="application/json")
    return Response(json.dumps(result, cls=PythonJSONEncoder), status=200,
                    content_type="application/json")


@app.route('/api/send-contact-us-form/', methods=['GET', 'POST'])
def send_simple_message():
    if request.method == 'POST':
        json_response = json.loads(request.data)
        name = json_response['name']
        email = json_response['email']
        message = json_response['message']
        subject = 'Contact Us | MozPacers : Response from ' + name
        requests.post(
            environ["MAIL_ADDRESS"],
            auth=("api", environ["API_KEY"]),
            data={"from": environ["MAIL_FROM"],
                  "to": environ["MAIL_TO"],
                  "subject": 'Contact Us | MozPacers : Response from ' + name,
                  "text": name + ' with mail id: ' + email + ' just filled the' + \
                          ' contact-us form on MozPacers.org with message: ' + message})
        return Response(json.dumps({"Message": "Email Sent successfully"}), status=200,
                            content_type="application/json")
{'fraction_non_alphanumeric': 0.12429378531073447, 'fraction_numerical': 0.006148222000664672, 'mean_word_length': 2.4872537659327927, 'pattern_counts': {'":': 19, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1272546', 'n_tokens_mistral': 1707, 'n_tokens_neox': 1656, 'n_words': 413}