# -*- coding: utf-8 -*-

from __future__ import division

from django.conf import settings
from django.shortcuts import render

from account.models import UserInfo
from codes.models import CourseCodeInfo
from course.decorators import check_token
from courses.models import CourseInfo
from utils.error.errno_utils import CourseCodeStatusCode, ProfileStatusCode
from utils.error.response_utils import response


@check_token
def course_list(request):
    user_id = request.GET.get('user_id', '')

    try:
        user = UserInfo.objects.get(user_id=user_id, status=True)
    except UserInfo.DoesNotExist:
        return response(ProfileStatusCode.PROFILE_NOT_FOUND)

    try:
        course_code = CourseCodeInfo.objects.get(user_id=user.user_id, exchanged=True, status=True)
    except CourseCodeInfo.DoesNotExist:
        response(CourseCodeStatusCode.COURSE_CODE_NOT_FOUND)

    courses = CourseInfo.objects.filter(status=True)
    courses = [course.data for course in courses]

    return render(request, 'page/course_list.html', {
        'domain': settings.DOMAIN,
        'courses': courses,
        'params': 'user_id={}&vtoken={}'.format(user_id, request.GET.get('vtoken', '')),
    })
