<commit_before>from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import Http404,HttpResponse,HttpResponseRedirect
from django.contrib.auth.decorators import permission_required
from django.utils import simplejson
from forms import TeamForm, TeamEntryForm, ScratchForm
from errors import *
from models import *
import tab_logic
from datetime import datetime

def view_teams(request):
    c_teams = [(t.pk,t.name) for t in Team.objects.all().order_by("name")]
    return render_to_response('list_data.html', 
                             {'item_type':'team',
                              'title': "Viewing All Teams",
                              'item_list':c_teams}, 
                              context_instance=RequestContext(request))

def view_team(request, team_id):
    team_id = int(team_id)
    try:
        team = Team.objects.get(pk=team_id)
        stats = []
        stats.append(("Wins", tab_logic.tot_wins(team)))
        stats.append(("Total Speaks", tab_logic.tot_speaks(team)))
        stats.append(("Govs",tab_logic.num_govs(team)))
        stats.append(("Opps",tab_logic.num_opps(team)))
    except Team.DoesNotExist:
        return render_to_response('error.html', 
                                 {'error_type': "View Team",
                                  'error_name': str(team_id),
                                  'error_info':"No such Team"}, 
                                  context_instance=RequestContext(request))
    if request.method == 'POST':
        form = TeamForm(request.POST,instance=team)
        if form.is_valid():
            try:
               form.save()
            except ValueError:
                return render_to_response('error.html', 
                                         {'error_type': "Team",
                                          'error_name': "["+form.cleaned_data['name']+"]",
                                          'error_info':"Team name cannot be validated, most likely a non-existent team"}, 
                                          context_instance=RequestContext(request))
            return render_to_response('thanks.html', 
                                     {'data_type': "Team",
                                      'data_name': "["+form.cleaned_data['name']+"]"}, 
                                      context_instance=RequestContext(request))
    else:
        form = TeamForm(instance=team)
        links = [('/team/'+str(team_id)+'/scratches/view/','Scratches for '+str(team.name), False),
                 ('/team/'+str(team_id)+'/delete/', 'Delete', True)]
        for deb in team.debaters.all():
            links.append(('/debater/'+str(deb.id)+'/', "View %s" % deb.name, False))
        return render_to_response('data_entry.html', 
                                 {'title':"Viewing Team: %s"%(team.name),
                                  'form': form,
                                  'links': links,
                                  'team_obj':team,
                                  'team_stats':stats}, 
                                  context_instance=RequestContext(request))
    
    return render_to_response('data_entry.html', 
                             {'form': form}, 
                             context_instance=RequestContext(request))
    
def enter_team(request):
    if request.method == 'POST':
        form = TeamEntryForm(request.POST)
        if form.is_valid():
            try:
                team = form.save()
            except ValueError:
                return render_to_response('error.html', 
                                         {'error_type': "Team",'error_name': "["+form.cleaned_data['name']+"]",
                                          'error_info':"Team name cannot be validated, most likely a duplicate school"}, 
                                          context_instance=RequestContext(request))
            num_forms = form.cleaned_data['number_scratches']
            return HttpResponseRedirect('/team/'+str(team.pk)+'/scratches/add/'+str(num_forms))
    else:
        form = TeamEntryForm()
    return render_to_response('data_entry.html',
                             {'form': form, 'title': "Create Team"},
                              context_instance=RequestContext(request))
    
@permission_required('tab.team.can_delete', login_url="/403/")                               
def delete_team(request, team_id):
    team_id = int(team_id)
    try :
        team = Team.objects.get(pk=team_id)
        team.delete()
    except Team.DoesNotExist:
        return render_to_response('error.html', 
                                 {'error_type': "Team",
                                 'error_name': str(team_id),
                                 'error_info':"Team does not exist"}, 
                                 context_instance=RequestContext(request))
    return render_to_response('thanks.html', 
                             {'data_type': "Team",
                              'data_name': "["+str(team_id)+"]",
                              'data_modification': 'DELETED'}, 
                              context_instance=RequestContext(request))

def add_scratches(request, team_id, number_scratches):
    try:
        team_id,number_scratches = int(team_id),int(number_scratches)
    except ValueError:
        return render_to_response('error.html', 
                                 {'error_type': "Scratch",'error_name': "Data Entry",
                                  'error_info':"I require INTEGERS!"}, 
                                  context_instance=RequestContext(request))
    try:
        team = Team.objects.get(pk=team_id)
    except Team.DoesNotExist:
        return render_to_response('error.html', 
                                 {'error_type': "Add Scratches for Team",
                                  'error_name': str(team_id),
                                  'error_info':"No such Team"}, 
                                  context_instance=RequestContext(request))
        
    if request.method == 'POST':
        forms = [ScratchForm(request.POST, prefix=str(i)) for i in range(1,number_scratches+1)]
        all_good = True
        for form in forms:
            all_good = all_good and form.is_valid()
        if all_good:
            for form in forms:
                form.save()
            return render_to_response('thanks.html', 
                                     {'data_type': "Scratches for team",
                                      'data_name': "["+str(team_id)+"]",
                                      'data_modification': "CREATED"},
                                      context_instance=RequestContext(request))            
    else:
        forms = [ScratchForm(prefix=str(i), initial={'team':team_id,'scratch_type':0}) for i in range(1,number_scratches+1)]
    return render_to_response('data_entry_multiple.html', 
                             {'forms': zip(forms,[None]*len(forms)),
                              'data_type':'Scratch',
                              'title':"Adding Scratch(es) for %s"%(team.name)}, 
                              context_instance=RequestContext(request))
    
