<commit_before>import logging
from corehq.apps.app_manager.views import require_can_edit_apps
import magic
from django.conf import settings
from couchdbkit.exceptions import ResourceNotFound
from django.contrib.sites.models import Site
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.utils import simplejson
from django.views.decorators.http import require_POST
import zipfile
from corehq.apps.hqmedia import utils
from corehq.apps.app_manager.models import get_app
from dimagi.utils.web import render_to_response
from corehq.apps.hqmedia.models import CommCareImage, CommCareAudio

X_PROGRESS_ERROR = 'Server Error: You must provide X-Progress-ID header or query param.'

def get_media_type(media_type):
    return {
        'CommCareImage': CommCareImage,
        'CommCareAudio': CommCareAudio,
    }[media_type]

def download_media(request, media_type, doc_id):
    try:
        media = get_media_type(media_type)
        try:
            try:
                media = media.get(doc_id)
            except Exception:
                logging.error("looks like %r.get(%r) failed" % (media, doc_id))
            data, content_type = media.get_display_file()
            response = HttpResponse(mimetype=content_type)
            response.write(data)
            return response
        except ResourceNotFound:
            raise Http404("No Media Found")
    except KeyError:
        raise Http404("unknown media type %s" % media_type)



@require_can_edit_apps
def search_for_media(request, domain, app_id):
    media_type = request.GET['t']
    if media_type == 'Image':
        files = CommCareImage.search(request.GET['q'])
    elif media_type == 'Audio':
        files = CommCareAudio.search(request.GET['q'])
    else:
        raise Http404()
    return HttpResponse(simplejson.dumps([
        {'url': i.url(),
         'licenses': [license.display_name for license in i.licenses],
         'tags': [tag for tags in i.tags.values() for tag in tags],
         'm_id': i._id} for i in files]))

@require_can_edit_apps
def choose_media(request, domain, app_id):
    # TODO: Add error handling
    app = get_app(domain, app_id)
    media_type = request.POST['media_type']
    media_id = request.POST['id']
    if media_type == 'Image':
        file = CommCareImage.get(media_id)
    elif media_type == 'Audio':
        file = CommCareImage.get(media_id)
    else:
        raise Http404()

    if file is None or not file.is_shared:
        return HttpResponse(simplejson.dumps({
            'match_found': False
        }))

    file.add_domain(domain)
    app.create_mapping(file, request.POST['path'])
    if media_type == 'Image':
        return HttpResponse(simplejson.dumps({
            'match_found': True,
            'image': {'m_id': file._id, 'url': file.url()},
            'file': True
        }))
    elif media_type == 'Audio':
        return HttpResponse(simplejson.dumps({'match_found': True, 'audio': {'m_id': file._id, 'url': file.url()}}))
    else:
        raise Http404()

@require_can_edit_apps
def media_urls(request, domain, app_id):
    app = get_app(domain, app_id)
    refs, missing_refs = app.get_template_map(request.GET.getlist('path[]'))
    pathUrls = {}
    for ref in refs:
        pathUrls[ref['path']] = ref

    return HttpResponse(simplejson.dumps(pathUrls))

@require_can_edit_apps
def media_map(request, domain, app_id):
    app = get_app(domain, app_id)
    sorted_images, sorted_audio, has_error = utils.get_sorted_multimedia_refs(app)

    images, missing_image_refs = app.get_template_map(sorted_images)
    audio, missing_audio_refs = app.get_template_map(sorted_audio)

    return render_to_response(request, "hqmedia/map.html", {
        "domain": domain,
        "app": app,
        "multimedia": {
         "images": images,
         "audio": audio,
         "has_error": has_error
        },
        "missing_image_refs": missing_image_refs,
        "missing_audio_refs": missing_audio_refs
    })

def media_from_path(request, domain, app_id, file_path):
    app = get_app(domain, app_id)
    sorted_images, sorted_audio, has_error = utils.get_sorted_multimedia_refs(app)

    images, _ = app.get_template_map(sorted_images)
    audio, _ = app.get_template_map(sorted_audio)

    for i in images:
        i['type'] = 'CommCareImage'

    for a in audio:
        a['type'] = 'CommCareAudio'

    media = images + audio
    for m in media:
        if m['path'][10:] == file_path: # [10:] is to remove the 'jr://file/'
            if m.get('m_id', ""):
                return download_media(request, m['type'], m['m_id'])
            else:
                raise Http404('No Media Found')
    raise Http404('No Media Found')
