<commit_before>from django.shortcuts import render
from forms.models import Question
from .models import Nomination, NominationInstance, UserProfile, Post, Club
from django.contrib.auth.decorators import login_required
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse, reverse_lazy
from django.core.exceptions import ObjectDoesNotExist
from .forms import NominationForm, PostForm, ConfirmApplication, ClubForm, CommentForm, UserId
from forms.models import Questionnaire
import json
from .filters import NominationFilter
from django.core.exceptions import ObjectDoesNotExist

def index(request):
    if request.user.is_authenticated:
        filter = NominationFilter(request.GET, queryset=Nomination.objects.filter(status='Nomination out').order_by('-opening_date'))
        posts = Post.objects.filter(post_holders=request.user)
        username = UserProfile.objects.get(user=request.user)
        return render(request, 'index1.html', context={'posts': posts, 'username': username,'filter':filter})
    else:
        return HttpResponseRedirect(reverse('login'))

@login_required
def club_list(request):
    admin_club = Club.objects.get(club_name="Admin")
    clubs = Club.objects.filter(club_parent=admin_club)

    return render(request, 'clubs/council_clubs.html', context={'clubs': clubs})


@login_required
def club_view(request, pk):
    club = Club.objects.get(pk=pk)
    child_clubs = Club.objects.filter(club_parent=club)
    child_clubs_reverse = child_clubs[::-1]
    posts = Post.objects.filter(post_holders=request.user)

    club_approvals = Club.objects.filter(club_approvals=club).filter(status='Club created')

    return render(request, 'club.html', context={'club': club, 'child_clubs': child_clubs_reverse,
                                                 'posts': posts, 'club_approval': club_approvals})


@login_required
def post_view(request, pk):
    post = Post.objects.get(pk=pk)
    child_posts = Post.objects.filter(parent=post)
    child_posts_reverse = child_posts[::-1]

    post_approvals = Post.objects.filter(post_approvals=post).filter(status='Post created')
    nomi_approvals = Nomination.objects.filter(nomi_approvals=post).filter(status='Nomination created')

    if request.user in post.post_holders.all():
        return render(request, 'post1.html', context={'post': post, 'child_posts': child_posts_reverse,
                                                      'post_approval': post_approvals, 'nomi_approval': nomi_approvals})
    else:
        return render(request, 'no_access.html')


@login_required
def club_create(request, pk):
    if request.method == 'POST':
        parent = Club.objects.get(pk=pk)
        club_form = ClubForm(request.POST)

        if club_form.is_valid():
            club = Club.objects.create(club_name=club_form.cleaned_data['club_name'], club_parent=parent)
            club_pk = club.pk

            return HttpResponseRedirect(reverse('index'))

    else:
        club_form = ClubForm()

    return render(request, 'nomi/club_form.html', context={'form': club_form})


@login_required
def post_create(request, pk):
    parent = Post.objects.get(pk=pk)
    if request.method == 'POST':
        post_form = PostForm(request.POST)
        club = parent.club
        if post_form.is_valid():
            post = Post.objects.create(post_name=post_form.cleaned_data['post_title'], club=club, parent=parent)
            post_pk = post.pk

            return HttpResponseRedirect(reverse('nomi_create', kwargs={'pk': post_pk}))

    else:
        post_form = PostForm()

    if  request.user in parent.post_holders.all():
        return render(request, 'nomi/post_form.html', context={'form': post_form})
    else:
        return render(request,'no_access.html')


@login_required
def child_club_view(request, pk, view_pk):
    club = Club.objects.get(pk=pk)

    if club.status == 'Club created':
        approved = 1
    else:
        approved = 0

    view_pk = view_pk
    view = Club.objects.get(pk=view_pk)

    if view.perms == 'normal':
        power_to_approve = 0
    else:
        power_to_approve = 1

    view_parent = Club.objects.get(pk=view.club_parent.pk)

    if view_parent in club.club_approvals.all():
        approval = 1
    else:
        approval = 0

    return render(request, 'child_club.html', {'club': club, 'view_pk': view_pk, 'ap': approved,
                                               'approval': approval, 'power_to_approve': power_to_approve})


