<commit_before>""" Member views. """

from django.shortcuts import render, get_object_or_404, redirect
from events.models import Ticket
from events.forms import TicketForm
from forms import ProfileForm, ProfilePhotoForm, CroppingImageForm
from forms import UsernameForm
from django.http import Http404
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.views.decorators.http import require_POST
from StringIO import StringIO
from PIL import Image
from django.core.files import File


def require_editing_own_profile(f):
    """ Require that the pk passed is equal to the current user's pk. """
    def inner_require_editing_own_profile(request, pk):
        member = get_object_or_404(User, pk=pk)
        if not member == request.user and not request.user.is_staff:
            raise Http404
        return f(request, pk)
    return login_required(inner_require_editing_own_profile)


@login_required
def my_tickets(request):
    """ List tickets I have purchased. """
    return render(request, "members/my_tickets.html")


@login_required
def edit_ticket(request, pk):
    """ Edit the quantity of tickets. """
    ticket = get_object_or_404(Ticket, pk=pk)

    if not ticket.user == request.user:
        raise Http404

    if not ticket.event.is_future():
        return redirect("my_tickets")

    max_tickets = ticket.event.remaining_tickets + ticket.number + 1

    form = TicketForm(event=ticket.event,
                      initial={"quantity": ticket.number},
                      max_tickets=max_tickets)

    if request.method == "POST":
        form = TicketForm(request.POST,
                          event=ticket.event,
                          max_tickets=max_tickets)
        if form.is_valid():
            ticket.number = form.cleaned_data['quantity']
            ticket.save()
            return redirect("my_tickets")

    return render(request, "members/edit_ticket.html",
                  {"ticket": ticket, "form": form})


@login_required
def cancel_ticket(request, pk):
    """ Cancel a ticket. """
    ticket = get_object_or_404(Ticket, pk=pk)

    if not ticket.user == request.user:
        raise Http404

    if not ticket.event.is_future():
        return redirect("my_tickets")

    if request.method == "POST":
        ticket.cancel()
        return redirect("my_tickets")

    return render(request, "members/cancel_ticket.html", {"ticket": ticket})


def view_profile(request, pk):
    """ View a member's profile. """
    member = get_object_or_404(User, pk=pk)

    return render(request, "members/view_profile.html", {"member": member})


@require_editing_own_profile
def edit_profile(request, pk):
    """ Edit a member's profile. """
    member = get_object_or_404(User, pk=pk)
    form = ProfileForm(
        initial={
            "first_name": member.first_name,
            "last_name": member.last_name,
            "bio": member.profile.bio,
            "show_facebook_urls": member.profile.show_facebook_urls,
            "show_github_urls": member.profile.show_github_urls
        })

    if request.method == "POST":
        form = ProfileForm(request.POST)
        if form.is_valid():
            member.first_name = form.cleaned_data['first_name']
            member.last_name = form.cleaned_data['last_name']
            member.profile.bio = form.cleaned_data['bio']
            member.profile.show_facebook_urls = \
                form.cleaned_data['show_facebook_urls']
            member.profile.show_github_urls = \
                form.cleaned_data['show_github_urls']
            member.profile.show_linkedin_urls = \
                form.cleaned_data['show_linkedin_urls']
            member.profile.show_twitter_urls = \
                form.cleaned_data['show_twitter_urls']
            member.profile.show_google_urls = \
                form.cleaned_data['show_google_urls']
            member.profile.show_stackexchange_urls = \
                form.cleaned_data['show_stackexchange_urls']
            member.profile.show_github_urls = \
                form.cleaned_data['show_github_urls']
<commit_msg>Pre-populate "Show x profiles" on edit profile<commit_after>""" Member views. """

from django.shortcuts import render, get_object_or_404, redirect
from events.models import Ticket
from events.forms import TicketForm
from forms import ProfileForm, ProfilePhotoForm, CroppingImageForm
from forms import UsernameForm
from django.http import Http404
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.views.decorators.http import require_POST
from StringIO import StringIO
from PIL import Image
from django.core.files import File


