from course.models import Course
from group.models import Group
from django.shortcuts import get_object_or_404
from django.http import Http404


def get_context(content_type, context):
    if content_type == 'course':
        return get_object_or_404(Course, id=context)
    elif content_type == 'group':
        return get_object_or_404(Group, id=context)
    else:
        raise Http404


# decorator whom call function_in if user is authenticated, function_out if not
def user_logged(function_in, function_out):
    def toggle(request, *args, **kwargs):
        if request.user.is_authenticated():
            return function_in(request, *args, **kwargs)
        else:
            return function_out(request, *args, **kwargs)
    return toggle
