def prerender_profile_widget(request):

    local_user = request.user

    real_person = Person.objects.get_subclass(user=local_user.id)

    # If local_person is a Student
    if isinstance(real_person, Student):

        local_student = get_object_or_404(Student, pk=local_user.id)

        # We get the xp to calculate the current level for the navbar
        total_xp = local_student.xp

        current_level = qubal_xp.calculate_level(total_xp)

        current_xp = qubal_xp.calculate_current_xp(total_xp)

        xp_needed = Rules_Xp_per_Level.objects.get(level=current_level).xp

        background_img = prerender_profile_widget_background(local_student)
        background_color = prerender_profile_widget_background_color(local_student)

        context = {
            "student": local_student,
            "current_level": current_level,
            "current_xp": current_xp,
            "xp_needed": xp_needed,
            "background_img": background_img,
            "background_color": background_color,
            "SUNRISE_URL": settings.SUNRISE_URL,
        }

        profile_widget_block = render(request, "jawa_qubal/jawa_profile_widget.inc", context)
        profile_widget_block = str(profile_widget_block)
        fixed_profile_widget_block = profile_widget_block.replace("Content-Type: text/html; charset=utf-8", "")

        return fixed_profile_widget_block
def prerender_ajax_dashboard(request):

    if request.user.is_authenticated():

        local_user = request.user

        # we need to check the type of local_person (Student or Teacher)
        local_person = request.user

        # we do it with the fancy django_model_utils lib
        # we ask the parent class Person which kind of instance is the user logged_in (teacher or student)

        # Check for avoiding a crash on the user not being a person YET!
        if Person.objects.filter(user=local_person.id):

            # We check the login_rule to add XP to the user if last action is logged.
            qubal_rules.login_rule(local_user)

            real_person = Person.objects.get_subclass(user=local_person.id)

            # We ask if it is student or not

            if isinstance(real_person, Student):
                # we know the user is a student

                # Student.objects.get(pk=local_person.id):

                local_student = get_object_or_404(Student, pk=local_person.id)

                total_xp = local_student.xp

                current_xp = qubal_xp.calculate_current_xp(total_xp)

                current_level = qubal_xp.calculate_level(total_xp)

                xp_needed = Rules_Xp_per_Level.objects.get(level=current_level).xp

                teams_list = local_student.is_team_member_of.all()[:3]

                achievement_list = local_student.has_achievements.all().order_by("id")
                achievement_list = achievement_list.reverse()[1:4]

                last_achievement_list = local_student.has_achievements.all().order_by("id")
                last_achievement_list = last_achievement_list.reverse()[:1]

                # We get the specific student powers and assign it to the student_powers var
                # to send it as context to the index.html

                student_powers = local_student.has_powers

                percent = current_level * 10

                percent_xp = total_xp * 100 / qubal_xp.sumatorium_levels()

                if current_xp != 0:
                    percent_current_xp = current_xp * 100 / xp_needed
                else:
                    percent_current_xp = 0

                    # Check for avoid a crash when the image doesn't exist

                if not os.path.isfile(settings.MEDIA_ROOT + str(local_student.image)):

                    local_student.image = ""

                next_level = current_level + 1
                next_level_at = xp_needed - current_xp

                url = "dashboard_ajax.html"

                html = prerender_nav(local_student, url, current_level, settings, request)

                notifications_script = prerender_notifications(local_student)
                # source_file = open(local_student.image.url,'r')
                # image_generator = Thumbnail(local_student.image)
                # result = image_generator.generate()

                context = {
                    "student": local_student,
                    "current_level": current_level,
                    "current_xp": current_xp,
                    "xp_needed_for_level_up": xp_needed,
                    "total_xp": total_xp,
                    "student_teams_list": teams_list,
                    "local_person_id": local_person.id,
                    "student_achievement_list": achievement_list,
                    "percent": percent,
                    "percent_current_xp": percent_current_xp,
                    "last_achievement_list": last_achievement_list,
                    "next_level_at": next_level_at,
                    "next_level": next_level,
                    "student_powers": student_powers,
                    "navbar_content": html,
                    "notifications_content": notifications_script,
                    "SUNRISE_URL": settings.SUNRISE_URL,
                    "QUBAL_VERSION": settings.QUBAL_VERSION,
                }

                return render(request, "spex_qubal/dashboard_ajax.html", context)

            elif isinstance(real_person, Teacher):
                # we know the user is a teacher

                local_teacher = get_object_or_404(Teacher, pk=local_person.id)

                #####
                # Check for avoid a crash when the image doesn't exist

                if not os.path.isfile(settings.MEDIA_ROOT + str(local_teacher.image)):

                    local_teacher.image = ""

                    #####

                total_xp = local_teacher.xp

                current_level = calculate_level(total_xp)

                url = "spex_index.html"

                html = prerender_nav(local_teacher, url, current_level, settings, request)

                context = {
                    "teacher": local_teacher,
                    "navbar_content": html,
                    "SUNRISE_URL": settings.SUNRISE_URL,
                    "QUBAL_VERSION": settings.QUBAL_VERSION,
                }

                return render(request, "spex_qubal/index_teacher.html", context)

            else:
                # we know you're a user, but you've no type!

                return render(request, "Not yet done...and you are an unknown entity sneaking around!!!!!")

        else:

            # we know you're a user, and not yet decided if student or teacher
            # return  HttpResponse("You should choose what to be!!! you're just thi user: "******"/register_character_landing/")

    else:  # Drop him to landing if he's not authenticated
        return HttpResponseRedirect("/landing/")
