import json
import logging

import requests
from django.conf import settings
from django.core.management import BaseCommand

from api.models import Feedback

logger = logging.getLogger(__name__)


def send_feedback_to_open311(f):
    open_311_url = settings.OPEN311_URL + "/requests.json"

    data = dict(
            api_key=settings.OPEN311_API_KEY,
            service_code=f.service_code,
            description=f.description,
            title=f.title,
            lat=f.lat,
            long=f.lon,
            service_object_type=f.service_object_type,
            service_object_id=f.service_object_id,
            address_string=f.address_string,
            email=f.email,
            first_name=f.first_name,
            last_name=f.last_name,
            phone=f.phone,
            media_url=f.media_url
    )

    r = requests.post(open_311_url, data=data, allow_redirects=True)
    content = json.loads(r.content.decode('utf-8'))

    if r.status_code == 200:
        f.service_request_id = content[0]['service_request_id']
        f.service_notice = content[0]['service_notice']
        f.save()
    else:
        logger.info(content)


class Command(BaseCommand):
    help = 'Push new feedbacks to Open311 and save their service_request_id.'

    def handle(self, *args, **options):
        feedbacks = Feedback.objects.filter(service_request_id='')
        logger.info("Number of feedback to send: {}".format(len(feedbacks)))

        for feedback in feedbacks:
            send_feedback_to_open311(feedback)

        logger.info('Feedbacks are sent to remote system')

@@ -19,16 +19,30 @@
  logging
+%0Aimport imghdr
 %0A%0Aimport
@@ -130,16 +130,47 @@
 eCommand
+%0Afrom django.db.models import Q
 %0A%0Afrom a
@@ -194,16 +194,27 @@
 Feedback
+, MediaFile
 %0A%0Alogger
@@ -874,16 +874,365 @@
 %0A    )%0A%0A
+    files = MediaFile.objects.filter(feedback_id=f.pk)%0A    multiple_files = %5B%5D%0A%0A    # TODO: (for future releases) files uploading should be tested properly%0A    for file in files:%0A        file_type = 'image/' + imghdr.what(file.file)%0A        multiple_files.append(%0A                ('media', (file.original_filename, file.file, file_type))%0A        )%0A%0A
     r = 
@@ -1269,16 +1269,38 @@
 ta=data,
+ files=multiple_files,
  allow_r
@@ -1765,16 +1765,18 @@
 .filter(
+Q(
 service_
@@ -1789,12 +1789,57 @@
 t_id
+__isnull=True) %7C Q(service_request_id__exact
 ='')
+)
 %0A   

Support files uploading to Open311 in pushdata command