import json
import logging
import MySQLdb
import os

from dirq.queue import Queue, QueueError
from django.conf import settings
from rest_framework.response import Response
from rest_framework.views import APIView

from apel.db.loader import Loader, LoaderException

class IndexView(APIView):
    """An example URL."""

    def get(self, request, format=None):
        """An example get method."""
        logger = logging.getLogger(__name__)

        response = "Hello, world. You're at the index."

        # parse query parameters
        group_name = request.GET.get('group', '')
        if group_name is "":
            group_name = None
        service_name = request.GET.get('service', '')

        if service_name is "":
            service_name = None

        start_date = request.GET.get('from', '')
        if start_date is "":
            start_date = None

        end_date = request.GET.get('to', '')
        if end_date is "":
            end_date = "TODAY"

        logger.info("%s %s %s %s", group_name, service_name, start_date, end_date)

        # get the data requested
        database = MySQLdb.connect('localhost', 'root', '', 'apel_rest')
        cursor = database.cursor()

        if group_name is not None:
            cursor.execute('select * from cloudrecords where VOGroup = %s', [group_name])
        elif service_name is not None:
            cursor.execute('select * from cloudrecords where SiteName = %s', [service_name])
        else:
            cursor.execute('select VOGroupID, SiteID, DATE_FORMAT(UpdateTime, \'%d/%m/%Y %H:%M:%S\') AS TimeStamp, WallDuration from cloudrecords')

        columns = cursor.description
        result = [{columns[index][0]:column for index, column in enumerate(value)} for value in cursor.fetchall()]

        return Response(json.dumps(result), status=200)

    def post(self, request, format=None):
        """An example post method."""
        logger = logging.getLogger(__name__)

        try:
            empaid = request.META['HTTP_EMPA_ID']
        except KeyError:
            empaid = 'noid'

        logger.info("Received message. ID = %s", empaid)

        try:
            signer = request.META['SSL_CLIENT_S_DN']
        except KeyError:
            signer = "None"

        if "_content" in request.POST.dict():
            # then POST likely to come via the rest api framework
            # hence use the content of request.POST as message
            body = request.POST.get('_content')
        else:
            # POST likely to comes through a browser client or curl
            # hence use request.body as message
            body = request.body

        logger.debug("Message body received: %s", body)

        for header in request.META:
            logger.debug("%s: %s", header, request.META[header])

        # taken from ssm2
        QSCHEMA = {'body': 'string',
                   'signer': 'string',
                   'empaid': 'string?'}

        inqpath = os.path.join(settings.QPATH, 'incoming')
        # rejectqpath = os.path.join(qpath, 'reject')
        inq = Queue(inqpath, schema=QSCHEMA)
        # rejectq = Queue(rejectqpath, schema=REJECT_SCHEMA)

        try:
            name = inq.add({'body': body,
                            'signer': signer,
                            'empaid': empaid})
        except QueueError as err:
            logger.error("Could not save %s/%s: %s", inqpath, name, err)

            response = "Data could not be saved to disk, please try again."
            return Response(response, status=500)

        logger.info("Message saved to in queue as %s/%s", inqpath, name)

@@ -1552,17 +1552,17 @@
 %25d/%25
-m
+M
 /%25Y %25H:%25
 M:%25S
@@ -1561,12 +1561,12 @@
 %25H:%25
-M:%25S
+i:%25s
 %5C') 
@@ -1794,26 +1794,14 @@
 nse(
-json.dumps(
 result
-)
 , st

Fix format of return data