def view_scratches(request, team_id):
    try:
        team_id = int(team_id)
    except ValueError:
        return render_to_response('error.html', 
                                 {'error_type': "Scratch",'error_name': "Delete",
                                  'error_info':"I require INTEGERS!"}, 
                                  context_instance=RequestContext(request))
    scratches = Scratch.objects.filter(team=team_id)
    number_scratches = len(scratches)
    team = Team.objects.get(pk=team_id)
    if request.method == 'POST':
        forms = [ScratchForm(request.POST, prefix=str(i),instance=scratches[i-1]) for i in range(1,number_scratches+1)]
        all_good = True
        for form in forms:
            all_good = all_good and form.is_valid()
        if all_good:
            for form in forms:
                form.save()
            return render_to_response('thanks.html', 
                                     {'data_type': "Scratches for team",
                                      'data_name': "["+str(team_id)+"]",
                                      'data_modification': "EDITED"},
                                      context_instance=RequestContext(request))  
    else:
        forms = [ScratchForm(prefix=str(i), instance=scratches[i-1]) for i in range(1,len(scratches)+1)]
    delete_links = ["/team/"+str(team_id)+"/scratches/delete/"+str(scratches[i].id) for i in range(len(scratches))]
    links = [('/team/'+str(team_id)+'/scratches/add/1/','Add Scratch', False)]
    return render_to_response('data_entry_multiple.html', 
                             {'forms': zip(forms,delete_links),
                              'data_type':'Scratch',
                              'links':links,
                              'title':"Viewing Scratch Information for %s"%(team.name)}, 
                              context_instance=RequestContext(request))

def all_tab_cards(request):
    all_teams = Team.objects.all()<commit_msg>Require permissions to view all tab cards<commit_after>from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import Http404,HttpResponse,HttpResponseRedirect
from django.contrib.auth.decorators import permission_required
from django.utils import simplejson
from forms import TeamForm, TeamEntryForm, ScratchForm
from errors import *
from models import *
import tab_logic
from datetime import datetime

def view_teams(request):
    c_teams = [(t.pk,t.name) for t in Team.objects.all().order_by("name")]
    return render_to_response('list_data.html', 
                             {'item_type':'team',
                              'title': "Viewing All Teams",
                              'item_list':c_teams}, 
                              context_instance=RequestContext(request))

def view_team(request, team_id):
    team_id = int(team_id)
    try:
        team = Team.objects.get(pk=team_id)
        stats = []
        stats.append(("Wins", tab_logic.tot_wins(team)))
        stats.append(("Total Speaks", tab_logic.tot_speaks(team)))
        stats.append(("Govs",tab_logic.num_govs(team)))
        stats.append(("Opps",tab_logic.num_opps(team)))
    except Team.DoesNotExist:
        return render_to_response('error.html', 
                                 {'error_type': "View Team",
                                  'error_name': str(team_id),
                                  'error_info':"No such Team"}, 
                                  context_instance=RequestContext(request))
    if request.method == 'POST':
        form = TeamForm(request.POST,instance=team)
        if form.is_valid():
            try:
               form.save()
            except ValueError:
                return render_to_response('error.html', 
                                         {'error_type': "Team",
                                          'error_name': "["+form.cleaned_data['name']+"]",
                                          'error_info':"Team name cannot be validated, most likely a non-existent team"}, 
                                          context_instance=RequestContext(request))
            return render_to_response('thanks.html', 
                                     {'data_type': "Team",
                                      'data_name': "["+form.cleaned_data['name']+"]"}, 
                                      context_instance=RequestContext(request))
    else:
        form = TeamForm(instance=team)
        links = [('/team/'+str(team_id)+'/scratches/view/','Scratches for '+str(team.name), False),
                 ('/team/'+str(team_id)+'/delete/', 'Delete', True)]
        for deb in team.debaters.all():
            links.append(('/debater/'+str(deb.id)+'/', "View %s" % deb.name, False))
        return render_to_response('data_entry.html', 
                                 {'title':"Viewing Team: %s"%(team.name),
                                  'form': form,
                                  'links': links,
                                  'team_obj':team,
                                  'team_stats':stats}, 
                                  context_instance=RequestContext(request))
    
    return render_to_response('data_entry.html', 
                             {'form': form}, 
                             context_instance=RequestContext(request))
    