@login_required
def child_post_view(request, pk, view_pk):
    post = Post.objects.get(pk=pk)
    nominations = Nomination.objects.filter(nomi_post=post)

    if post.status == 'Post created':
        approved = 1
    else:
        approved = 0

    view_pk = view_pk
    view = Post.objects.get(pk=view_pk)

    if view.perms == 'normal':
        power_to_approve = 0
    else:
        power_to_approve = 1

    view_parent = Post.objects.get(pk=view.parent.pk)

    if view_parent in post.post_approvals.all():
        approval = 1
    else:
        approval = 0


    access = False
    for pt in post.post_approvals.all():
        if request.user in pt.post_holders.all():
            access=True
            break

    if access or request.user in post.parent.post_holders.all():

        form = UserId(request.POST or None)
        info = 0
        if form.is_valid():
            try:
                userp = UserProfile.objects.get(roll_no=form.cleaned_data['user_roll'])
            except ObjectDoesNotExist:
                userp = None
            if userp:
                post.post_holders.add(userp.user)
                info = 'successfully added'
            else:
                info = "no such user"
            return render(request, 'child_post1.html', {'post': post, 'view_pk': view_pk, 'ap': approved,
                                                        'approval': approval, 'power_to_approve': power_to_approve,
                                                        'nominations': nominations,'form':form,'info':info})



        return render(request, 'child_post1.html', {'post': post, 'view_pk': view_pk, 'ap': approved,
                                               'approval': approval, 'power_to_approve': power_to_approve,
                                               'nominations': nominations,'form':form,'info':info})
    else:
        return render(request,'no_access.html')



@login_required<commit_msg>removed use of view pk passed in url<commit_after>from django.shortcuts import render
from forms.models import Question
from .models import Nomination, NominationInstance, UserProfile, Post, Club
from django.contrib.auth.decorators import login_required
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse, reverse_lazy
from django.core.exceptions import ObjectDoesNotExist
from .forms import NominationForm, PostForm, ConfirmApplication, ClubForm, CommentForm, UserId
from forms.models import Questionnaire
import json
from .filters import NominationFilter
from django.core.exceptions import ObjectDoesNotExist

def index(request):
    if request.user.is_authenticated:
        filter = NominationFilter(request.GET, queryset=Nomination.objects.filter(status='Nomination out').order_by('-opening_date'))
        posts = Post.objects.filter(post_holders=request.user)
        username = UserProfile.objects.get(user=request.user)
        return render(request, 'index1.html', context={'posts': posts, 'username': username,'filter':filter})
    else:
        return HttpResponseRedirect(reverse('login'))

@login_required
def club_list(request):
    admin_club = Club.objects.get(club_name="Admin")
    clubs = Club.objects.filter(club_parent=admin_club)

    return render(request, 'clubs/council_clubs.html', context={'clubs': clubs})


@login_required
def club_view(request, pk):
    club = Club.objects.get(pk=pk)
    child_clubs = Club.objects.filter(club_parent=club)
    child_clubs_reverse = child_clubs[::-1]
    posts = Post.objects.filter(post_holders=request.user)

    club_approvals = Club.objects.filter(club_approvals=club).filter(status='Club created')

    return render(request, 'club.html', context={'club': club, 'child_clubs': child_clubs_reverse,
                                                 'posts': posts, 'club_approval': club_approvals})


