from flask import render_template, make_response
from flask_restful import Resource, reqparse
from werkzeug import exceptions

from website.activity_logs import activities

ACTIVITY_PLACEHOLDER = "Please Pick an Activity"


class AddActivity(Resource):

    def __init__(self):
        self.header = {'Content-Type': 'text/html'}
        self.activities = activities.Activities()
        self._parser = reqparse.RequestParser(bundle_errors=True)
        self._parser.add_argument('activity', type=str, required=True)
        self._parser.add_argument('date', type=str)
        self._parser.add_argument('activity_time', type=float)

    def get(self):
        activity_list = [ACTIVITY_PLACEHOLDER] + self.activities.activity_list
        template = render_template('add_activity.html', activity_list=activity_list)
        return make_response(template, 200, self.header)

    def post(self):
        try:
            activity_info = self._parser.parse_args()
        except exceptions.BadRequest as e:
            err_msgs = []
            err_data = e.data
            for field, err in err_data['message'].items():
                err = err.strip()
                err = err.replace(':', '')
                err_msgs.append('Error with {}: {}'.format(field, err))

            activity_list = [ACTIVITY_PLACEHOLDER] + self.activities.activity_list
            template = render_template('add_activity.html', error=err_msgs, activity_list=activity_list)
            return make_response(template, 400, self.header)

        if activity_info.get('activity') == ACTIVITY_PLACEHOLDER:
            activity_list = [ACTIVITY_PLACEHOLDER] + self.activities.activity_list
            template = render_template('add_activity.html', activity_list=activity_list)
            return make_response(template, 200, self.header)

        if not activity_info.get('date'):
            activity_list = [ACTIVITY_PLACEHOLDER] + self.activities.activity_list
            template = render_template('add_activity.html', activity_list=activity_list,
                                       activity=activity_info.get('activity'))
            return make_response(template, 200, self.header)

        success = self.activities.add_activity(activity_info)

        if success:
            message = "Successfully added activity"
            activity_list = [ACTIVITY_PLACEHOLDER] + self.activities.activity_list
            template = render_template('add_activity.html', alert=message, activity_list=activity_list)
            return make_response(template, 400, self.header)

        else:
            err_msgs = "Failed to add activity"
            activity_list = [ACTIVITY_PLACEHOLDER] + self.activities.activity_list
            template = render_template('add_activity.html', error=err_msgs, activity_list=activity_list)
            return make_response(template, 400, self.header)

@@ -41,16 +41,25 @@
 response
+, request
 %0Afrom fl
@@ -89,50 +89,8 @@
 urce
-, reqparse%0Afrom werkzeug import exceptions
 %0A%0Afr
@@ -343,260 +343,8 @@
 es()
-%0A        self._parser = reqparse.RequestParser(bundle_errors=True)%0A        self._parser.add_argument('activity', type=str, required=True)%0A        self._parser.add_argument('date', type=str)%0A        self._parser.add_argument('activity_time', type=float)
 %0A%0A  
@@ -614,224 +614,458 @@
     
-try:%0A            activity_info = self._parser.parse_args()%0A        except exceptions.BadRequest as e:%0A            err_msgs = %5B%5D%0A            err_data = e.data%0A            for field, err in err_data%5B'message'%5D.items():
+if request.form:%0A            param = request.form%0A            # to dict %22flattens%22 the form- may lose a value if duplicate keys. Not really sure how that would happen.%0A            # (source: http://werkzeug.pocoo.org/docs/0.11/datastructures/#werkzeug.datastructures.MultiDict.to_dict)%0A            activity_info = param.to_dict()%0A            if not activity_info:%0A                activity_list = %5BACTIVITY_PLACEHOLDER%5D + self.activities.activity_list
 %0A   
@@ -1084,139 +1084,345 @@
  err
+or
  = 
-err.strip()%0A                err = err.replace(':', '')%0A                err_msgs.append('Error with %7B%7D: %7B%7D'.format(field, err))%0A%0A
+'Error sending activity info'%0A                template = render_template('add_activity.html', error=error, activity_list=activity_list)%0A                return make_response(template, 400, self.header)%0A        else:%0A            try:%0A                activity_info = request.get_json(force=True)%0A            except Exception as e:%0A    
     
@@ -1504,32 +1504,36 @@
 ist%0A            
+    
 template = rende
@@ -1563,39 +1563,40 @@
 y.html', error=e
-rr_msgs
+.message
 , activity_list=
@@ -1602,32 +1602,36 @@
 =activity_list)%0A
+    
             retu

Add Activity: don't use reqparse as it is depricated