def enter_team(request):
    if request.method == 'POST':
        form = TeamEntryForm(request.POST)
        if form.is_valid():
            try:
                team = form.save()
            except ValueError:
                return render_to_response('error.html', 
                                         {'error_type': "Team",'error_name': "["+form.cleaned_data['name']+"]",
                                          'error_info':"Team name cannot be validated, most likely a duplicate school"}, 
                                          context_instance=RequestContext(request))
            num_forms = form.cleaned_data['number_scratches']
            return HttpResponseRedirect('/team/'+str(team.pk)+'/scratches/add/'+str(num_forms))
    else:
        form = TeamEntryForm()
    return render_to_response('data_entry.html',
                             {'form': form, 'title': "Create Team"},
                              context_instance=RequestContext(request))
    
@permission_required('tab.team.can_delete', login_url="/403/")                               
def delete_team(request, team_id):
    team_id = int(team_id)
    try :
        team = Team.objects.get(pk=team_id)
        team.delete()
    except Team.DoesNotExist:
        return render_to_response('error.html', 
                                 {'error_type': "Team",
                                 'error_name': str(team_id),
                                 'error_info':"Team does not exist"}, 
                                 context_instance=RequestContext(request))
    return render_to_response('thanks.html', 
                             {'data_type': "Team",
                              'data_name': "["+str(team_id)+"]",
                              'data_modification': 'DELETED'}, 
                              context_instance=RequestContext(request))

def add_scratches(request, team_id, number_scratches):
    try:
        team_id,number_scratches = int(team_id),int(number_scratches)
    except ValueError:
        return render_to_response('error.html', 
                                 {'error_type': "Scratch",'error_name': "Data Entry",
                                  'error_info':"I require INTEGERS!"}, 
                                  context_instance=RequestContext(request))
    try:
        team = Team.objects.get(pk=team_id)
    except Team.DoesNotExist:
        return render_to_response('error.html', 
                                 {'error_type': "Add Scratches for Team",
                                  'error_name': str(team_id),
                                  'error_info':"No such Team"}, 
                                  context_instance=RequestContext(request))
        
    if request.method == 'POST':
        forms = [ScratchForm(request.POST, prefix=str(i)) for i in range(1,number_scratches+1)]
        all_good = True
        for form in forms:
            all_good = all_good and form.is_valid()
        if all_good:
            for form in forms:
                form.save()
            return render_to_response('thanks.html', 
                                     {'data_type': "Scratches for team",
                                      'data_name': "["+str(team_id)+"]",
                                      'data_modification': "CREATED"},
                                      context_instance=RequestContext(request))            
    else:
        forms = [ScratchForm(prefix=str(i), initial={'team':team_id,'scratch_type':0}) for i in range(1,number_scratches+1)]
    return render_to_response('data_entry_multiple.html', 
                             {'forms': zip(forms,[None]*len(forms)),
                              'data_type':'Scratch',
                              'title':"Adding Scratch(es) for %s"%(team.name)}, 
                              context_instance=RequestContext(request))
    
def view_scratches(request, team_id):
    try:
        team_id = int(team_id)
    except ValueError:
        return render_to_response('error.html', 
                                 {'error_type': "Scratch",'error_name': "Delete",
                                  'error_info':"I require INTEGERS!"}, 
                                  context_instance=RequestContext(request))
    scratches = Scratch.objects.filter(team=team_id)
    number_scratches = len(scratches)
    team = Team.objects.get(pk=team_id)
    if request.method == 'POST':
        forms = [ScratchForm(request.POST, prefix=str(i),instance=scratches[i-1]) for i in range(1,number_scratches+1)]
        all_good = True
        for form in forms:
            all_good = all_good and form.is_valid()
        if all_good:
            for form in forms:
                form.save()
            return render_to_response('thanks.html', 
                                     {'data_type': "Scratches for team",
                                      'data_name': "["+str(team_id)+"]",
                                      'data_modification': "EDITED"},
                                      context_instance=RequestContext(request))  
    else:
        forms = [ScratchForm(prefix=str(i), instance=scratches[i-1]) for i in range(1,len(scratches)+1)]
    delete_links = ["/team/"+str(team_id)+"/scratches/delete/"+str(scratches[i].id) for i in range(len(scratches))]
    links = [('/team/'+str(team_id)+'/scratches/add/1/','Add Scratch', False)]
    return render_to_response('data_entry_multiple.html', 
                             {'forms': zip(forms,delete_links),
                              'data_type':'Scratch',
                              'links':links,
                              'title':"Viewing Scratch Information for %s"%(team.name)}, 
                              context_instance=RequestContext(request))

@permission_required('tab.tab_settings.can_change', login_url="/403/")
def all_tab_cards(request):
    all_teams = Team.objects.all()
{'fraction_non_alphanumeric': 0.1154075924618476, 'fraction_numerical': 0.0021180687557703797, 'mean_word_length': 1.6137686302342087, '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': 98, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '77757', 'n_tokens_mistral': 4942, 'n_tokens_neox': 4705, 'n_words': 855}