<commit_before>import os

from django.conf import settings
from django.contrib import messages
from django.contrib.auth.models import User, Group
from django.core.exceptions import PermissionDenied
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.utils.translation import ugettext as _
from django.views.decorators.http import require_POST, require_http_methods

from kitsune.access.decorators import login_required
from kitsune.groups.forms import (
    GroupProfileForm, GroupAvatarForm, AddUserForm)
from kitsune.groups.models import GroupProfile
from kitsune.sumo.urlresolvers import reverse
from kitsune.sumo.utils import get_next_url
from kitsune.upload.tasks import _create_image_thumbnail


def list(request):
    groups = GroupProfile.objects.select_related('group').all()
    return render(request, 'groups/list.html', {'groups': groups})


def profile(request, group_slug, member_form=None, leader_form=None):
    prof = get_object_or_404(GroupProfile, slug=group_slug)
    leaders = prof.leaders.all().select_related('profile')
    members_list = prof.group.user_set.all().select_related('profile')
    paginator = Paginator(members_list, 30)
    page = request.GET.get('page')
    try:
        members = paginator.page(page)
    except PageNotAnInteger:
        members = paginator.page(1)
    except EmptyPage:
        members = paginator.page(paginator.num_pages)

    is_paginated = paginator.num_pages > 1
    user_can_edit = _user_can_edit(request.user, prof)
    user_can_manage_leaders = _user_can_manage_leaders(request.user, prof)
    return render(request, 'groups/profile.html', {
        'profile': prof, 'leaders': leaders,
        'members': members, 'user_can_edit': user_can_edit,
        'user_can_manage_leaders': user_can_manage_leaders,
        'is_paginated': is_paginated,
        'member_form': member_form or AddUserForm(),
        'leader_form': leader_form or AddUserForm()})


@login_required
@require_http_methods(['GET', 'POST'])
def edit(request, group_slug):
    prof = get_object_or_404(GroupProfile, slug=group_slug)

    if not _user_can_edit(request.user, prof):
        raise PermissionDenied

    form = GroupProfileForm(request.POST or None, instance=prof)
    if request.method == 'POST' and form.is_valid():
        form.save()
        messages.add_message(request, messages.SUCCESS,
                             _('Group information updated successfully!'))
        return HttpResponseRedirect(prof.get_absolute_url())

    return render(request, 'groups/edit.html', {
        'form': form, 'profile': prof})


@login_required
@require_http_methods(['GET', 'POST'])
def edit_avatar(request, group_slug):
    """Edit group avatar."""
    prof = get_object_or_404(GroupProfile, slug=group_slug)

    if not _user_can_edit(request.user, prof):
        raise PermissionDenied

    form = GroupAvatarForm(request.POST or None, request.FILES or None,
                           instance=prof)

    old_avatar_path = None
    if prof.avatar and os.path.isfile(prof.avatar.path):
        # Need to store the path, or else django's
        # form.is_valid() messes with it.
        old_avatar_path = prof.avatar.path
    if request.method == 'POST' and form.is_valid():
        # Upload new avatar and replace old one.
        if old_avatar_path:
            os.unlink(old_avatar_path)

        prof = form.save()
        content = _create_image_thumbnail(prof.avatar.path,
                                          settings.AVATAR_SIZE, pad=True)
        # We want everything as .png
        name = prof.avatar.name + ".png"
        # Delete uploaded avatar and replace with thumbnail.
        prof.avatar.delete()
        prof.avatar.save(name, content, save=True)
        return HttpResponseRedirect(prof.get_absolute_url())

    return render(request, 'groups/edit_avatar.html', {
        'form': form, 'profile': prof})


@login_required
@require_http_methods(['GET', 'POST'])
def delete_avatar(request, group_slug):
    """Delete group avatar."""
    prof = get_object_or_404(GroupProfile, slug=group_slug)

    if not _user_can_edit(request.user, prof):
        raise PermissionDenied

    if request.method == 'POST':
        # Delete avatar here
        if prof.avatar:
            prof.avatar.delete()
        return HttpResponseRedirect(prof.get_absolute_url())
<commit_msg>Ref #2899. Fix uploading of group avatars.<commit_after>import os