@login_required
def post_view(request, pk):
    post = Post.objects.get(pk=pk)
    child_posts = Post.objects.filter(parent=post)
    child_posts_reverse = child_posts[::-1]

    post_approvals = Post.objects.filter(post_approvals=post).filter(status='Post created')
    nomi_approvals = Nomination.objects.filter(nomi_approvals=post).filter(status='Nomination created')

    if request.user in post.post_holders.all():
        return render(request, 'post1.html', context={'post': post, 'child_posts': child_posts_reverse,
                                                      'post_approval': post_approvals, 'nomi_approval': nomi_approvals})
    else:
        return render(request, 'no_access.html')


@login_required
def club_create(request, pk):
    if request.method == 'POST':
        parent = Club.objects.get(pk=pk)
        club_form = ClubForm(request.POST)

        if club_form.is_valid():
            club = Club.objects.create(club_name=club_form.cleaned_data['club_name'], club_parent=parent)
            club_pk = club.pk

            return HttpResponseRedirect(reverse('index'))

    else:
        club_form = ClubForm()

    return render(request, 'nomi/club_form.html', context={'form': club_form})


@login_required
def post_create(request, pk):
    parent = Post.objects.get(pk=pk)
    if request.method == 'POST':
        post_form = PostForm(request.POST)
        club = parent.club
        if post_form.is_valid():
            post = Post.objects.create(post_name=post_form.cleaned_data['post_title'], club=club, parent=parent)
            post_pk = post.pk

            return HttpResponseRedirect(reverse('nomi_create', kwargs={'pk': post_pk}))

    else:
        post_form = PostForm()

    if  request.user in parent.post_holders.all():
        return render(request, 'nomi/post_form.html', context={'form': post_form})
    else:
        return render(request,'no_access.html')


@login_required
def child_club_view(request, pk, view_pk):
    club = Club.objects.get(pk=pk)

    if club.status == 'Club created':
        approved = 1
    else:
        approved = 0

    view_pk = view_pk
    view = Club.objects.get(pk=view_pk)

    if view.perms == 'normal':
        power_to_approve = 0
    else:
        power_to_approve = 1

    view_parent = Club.objects.get(pk=view.club_parent.pk)

    if view_parent in club.club_approvals.all():
        approval = 1
    else:
        approval = 0

    return render(request, 'child_club.html', {'club': club, 'view_pk': view_pk, 'ap': approved,
                                               'approval': approval, 'power_to_approve': power_to_approve})


@login_required
def child_post_view(request, pk):
    post = Post.objects.get(pk=pk)
    nominations = Nomination.objects.filter(nomi_post=post)

    access = False
    view=0
    for pt in post.post_approvals.all():
        if request.user in pt.post_holders.all():
            access=True
            view=pt
            break

    if access or request.user in post.parent.post_holders.all():
        if post.status == 'Post created':
            approved = 1
        else:
            approved = 0

        if view.perms == 'normal':
            power_to_approve = 0
        else:
            power_to_approve = 1

        view_parent = Post.objects.get(pk=view.parent.pk)

        if view_parent in post.post_approvals.all():
            approval = 1
        else:
            approval = 0

        form = UserId(request.POST or None)
        info = 0
        if form.is_valid():
            try:
                userp = UserProfile.objects.get(roll_no=form.cleaned_data['user_roll'])
            except ObjectDoesNotExist:
                userp = None
            if userp:
                post.post_holders.add(userp.user)
                info = 'successfully added'
            else:
                info = "no such user"
            return render(request, 'child_post1.html', {'post': post,'ap': approved,
                                                        'approval': approval, 'power_to_approve': power_to_approve,
                                                        'nominations': nominations,'form':form,'info':info})



        return render(request, 'child_post1.html', {'post': post,'ap': approved,
                                               'approval': approval, 'power_to_approve': power_to_approve,
                                               'nominations': nominations,'form':form,'info':info})
    else:
        return render(request,'no_access.html')



@login_required
{'fraction_non_alphanumeric': 0.10950920245398774, 'fraction_numerical': 0.00299079754601227, 'mean_word_length': 2.726, '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': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4573868', 'n_tokens_mistral': 3853, 'n_tokens_neox': 3752, 'n_words': 789}