<commit_before># Create your views here.
from django.http import HttpResponse, Http404
from django.shortcuts import get_object_or_404, render_to_response
from django.contrib.contenttypes.models import ContentType
from models import get_shareables, share_object_with_groups

def get_shared_content_instance(ctid, pk, user):
    """
     Make sure user owns content and that content is shareable
    """
    try:
        ct = ContentType.objects.get(pk=ctid)
        # shareable?
        assert ct.model_class().__name__.lower() in get_shareables()
        obj = ct.model_class().objects.get(pk=pk, user=user)
        return obj
    except:
        return None
    
def get_sharing_groups(ctid, user):
    """
     Get a list of users groups that have appropriate permissions for this object
    """
    try:
        ct = ContentType.objects.get(pk=ctid)
        model_class, permission = get_shareables()[ct.model_class().__name__.lower()]
        groups = user.groups.filter(permissions=permission)
        return groups
    except:
        return None

def share_form(request, pk, object_type):
    # User must be logged in
    user = request.user
    if user.is_anonymous() or not user.is_authenticated():
        return HttpResponse('You must be logged in', status=401)

    # Make sure user owns content and that content is shareable
    obj = get_shared_content_instance(object_type, pk, user)
    if not obj:
        return HttpResponse("This object is not shareable.", status=404)
    obj_type_verbose = obj._meta.verbose_name

    if request.method == 'GET':
        # Display the form
        # Which groups is this object already shared to?
        already_shared_groups = obj.sharing_groups.all()

        # Get a list of users groups that have appropriate permissions for this object
        groups = get_sharing_groups(object_type, user)
        if not groups:
            return HttpResponse("There are no groups to which you can share your content at this time.", status=404)

        #return HttpResponse("User %s is sharing object type %s with pk %s <br/> %s" % (user, object_type, pk, groups ) )
        return render_to_response('share_form.html', {'groups': groups, 'already_shared_groups': already_shared_groups, 'obj': obj, 
                                                      'obj_type_verbose': obj_type_verbose,  'user':user}) 

    elif request.method == 'POST':
        group_ids = [int(x) for x in request.POST.getlist('sharing_groups')]

        try:
            share_object_with_groups(obj,group_ids)
            if len(group_ids) == 0:
                restext = "<br/><p id='sharing_response'>The %s named %s is now unshared with all groups.</p id='sharing_response'>" % (obj_type_verbose, str(obj))
            else:
                restext = "<br/><p id='sharing_response'>The %s named %s is now shared with groups %s</p id='sharing_response'>" % (obj_type_verbose, str(obj), ','.join([str(x) for x in group_ids]))
            return HttpResponse(restext,status=200)
        except:<commit_msg>fixed unicode bug in response<commit_after># Create your views here.
from django.http import HttpResponse, Http404
from django.shortcuts import get_object_or_404, render_to_response
from django.contrib.contenttypes.models import ContentType
from models import get_shareables, share_object_with_groups

def get_shared_content_instance(ctid, pk, user):
    """
     Make sure user owns content and that content is shareable
    """
    try:
        ct = ContentType.objects.get(pk=ctid)
        # shareable?
        assert ct.model_class().__name__.lower() in get_shareables()
        obj = ct.model_class().objects.get(pk=pk, user=user)
        return obj
    except:
        return None
    
def get_sharing_groups(ctid, user):
    """
     Get a list of users groups that have appropriate permissions for this object
    """
    try:
        ct = ContentType.objects.get(pk=ctid)
        model_class, permission = get_shareables()[ct.model_class().__name__.lower()]
        groups = user.groups.filter(permissions=permission)
        return groups
    except:
        return None

def share_form(request, pk, object_type):
    # User must be logged in
    user = request.user
    if user.is_anonymous() or not user.is_authenticated():
        return HttpResponse('You must be logged in', status=401)

    # Make sure user owns content and that content is shareable
    obj = get_shared_content_instance(object_type, pk, user)
    if not obj:
        return HttpResponse("This object is not shareable.", status=404)
    obj_type_verbose = obj._meta.verbose_name

    if request.method == 'GET':
        # Display the form
        # Which groups is this object already shared to?
        already_shared_groups = obj.sharing_groups.all()

        # Get a list of users groups that have appropriate permissions for this object
        groups = get_sharing_groups(object_type, user)
        if not groups:
            return HttpResponse("There are no groups to which you can share your content at this time.", status=404)

        #return HttpResponse("User %s is sharing object type %s with pk %s <br/> %s" % (user, object_type, pk, groups ) )
        return render_to_response('share_form.html', {'groups': groups, 'already_shared_groups': already_shared_groups, 'obj': obj, 
                                                      'obj_type_verbose': obj_type_verbose,  'user':user}) 

    elif request.method == 'POST':
        group_ids = [int(x) for x in request.POST.getlist('sharing_groups')]

        try:
            share_object_with_groups(obj,group_ids)
            if len(group_ids) == 0:
                restext = "<br/><p id='sharing_response'>The %s named %s is now unshared with all groups.</p id='sharing_response'>" % (obj_type_verbose, unicode(obj))
            else:
                restext = "<br/><p id='sharing_response'>The %s named %s is now shared with groups %s</p id='sharing_response'>" % (obj_type_verbose, unicode(obj), ','.join([str(x) for x in group_ids]))
            return HttpResponse(restext,status=200)
        except:
{'fraction_non_alphanumeric': 0.0973044049967127, 'fraction_numerical': 0.006245890861275477, 'mean_word_length': 2.948734587929916, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5183061', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1704, 'n_words': 531}