def prerender_ajax_course_listing(request):

    if request.user.is_authenticated():

        local_person = request.user

        real_person = Person.objects.get_subclass(user=local_person.id)

        if isinstance(real_person, Student):

            if Student.objects.get(pk=local_person.id):

                local_student = get_object_or_404(Student, pk=local_person.id)
                local_student_courses = local_student.active_courses.all().order_by("-starting_date")

                course_list = Course.objects.all().order_by("-starting_date")[:10]

                total_xp = local_student.xp
                current_level = qubal_xp.calculate_level(total_xp)

                #####
                # Check for avoid a crash when the image doesn't exist

                if not os.path.isfile(settings.MEDIA_ROOT + str(local_student.image)):
                    local_student.image = ""

                    # url = 'course_listing.html'

                    # html = prerender_nav(local_student, url, current_level, settings, request)
                html = ""

                notifications_script = prerender_notifications(local_student)

                context = {
                    "local_student": local_student,
                    "local_student_courses": local_student_courses,
                    "course_list": course_list,
                    "SUNRISE_URL": settings.SUNRISE_URL,
                    "notifications_content": notifications_script,
                    "navbar_content": html,
                }

            return render(request, "spex_qubal/course_listing_ajax.html", context)

        elif isinstance(real_person, Teacher):

            if Teacher.objects.get(pk=local_person.id):

                local_teacher = get_object_or_404(Teacher, pk=local_person.id)
                total_xp = local_teacher.xp
                current_level = calculate_level(total_xp)
                #####
                # Check for avoid a crash when the image doesn't exist
                if not os.path.isfile(settings.MEDIA_ROOT + str(local_teacher.image)):
                    local_teacher.image = ""

                authored_course_list = Course.objects.filter(author=local_person.id)
                not_authored_course_list = Course.objects.exclude(author=local_person.id)

                url = "course_listing_ajax.html"

                html = prerender_nav(local_teacher, url, current_level, settings, request)

                context = {
                    "local_teacher": local_teacher,
                    "authored_course_list": authored_course_list,
                    "not_authored_course_list": not_authored_course_list,
                    "SUNRISE_URL": settings.SUNRISE_URL,
                    "navbar_content": html,
                }

            return render(request, "qubalapp/course_listing_teacher.html", context)

    else:
        return HttpResponseRedirect(settings.SUNRISE_URL + "landing/")