<commit_before>"""Views at the configuration root level."""

from django.contrib.auth.models import User
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.views.generic import TemplateView, CreateView, UpdateView
from imager_images.models import Photo, Album
from imager_profile.models import ImagerProfile
from .forms import AlbumForm, UserForm, ImagerProfileForm, UserProfileFormSet
DEFAULT_IMG_URL = 'media/django-magic.jpg'


class HomeView(TemplateView):
    """Generic home view showing public image to any user."""

    template_name = "home.html"

    def get_context_data(self, *args, **kwargs):
        """Return dictionary of context information for home view."""
        img_url = DEFAULT_IMG_URL
        user_photo = Photo.public.random()
        if user_photo:
            img_url = user_photo.img_file.url

        return {'img_url': img_url}


class CreatePhotoView(CreateView):
    """Create a new Photo to the database."""

    model = Photo
    template_name = 'create_obj.html'
    success_url = '/images/library/'
    fields = ['albums', 'img_file', 'title', 'description', 'published']

    def form_valid(self, form):
        """Insert the user from request context into the form as the owner."""
        form.instance.owner = self.request.user
        return super(CreatePhotoView, self).form_valid(form)

    def get_form(self, form_class=None):
        """Return an instance of the form to be used in this view."""
        form = super(CreatePhotoView, self).get_form(form_class=form_class)
        form.fields['albums'].queryset = self.request.user.albums
        return form


class CreateAlbumView(CreateView):
    """Create a new Album to the database."""

    model = Album
    form_class = AlbumForm
    template_name = 'create_obj.html'
    success_url = '/images/library/'

    def form_valid(self, form):
        """Insert the user from request context into the form as the owner."""
        form.instance.owner = self.request.user
        return super(CreateAlbumView, self).form_valid(form)

    def get_form(self, form_class=None):
        """Return an instance of the form to be used in this view."""
        form = super(CreateAlbumView, self).get_form(form_class=form_class)
        form.fields['photos'].queryset = self.request.user.photos
        return form


class EditPhotoView(UpdateView):
    """Allows user to edit their own photos."""

    model = Photo
    template_name = "edit_obj.html"
    success_url = '/images/library/'
    fields = ['albums', 'title', 'description', 'published']

    def get(self, request, *args, **kwargs):
        """Allow only photos belonging to current user as the view queryset."""
        self.queryset = request.user.photos
        return super(EditPhotoView, self).get(request, *args, **kwargs)

    def get_form(self, form_class=None):
        """Return an instance of the form to be used in this view."""
        form = super(EditPhotoView, self).get_form(form_class=form_class)
        form.fields['albums'].queryset = self.request.user.albums
        return form


class EditAlbumView(UpdateView):
    """Allows user to edit their own photos."""

    model = Album
    form_class = AlbumForm
    template_name = "edit_obj.html"
    success_url = '/images/library/'
    fields = ['cover', 'title', 'description', 'published']

    def get(self, request, *args, **kwargs):
        """Allow only albums belonging to current user as the view queryset."""
        self.queryset = request.user.albums
        return super(EditAlbumView, self).get(request, *args, **kwargs)

    def get_form(self, form_class=None):
        """Return an instance of the form to be used in this view."""
        form = super(EditAlbumView, self).get_form(form_class=form_class)
        form.fields['photos'].queryset = self.request.user.photos
        return form


def edit_profile(request):
    """Allow user to edit their ImagerProfile, and limited fields of User."""
    user = request.user
    profile = request.user.profile
    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=user)
        profile_form = ImagerProfileForm(request.POST, instance=profile)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            return HttpResponseRedirect('/profile/')
    user_form = UserForm(instance=user)
    profile_form = ImagerProfileForm(instance=profile)
    context = {'user_form': user_form, 'profile_form': profile_form}
    return render(request, 'edit_profile.html', context)


class EditProfileView(UpdateView):
    """Allow the user to edit their profile."""

    model = User
    form_class = UserProfileFormSet
    template_name = "edit_profile2.html"<commit_msg>Working on consolidating views into one.<commit_after>"""Views at the configuration root level."""

from django.contrib.auth.models import User
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.views.generic import TemplateView, CreateView, UpdateView
from imager_images.models import Photo, Album
from imager_profile.models import ImagerProfile
from .forms import AlbumForm, UserForm, ImagerProfileForm, UserProfileFormSet
DEFAULT_IMG_URL = 'media/django-magic.jpg'


class HomeView(TemplateView):
    """Generic home view showing public image to any user."""

    template_name = "home.html"

    def get_context_data(self, *args, **kwargs):
        """Return dictionary of context information for home view."""
        img_url = DEFAULT_IMG_URL
        user_photo = Photo.public.random()
        if user_photo:
            img_url = user_photo.img_file.url

        return {'img_url': img_url}


#####################
# Under construction consolidated class