from django.conf import settings
from django.contrib import messages
from django.contrib.auth.models import User, Group
from django.core.exceptions import PermissionDenied
from django.core.files.storage import default_storage
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.utils.translation import ugettext as _
from django.views.decorators.http import require_POST, require_http_methods

from kitsune.access.decorators import login_required
from kitsune.groups.forms import (
    GroupProfileForm, GroupAvatarForm, AddUserForm)
from kitsune.groups.models import GroupProfile
from kitsune.sumo.urlresolvers import reverse
from kitsune.sumo.utils import get_next_url
from kitsune.upload.tasks import _create_image_thumbnail


def list(request):
    groups = GroupProfile.objects.select_related('group').all()
    return render(request, 'groups/list.html', {'groups': groups})


def profile(request, group_slug, member_form=None, leader_form=None):
    prof = get_object_or_404(GroupProfile, slug=group_slug)
    leaders = prof.leaders.all().select_related('profile')
    members_list = prof.group.user_set.all().select_related('profile')
    paginator = Paginator(members_list, 30)
    page = request.GET.get('page')
    try:
        members = paginator.page(page)
    except PageNotAnInteger:
        members = paginator.page(1)
    except EmptyPage:
        members = paginator.page(paginator.num_pages)

    is_paginated = paginator.num_pages > 1
    user_can_edit = _user_can_edit(request.user, prof)
    user_can_manage_leaders = _user_can_manage_leaders(request.user, prof)
    return render(request, 'groups/profile.html', {
        'profile': prof, 'leaders': leaders,
        'members': members, 'user_can_edit': user_can_edit,
        'user_can_manage_leaders': user_can_manage_leaders,
        'is_paginated': is_paginated,
        'member_form': member_form or AddUserForm(),
        'leader_form': leader_form or AddUserForm()})


@login_required
@require_http_methods(['GET', 'POST'])
def edit(request, group_slug):
    prof = get_object_or_404(GroupProfile, slug=group_slug)

    if not _user_can_edit(request.user, prof):
        raise PermissionDenied

    form = GroupProfileForm(request.POST or None, instance=prof)
    if request.method == 'POST' and form.is_valid():
        form.save()
        messages.add_message(request, messages.SUCCESS,
                             _('Group information updated successfully!'))
        return HttpResponseRedirect(prof.get_absolute_url())

    return render(request, 'groups/edit.html', {
        'form': form, 'profile': prof})


@login_required
@require_http_methods(['GET', 'POST'])
def edit_avatar(request, group_slug):
    """Edit group avatar."""
    prof = get_object_or_404(GroupProfile, slug=group_slug)

    if not _user_can_edit(request.user, prof):
        raise PermissionDenied

    form = GroupAvatarForm(request.POST or None, request.FILES or None,
                           instance=prof)

    old_avatar_path = None

    if prof.avatar and default_storage.exists(prof.avatar.name):
        # Need to store the path, or else django's
        # form.is_valid() messes with it.
        old_avatar_path = prof.avatar.name

    if request.method == 'POST' and form.is_valid():
        # Upload new avatar and replace old one.
        if old_avatar_path:
            default_storage.delete(old_avatar_path)

        content = _create_image_thumbnail(form.instance.avatar.file, settings.AVATAR_SIZE, pad=True)
        # We want everything as .png
        name = form.instance.avatar.name + ".png"
        prof.avatar.save(name, content, save=True)
        return HttpResponseRedirect(prof.get_absolute_url())

    return render(request, 'groups/edit_avatar.html', {
        'form': form, 'profile': prof})


@login_required
@require_http_methods(['GET', 'POST'])
def delete_avatar(request, group_slug):
    """Delete group avatar."""
    prof = get_object_or_404(GroupProfile, slug=group_slug)

    if not _user_can_edit(request.user, prof):
        raise PermissionDenied

    if request.method == 'POST':
        # Delete avatar here
        if prof.avatar:
            prof.avatar.delete()
        return HttpResponseRedirect(prof.get_absolute_url())

{'fraction_non_alphanumeric': 0.09924693716983253, 'fraction_numerical': 0.004720692368214005, 'mean_word_length': 3.690564048497628, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7212970', 'n_tokens_mistral': 2730, 'n_tokens_neox': 2624, 'n_words': 608}