<commit_msg>cleanup and updates to multimedia refs<commit_after>import logging
from corehq.apps.app_manager.views import require_can_edit_apps
import magic
from django.conf import settings
from couchdbkit.exceptions import ResourceNotFound
from django.contrib.sites.models import Site
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.utils import simplejson
from django.views.decorators.http import require_POST
import zipfile
from corehq.apps.hqmedia import utils
from corehq.apps.app_manager.models import get_app
from dimagi.utils.web import render_to_response
from corehq.apps.hqmedia.models import CommCareImage, CommCareAudio

X_PROGRESS_ERROR = 'Server Error: You must provide X-Progress-ID header or query param.'

def get_media_type(media_type):
    return {
        'CommCareImage': CommCareImage,
        'CommCareAudio': CommCareAudio,
    }[media_type]

def download_media(request, media_type, doc_id):
    try:
        media = get_media_type(media_type)
        try:
            try:
                media = media.get(doc_id)
            except Exception:
                logging.error("looks like %r.get(%r) failed" % (media, doc_id))
            data, content_type = media.get_display_file()
            response = HttpResponse(mimetype=content_type)
            response.write(data)
            return response
        except ResourceNotFound:
            raise Http404("No Media Found")
    except KeyError:
        raise Http404("unknown media type %s" % media_type)



@require_can_edit_apps
def search_for_media(request, domain, app_id):
    media_type = request.GET['t']
    if media_type == 'Image':
        files = CommCareImage.search(request.GET['q'])
    elif media_type == 'Audio':
        files = CommCareAudio.search(request.GET['q'])
    else:
        raise Http404()
    return HttpResponse(simplejson.dumps([
        {'url': i.url(),
         'licenses': [license.display_name for license in i.licenses],
         'tags': [tag for tags in i.tags.values() for tag in tags],
         'm_id': i._id} for i in files]))

@require_can_edit_apps
def choose_media(request, domain, app_id):
    # TODO: Add error handling
    app = get_app(domain, app_id)
    media_type = request.POST['media_type']
    media_id = request.POST['id']
    if media_type == 'Image':
        file = CommCareImage.get(media_id)
    elif media_type == 'Audio':
        file = CommCareImage.get(media_id)
    else:
        raise Http404()

    if file is None or not file.is_shared:
        return HttpResponse(simplejson.dumps({
            'match_found': False
        }))

    file.add_domain(domain)
    app.create_mapping(file, request.POST['path'])
    if media_type == 'Image':
        return HttpResponse(simplejson.dumps({
            'match_found': True,
            'image': {'m_id': file._id, 'url': file.url()},
            'file': True
        }))
    elif media_type == 'Audio':
        return HttpResponse(simplejson.dumps({'match_found': True, 'audio': {'m_id': file._id, 'url': file.url()}}))
    else:
        raise Http404()

@require_can_edit_apps
def media_urls(request, domain, app_id):
    # IS THIS USED?????
    # I rewrote it so it actually produces _something_, but is it useful???
    app = get_app(domain, app_id)
    multimedia = app.get_media_references()
    pathUrls = {}
    for section, types in multimedia['references'].items():
        for media_type, info in types.items():
            for m in info['maps']:
                if m.get('path'):
                    pathUrls[m['path']] = m

    return HttpResponse(simplejson.dumps(pathUrls))

@require_can_edit_apps
def media_map(request, domain, app_id):
    app = get_app(domain, app_id)
    multimedia = app.get_media_references()

    return render_to_response(request, "hqmedia/map.html", {
        "domain": domain,
        "app": app,
        "multimedia": multimedia,
    })

def media_from_path(request, domain, app_id, file_path):
    # Not sure what the intentions were for this. I didn't see it getting used anywhere.
    # Rewrote it to use new media refs.
    # Yedi, care to comment?
    app = get_app(domain, app_id)
    multimedia = app.get_media_references()

    for section, types in multimedia['references'].items():
        for media_type, info in types.items():
            for media_map in info['maps']:
                 if media_map['path'][10:] == file_path and media_map.get('url'):
                    return HttpResponseRedirect(media_map['url'])

    raise Http404('No Media Found')

{'fraction_non_alphanumeric': 0.110618032254573, 'fraction_numerical': 0.00552007793051196, 'mean_word_length': 3.024390243902439, 'pattern_counts': {'":': 11, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6410248', 'n_tokens_mistral': 2921, 'n_tokens_neox': 2815, 'n_words': 706}