def require_editing_own_profile(f):
    """ Require that the pk passed is equal to the current user's pk. """
    def inner_require_editing_own_profile(request, pk):
        member = get_object_or_404(User, pk=pk)
        if not member == request.user and not request.user.is_staff:
            raise Http404
        return f(request, pk)
    return login_required(inner_require_editing_own_profile)


@login_required
def my_tickets(request):
    """ List tickets I have purchased. """
    return render(request, "members/my_tickets.html")


@login_required
def edit_ticket(request, pk):
    """ Edit the quantity of tickets. """
    ticket = get_object_or_404(Ticket, pk=pk)

    if not ticket.user == request.user:
        raise Http404

    if not ticket.event.is_future():
        return redirect("my_tickets")

    max_tickets = ticket.event.remaining_tickets + ticket.number + 1

    form = TicketForm(event=ticket.event,
                      initial={"quantity": ticket.number},
                      max_tickets=max_tickets)

    if request.method == "POST":
        form = TicketForm(request.POST,
                          event=ticket.event,
                          max_tickets=max_tickets)
        if form.is_valid():
            ticket.number = form.cleaned_data['quantity']
            ticket.save()
            return redirect("my_tickets")

    return render(request, "members/edit_ticket.html",
                  {"ticket": ticket, "form": form})


@login_required
def cancel_ticket(request, pk):
    """ Cancel a ticket. """
    ticket = get_object_or_404(Ticket, pk=pk)

    if not ticket.user == request.user:
        raise Http404

    if not ticket.event.is_future():
        return redirect("my_tickets")

    if request.method == "POST":
        ticket.cancel()
        return redirect("my_tickets")

    return render(request, "members/cancel_ticket.html", {"ticket": ticket})


def view_profile(request, pk):
    """ View a member's profile. """
    member = get_object_or_404(User, pk=pk)

    return render(request, "members/view_profile.html", {"member": member})


@require_editing_own_profile
def edit_profile(request, pk):
    """ Edit a member's profile. """
    member = get_object_or_404(User, pk=pk)
    form = ProfileForm(
        initial={
            "first_name": member.first_name,
            "last_name": member.last_name,
            "bio": member.profile.bio,
            "show_facebook_urls": member.profile.show_facebook_urls,
            "show_github_urls": member.profile.show_github_urls,
            "show_linkedin_urls": member.profile.show_linkedin_urls,
            "show_twitter_urls": member.profile.show_twitter_urls,
            "show_google_urls": member.profile.show_google_urls,
            "show_stackexchange_urls": member.profile.show_stackexchange_urls,
        })

    if request.method == "POST":
        form = ProfileForm(request.POST)
        if form.is_valid():
            member.first_name = form.cleaned_data['first_name']
            member.last_name = form.cleaned_data['last_name']
            member.profile.bio = form.cleaned_data['bio']
            member.profile.show_facebook_urls = \
                form.cleaned_data['show_facebook_urls']
            member.profile.show_github_urls = \
                form.cleaned_data['show_github_urls']
            member.profile.show_linkedin_urls = \
                form.cleaned_data['show_linkedin_urls']
            member.profile.show_twitter_urls = \
                form.cleaned_data['show_twitter_urls']
            member.profile.show_google_urls = \
                form.cleaned_data['show_google_urls']
            member.profile.show_stackexchange_urls = \
                form.cleaned_data['show_stackexchange_urls']

{'fraction_non_alphanumeric': 0.10054219707685054, 'fraction_numerical': 0.007307873644507308, 'mean_word_length': 2.777827248441674, 'pattern_counts': {'":': 30, '<': 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': '2707967', 'n_tokens_mistral': 2574, 'n_tokens_neox': 2488, 'n_words': 514}