<commit_before>import imghdr
import json
import logging

from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.http import (HttpResponse, HttpResponseRedirect,
                         HttpResponseBadRequest, Http404)
from django.shortcuts import get_object_or_404
from django.views.decorators.http import require_POST

from commonware.decorators import xframe_sameorigin
import jingo
from tower import ugettext as _

from gallery import ITEMS_PER_PAGE, DRAFT_TITLE_PREFIX
from gallery.forms import ImageForm, VideoForm
from gallery.models import Image, Video
from gallery.utils import upload_image, upload_video, check_media_permissions
from sumo.urlresolvers import reverse
from sumo.utils import paginate
from upload.utils import FileTooLargeError

MSG_FAIL_UPLOAD = {'image': _('Could not upload your image.'),
                   'video': _('Could not upload your video.')}


log = logging.getLogger('k.gallery')


def gallery(request, media_type='image'):
    """The media gallery.

    Filter can be set to 'images' or 'videos'.

    """
    if media_type == 'image':
        media_qs = Image.objects.filter(locale=request.locale)
    elif media_type == 'video':
        media_qs = Video.objects.filter(locale=request.locale)
    else:
        raise Http404

    media = paginate(request, media_qs, per_page=ITEMS_PER_PAGE)

    draft = _get_draft_info(request.user)
    image_form, video_form = _init_upload_forms(request, draft)

    return jingo.render(request, 'gallery/gallery.html',
                        {'media': media,
                         'media_type': media_type,
                         'image_form': image_form,
                         'video_form': video_form})


@login_required
@require_POST
def upload(request, media_type='image'):
    """Finalizes an uploaded draft.

    We could probably use this same form to handle no-JS fallback, if
    we ever need to support that.

    """
    draft = _get_draft_info(request.user)
    if media_type == 'image' and draft['image']:
        # We're publishing an image draft!
        image_form = _init_media_form(ImageForm, request, draft['image'])
        if image_form.is_valid():
            img = image_form.save()
            return HttpResponseRedirect(img.get_absolute_url())
        else:
            return gallery(request, media_type='image')
    elif media_type == 'video' and draft['video']:
        # We're publishing a video draft!
        video_form = _init_media_form(VideoForm, request, draft['video'])
        if video_form.is_valid():
            vid = video_form.save()
            return HttpResponseRedirect(vid.get_absolute_url())
        else:
            return gallery(request, media_type='video')

    return HttpResponseBadRequest(u'Unrecognized POST request.')
<commit_msg>Force cache invalidation on upload. [bug 614110]<commit_after>import imghdr
import json
import logging

from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.http import (HttpResponse, HttpResponseRedirect,
                         HttpResponseBadRequest, Http404)
from django.shortcuts import get_object_or_404
from django.views.decorators.http import require_POST

from commonware.decorators import xframe_sameorigin
import jingo
from tower import ugettext as _

from gallery import ITEMS_PER_PAGE, DRAFT_TITLE_PREFIX
from gallery.forms import ImageForm, VideoForm
from gallery.models import Image, Video
from gallery.utils import upload_image, upload_video, check_media_permissions
from sumo.urlresolvers import reverse
from sumo.utils import paginate
from upload.utils import FileTooLargeError

MSG_FAIL_UPLOAD = {'image': _('Could not upload your image.'),
                   'video': _('Could not upload your video.')}


log = logging.getLogger('k.gallery')


def gallery(request, media_type='image'):
    """The media gallery.

    Filter can be set to 'images' or 'videos'.

    """
    if media_type == 'image':
        media_qs = Image.objects.filter(locale=request.locale)
    elif media_type == 'video':
        media_qs = Video.objects.filter(locale=request.locale)
    else:
        raise Http404

    media = paginate(request, media_qs, per_page=ITEMS_PER_PAGE)

    draft = _get_draft_info(request.user)
    image_form, video_form = _init_upload_forms(request, draft)

    return jingo.render(request, 'gallery/gallery.html',
                        {'media': media,
                         'media_type': media_type,
                         'image_form': image_form,
                         'video_form': video_form})


@login_required
@require_POST
def upload(request, media_type='image'):
    """Finalizes an uploaded draft.

    We could probably use this same form to handle no-JS fallback, if
    we ever need to support that.

    """
    draft = _get_draft_info(request.user)
    if media_type == 'image' and draft['image']:
        # We're publishing an image draft!
        image_form = _init_media_form(ImageForm, request, draft['image'])
        if image_form.is_valid():
            img = image_form.save()
            # TODO: We can drop this when we start using Redis.
            invalidate = Image.objects.exclude(pk=img.pk)
            if invalidate.exists():
                Image.objects.invalidate(invalidate[0])
            return HttpResponseRedirect(img.get_absolute_url())
        else:
            return gallery(request, media_type='image')
    elif media_type == 'video' and draft['video']:
        # We're publishing a video draft!
        video_form = _init_media_form(VideoForm, request, draft['video'])
        if video_form.is_valid():
            vid = video_form.save()
            # TODO: We can drop this when we start using Redis.
            invalidate = Video.objects.exclude(pk=vid.pk)
            if invalidate.exists():
                Video.objects.invalidate(invalidate[0])
            return HttpResponseRedirect(vid.get_absolute_url())
        else:
            return gallery(request, media_type='video')

    return HttpResponseBadRequest(u'Unrecognized POST request.')

{'fraction_non_alphanumeric': 0.08896451142440447, 'fraction_numerical': 0.004213255550153946, 'mean_word_length': 3.0605263157894735, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6113250', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1690, 'n_words': 478}