<commit_before>"""
Views which work with Snippets, allowing them to be added, modified,
rated and viewed according to various criteria.

"""

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from django.views.generic import list_detail
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from cab.forms import SnippetForm
from cab.models import Language, Rating, Snippet, Tag
from bookmarks import base_generic_dict

def add_snippet(request):
    """
    Allows a user to add a Snippet to the database.
    
    Context::
        form
            The form to add the Snippet.
    
    Template::
        cab/add_snippet_form.html
    
    """
    original_id = request.GET.get('oid', None)
    
    if request.method == 'POST':
        form = SnippetForm(request.POST)
        if form.is_valid():
            new_snippet = Snippet(title=form.clean_data['title'],
                                  description=form.clean_data['description'],
                                  code=form.clean_data['code'],
                                  tag_list=form.clean_data['tag_list'],
                                  language_id=form.clean_data['language'],
                                  author=request.user)
            if original_id:
                new_snippet.original_id = original_id
            new_snippet.save()
            return HttpResponseRedirect(new_snippet.get_absolute_url())
    else:
        form = SnippetForm()
    return render_to_response('cab/add_snippet_form.html',
                              { 'form': form },
                              context_instance=RequestContext(request))
add_snippet = login_required(add_snippet)

def download(request, snippet_id):
    """
    Allows a user to download a Snippet's code.
    
    Context::
        None, returns a downloadable file.
    
    Template::
        None, returns a downloadable file.
    
    """
    snippet = get_object_or_404(Snippet, pk=snippet_id)
    response = HttpResponse(snippet.code, mimetype='text/plain')
    response['Content-Disposition'] = 'attachment; filename=%s.%s' % (snippet.id,
                                                                      snippet.language.file_extension)
    response['Content-Type'] = snippet.language.mime_type
    return response

def edit_snippet(request, snippet_id):
    """
    Allows a user to edit an existing Snippet.
    
    Context::
        form
            The form to add the Snippet.
    
        original
            The Snippet being edited.
    
    Template::
        cab/edit_snippet_form.html
    
    """
    snippet = get_object_or_404(Snippet,
                                pk=snippet_id,
                                author__pk=request.user.id)
    if request.method == 'POST':
        form = SnippetForm(request.POST)
        if form.is_valid():
            for field in ['title', 'description', 'code', 'tag_list']:
                setattr(snippet, field, form.clean_data[field])
            snippet.language_id = form.clean_data['language_id']
            snippet.save()
            return HttpResponseRedirect(snippet.get_absolute_url())
    else:
        form = SnippetForm(initial=snippet.__dict__)
    return render_to_response('cab/edit_snippet_form.html',
                              { 'form': form,
                                'original': snippet },
                              context_instance=RequestContext(request))
edit_snippet = login_required(edit_snippet)

def rate_snippet(request, snippet_id):
    """
    Allows a user to rate a Snippet.
    
    Context::
        None, returns a redirect back to the Snippet.<commit_msg>Trying something else<commit_after>"""
Views which work with Snippets, allowing them to be added, modified,
rated and viewed according to various criteria.

"""

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from django.views.generic import list_detail
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from cab.forms import SnippetForm
from cab.models import Language, Rating, Snippet, Tag
from bookmarks import base_generic_dict

def add_snippet(request):
    """
    Allows a user to add a Snippet to the database.
    
    Context::
        form
            The form to add the Snippet.
    
    Template::
        cab/add_snippet_form.html
    
    """
    original_id = request.GET.get('oid', None)
    
    if request.method == 'POST':
        form = SnippetForm(request.POST)
        if form.is_valid():
            new_snippet = Snippet(title=form.clean_data['title'],
                                  description=form.clean_data['description'],
                                  code=form.clean_data['code'],
                                  tag_list=form.clean_data['tag_list'],
                                  language_id=form.clean_data['language'],
                                  author=request.user)
            if original_id:
                new_snippet.original_id = original_id
            new_snippet.save()
            return HttpResponseRedirect(new_snippet.get_absolute_url())
    else:
        form = SnippetForm()
    return render_to_response('cab/add_snippet_form.html',
                              { 'form': form },
                              context_instance=RequestContext(request))
add_snippet = login_required(add_snippet)

def download(request, snippet_id):
    """
    Allows a user to download a Snippet's code.
    
    Context::
        None, returns a downloadable file.
    
    Template::
        None, returns a downloadable file.
    
    """
    snippet = get_object_or_404(Snippet, pk=snippet_id)
    response = HttpResponse(snippet.code, mimetype='text/plain')
    response['Content-Disposition'] = 'attachment; filename=%s.%s' % (snippet.id,
                                                                      snippet.language.file_extension)
    response['Content-Type'] = snippet.language.mime_type
    return response

def edit_snippet(request, snippet_id):
    """
    Allows a user to edit an existing Snippet.
    
    Context::
        form
            The form to add the Snippet.
    
        original
            The Snippet being edited.
    
    Template::
        cab/edit_snippet_form.html
    
    """
    snippet = get_object_or_404(Snippet,
                                pk=snippet_id,
                                author__pk=request.user.id)
    if request.method == 'POST':
        form = SnippetForm(request.POST)
        if form.is_valid():
            for field in ['title', 'description', 'code', 'tag_list']:
                setattr(snippet, field, form.clean_data[field])
            snippet.language_id = form.clean_data['language_id']
            snippet.save()
            return HttpResponseRedirect(snippet.get_absolute_url())
    else:
        form = SnippetForm(initial=dict(snippet.__dict__,
                                        language=snippet.language))
    return render_to_response('cab/edit_snippet_form.html',
                              { 'form': form,
                                'original': snippet },
                              context_instance=RequestContext(request))
edit_snippet = login_required(edit_snippet)

def rate_snippet(request, snippet_id):
    """
    Allows a user to rate a Snippet.
    
    Context::
        None, returns a redirect back to the Snippet.
{'fraction_non_alphanumeric': 0.08558735136547758, 'fraction_numerical': 0.002352018816150529, 'mean_word_length': 1.9956947162426615, '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': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7175419', 'n_tokens_mistral': 2071, 'n_tokens_neox': 2016, 'n_words': 476}