class CreateOrEditMixin(object):
    """Flexible class view for creation and editing of albums and photos."""

    template_name = 'create_or_edit.html'
    success_url = '/images/library/'
    own_queryset_name = None
    rel_queryset_name = None

    def get(self, request, *args, **kwargs):
        """Allow only photos belonging to current user as the view queryset."""
        self.queryset = getattr(self.request.user, self.own_queryset_name)
        return super(CreateOrEditMixin, self).get(request, *args, **kwargs)

    def get_form(self, form_class=None):
        """Return an instance of the form to be used in this view."""
        form = super(CreateOrEditMixin, self).get_form(form_class=form_class)
        owner_rel_queryset = getattr(self.request.user, self.rel_queryset_name)
        form.fields[self.rel_queryset_name].queryset = owner_rel_queryset
        return form

    def form_valid(self, form):
        """Insert the user from request context into the form as the owner."""
        form.instance.owner = self.request.user
        return super(CreateOrEditMixin, self).form_valid(form)

#########################


class CreatePhotoView(CreateOrEditMixin, CreateView):
    """Create a new Photo to the database."""

    model = Photo
    # template_name = 'create_obj.html'
    # success_url = '/images/library/'
    fields = ['albums', 'img_file', 'title', 'description', 'published']
    own_queryset_name = 'photos'
    rel_queryset_name = 'albums'

    # def form_valid(self, form):
    #     """Insert the user from request context into the form as the owner."""
    #     form.instance.owner = self.request.user
    #     return super(CreatePhotoView, self).form_valid(form)

    # def get_form(self, form_class=None):
    #     """Return an instance of the form to be used in this view."""
    #     form = super(CreatePhotoView, self).get_form(form_class=form_class)
    #     form.fields['albums'].queryset = self.request.user.albums
    #     return form


class CreateAlbumView(CreateOrEditMixin, CreateView):
    """Create a new Album to the database."""

    model = Album
    form_class = AlbumForm
    own_queryset_name = 'albums'
    rel_queryset_name = 'photos'
    # template_name = 'create_obj.html'
    # success_url = '/images/library/'

    # def form_valid(self, form):
    #     """Insert the user from request context into the form as the owner."""
    #     form.instance.owner = self.request.user
    #     return super(CreateAlbumView, self).form_valid(form)

    # def get_form(self, form_class=None):
    #     """Return an instance of the form to be used in this view."""
    #     form = super(CreateAlbumView, self).get_form(form_class=form_class)
    #     form.fields['photos'].queryset = self.request.user.photos
    #     return form


class EditPhotoView(CreateOrEditMixin, UpdateView):
    """Allows user to edit their own photos."""

    model = Photo
    # template_name = "edit_obj.html"
    # success_url = '/images/library/'
    fields = ['albums', 'title', 'description', 'published']
    own_queryset_name = 'photos'
    rel_queryset_name = 'albums'

    # def get(self, request, *args, **kwargs):
    #     """Allow only photos belonging to current user as the view queryset."""
    #     self.queryset = request.user.photos
    #     return super(EditPhotoView, self).get(request, *args, **kwargs)

    # def get_form(self, form_class=None):
    #     """Return an instance of the form to be used in this view."""
    #     form = super(EditPhotoView, self).get_form(form_class=form_class)
    #     form.fields['albums'].queryset = self.request.user.albums
    #     return form


class EditAlbumView(CreateOrEditMixin, UpdateView):
    """Allows user to edit their own photos."""

    model = Album
    form_class = AlbumForm
    # template_name = "edit_obj.html"
    # success_url = '/images/library/'
    # fields = ['cover', 'title', 'description', 'published']
    own_queryset_name = 'albums'
    rel_queryset_name = 'photos'

    # def get(self, request, *args, **kwargs):
    #     """Allow only albums belonging to current user as the view queryset."""
    #     self.queryset = request.user.albums
    #     return super(EditAlbumView, self).get(request, *args, **kwargs)

    # def get_form(self, form_class=None):
    #     """Return an instance of the form to be used in this view."""
    #     form = super(EditAlbumView, self).get_form(form_class=form_class)
    #     form.fields['photos'].queryset = self.request.user.photos
    #     return form


def edit_profile(request):
    """Allow user to edit their ImagerProfile, and limited fields of User."""
    user = request.user
    profile = request.user.profile
    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=user)
        profile_form = ImagerProfileForm(request.POST, instance=profile)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            return HttpResponseRedirect('/profile/')
    user_form = UserForm(instance=user)
    profile_form = ImagerProfileForm(instance=profile)
    context = {'user_form': user_form, 'profile_form': profile_form}
    return render(request, 'edit_profile.html', context)


class EditProfileView(UpdateView):
    """Allow the user to edit their profile."""

    model = User
    form_class = UserProfileFormSet
    template_name = "edit_profile2.html"
{'fraction_non_alphanumeric': 0.11442521083000444, 'fraction_numerical': 0.0001775410563692854, 'mean_word_length': 3.419772459788152, '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': '6934978', 'n_tokens_mistral': 3275, 'n_tokens_neox': 3189, 'n_words': 908}