コード例 #1
0
def wiki(request, lesson, chapter):
    if request.method == "GET":

        template = "wiki/wiki.html"

        all_wiki = []
        for i in range(1, WikiMenuContent.get_wiki_size()):
            all_wiki.append(
                WikiMenuContent(i)
            )

        choice_wiki = Wiki.objects.get(lesson=lesson, chapter=chapter)
        all_content = list(Content.objects.filter(wiki=choice_wiki))
        all_content.sort()

        return render(request, template, {
            "title": str(choice_wiki),
            "all_content": all_content,
            "all_wiki": all_wiki,
        })

    search = request.POST['search']

    search_for_sql = '@'.join(search) + '@'

    sql = """
    SELECT lesson, chapter, `name`
    FROM wiki_wiki 
    WHERE `name` LIKE "%%%s%%"
    ESCAPE "@"
    """ % search_for_sql

    search_title_result = execute_sql(sql)

    sql = """
    SELECT w.lesson, w.chapter, w.name, c.content
    FROM wiki_content as c, wiki_wiki as w
    WHERE (c.isText = 1 OR c.isTitle = 1) AND c.content LIKE "%%%s%%" AND c.wiki_id = w.id
    ESCAPE "@"
    """ % search_for_sql

    search_content_result = execute_sql(sql)

    sql = '''
    select w.lesson, w.chapter, w.name, c.content
    from wiki_content as c, wiki_wiki as w 
    where isTable = 1 and c.wiki_id = w.id
    '''

    search_table_result = execute_sql(sql)
    search_table_result = ((i[0], i[1], i[2], clean_table_content(i[3])) for i in search_table_result)
    search_table_result = (i for i in search_table_result if i[3].find(search) != -1)
コード例 #2
0
ファイル: views.py プロジェクト: alan8365/EasyCoding
def register(request):
    template = 'account/signup.html'
    if request.method == 'GET':
        return render(request, template)

    if 'type' in request.POST:
        if request.POST.get('type') == 'check_email':

            email = request.POST.get('email')

            repeat = True
            sql = """
                select id 
                from account_user
                where email = "%s"
            """ % email

            repeat_email = execute_sql(sql)

            if not repeat_email:
                repeat = False

            response = JsonResponse({"repeat": repeat})

        elif request.POST.get('type') == 'check_username':

            username = request.POST.get('username')

            repeat = True

            sql = """
                select id 
                from account_user
                where username = "******"
            """ % username

            repeat_username = execute_sql(sql)

            if not repeat_username:
                repeat = False

            response = JsonResponse({"repeat": repeat})

        return response

    form = UserForm(request.POST)
    user = form.save()
    print(user)

    return login(request)
コード例 #3
0
ファイル: views.py プロジェクト: alan8365/EasyCoding
def vote(request, pk, sort_by="vote_number", page=1):
    template = "tutorial/vote.html"
    pk = 12

    if request.method == "GET":
        ass = Assessment.objects.get(pk=pk)

        contents = get_vote_content_by_id(pk, request.user.id)

        sort_types = {
            "vote_number": "票數較多",
            "date_first": "由新至舊",
            "date_last": "由舊至新"
        }

        if sort_by == "vote_number":
            contents = sorted(contents,
                              key=lambda x: x.point_percent,
                              reverse=True)
        elif sort_by == "date_first":
            contents = sorted(contents, key=lambda x: x.datetime, reverse=True)
        elif sort_by == "date_last":
            contents = sorted(contents, key=lambda x: x.datetime)

        contents = list_split(contents, 4)
        page_number = len(contents)

        sql = """
            select ass_code_id
            from assessment_havevoted as hv, assessment_assessmentcode as ac
            where hv.user_id = %d and ac.ass_id = %d and hv.ass_code_id = ac.id    
        """ % (request.user.id, pk)

        already_voted = execute_sql(sql)
        already_voted = already_voted[0][0] if already_voted else None

        return render(
            request, template, {
                "assessment": ass,
                "contents": contents[page - 1],
                "pk": pk,
                "already_voted_id": already_voted,
                "sort_by": sort_by,
                "sort_types": sort_types.items(),
                "page": page,
                "page_number": page_number,
            })
コード例 #4
0
def is_user_already_get_achievement(user, achievement):
    sql = """
        select a.achievement_id
        from account_user as u,
             account_achievement_get as a
        where a.user_id = u.id and u.id = %d
    """ % user.id

    result = execute_sql(sql)

    aid = achievement.name

    for i in result:
        if aid == i[0]:
            return True

    return False
コード例 #5
0
ファイル: apps.py プロジェクト: alan8365/EasyCoding
    def __init__(self, lesson, user):
        self.lesson = lesson
        self.label = "第%d章作業" % lesson.number

        sql = """
            SELECT a.id
            FROM assessment_assessment as a, course_course as c 
            WHERE c.lesson_id = %d and a.course_id = c.id
        """ % lesson.number

        temp = execute_sql(sql)

        all_assessment = {i.ass for i in user.assessmentcode_set.all()}

        assessments = []

        for i in temp:
            assessment = Assessment.objects.get(id=i[0])

            if assessment in all_assessment:
                assessment.is_done = True
            else:
                assessment.is_done = False

            assessments.append(assessment)

        assessments.sort()
        self.assessments = assessments

        self.lesson_progress = user.assess_progress.course.lesson
        self.chapter_progress = user.assess_progress.course.chapter

        self.is_done = False
        self.is_lock = False

        if self.lesson_progress < lesson:
            self.is_lock = True
        else:
            for assessment in assessments:
                if not assessment.is_done:
                    break
            else:
                self.is_done = True
コード例 #6
0
ファイル: views.py プロジェクト: alan8365/EasyCoding
def home(request):
    template = "tutorial/home.html"

    if request.method == "GET":
        user_course = request.user.course_progress

        all_course = []

        for i in Lesson.objects.all().exclude(number__gt=11):
            all_course.append(CourseMenuContent(i, user_course))

        assessment_level_completed = []

        for i in range(1, 6):
            sql = """
                SELECT COUNT(ac.id)
                FROM assessment_assessmentcode as ac, assessment_assessment as a 
                WHERE ac.user_id = %d and a.level = %d and ac.ass_id = a.id
            """ % (request.user.id, i)

            temp = execute_sql(sql)[0][0]

            assessment_level_completed.append(temp)

        temp = len(Assessment.objects.all()) - sum(assessment_level_completed)
        assessment_level_completed.append(temp)

        assessment_lists = [
            assessment_list(i, request.user)
            for i in Lesson.objects.all().exclude(number__gt=11).exclude(
                number__lt=2)
        ]

        return render(
            request, template, {
                'username': request.user.username,
                'user_course': user_course,
                'all_course': all_course,
                'assessment_level_completed': assessment_level_completed,
                'assessment_lists': assessment_lists,
                'domain': 'tutorial'
            })
コード例 #7
0
def home(request):
    if not request.user.is_authenticated:
        return redirect('account:index')

    if request.user.course_progress.lesson.number == 0:
        return redirect('tutorial:home')

    if request.method == "GET":
        user_course = request.user.course_progress

        all_course = []

        for i in Lesson.objects.all().exclude(number__gt=11).exclude(
                number__lt=1):
            all_course.append(CourseMenuContent(i, user_course))

        assessment_level_completed = []

        for i in range(1, 6):
            sql = """
                SELECT COUNT(ac.id)
                FROM assessment_assessmentcode as ac, assessment_assessment as a 
                WHERE ac.user_id = %d and a.level = %d and ac.ass_id = a.id
            """ % (request.user.id, i)

            temp = execute_sql(sql)[0][0]

            assessment_level_completed.append(temp)

        temp = len(
            Assessment.objects.all()) - sum(assessment_level_completed) - 1
        assessment_level_completed.append(temp)

        assessment_lists = [
            assessment_list(i, request.user)
            for i in Lesson.objects.all().exclude(number__gt=11).exclude(
                number__lt=2)
        ]

        return render(
            request, 'home.html', {
                'username': request.user.username,
                'user_course': user_course,
                'all_course': all_course,
                'assessment_level_completed': assessment_level_completed,
                'assessment_lists': assessment_lists,
                'domain': 'course'
            })

    search = request.POST['search']
    search_highlight = "<highlight>%s</highlight>" % search
    search_for_sql = '@' + '@'.join(search)

    sql = """
        SELECT lesson, chapter, `name`
        FROM wiki_wiki 
        WHERE chapter != 0 AND `name` LIKE "%%%s%%"
        ESCAPE "@"
        """ % search_for_sql

    search_title_result = execute_sql(sql)
    search_title_result = ((i[0], i[1], i[2].replace(search, search_highlight))
                           for i in search_title_result)

    sql = """
        SELECT w.lesson, w.chapter, w.name, c.content
        FROM wiki_content as c, wiki_wiki as w
        WHERE (c.isText = 1 OR c.isTitle = 1) AND c.wiki_id = w.id AND c.content LIKE "%%%s%%"
        ESCAPE "@"
        """ % search_for_sql

    search_content_result = execute_sql(sql)
    search_content_result = clean_sql_content(search_content_result, search)
    # search_content_result = ((i[0], i[1], i[2], i[3].replace(search, search_highlight)) for i in search_content_result)

    sql = '''
        select w.lesson, w.chapter, w.name, c.content
        from wiki_content as c, wiki_wiki as w 
        where isTable = 1 and c.wiki_id = w.id
        '''

    search_table_result = execute_sql(sql)
    search_table_result = ((i[0], i[1], i[2], clean_table_content(i[3]))
                           for i in search_table_result)
    search_table_result = (i for i in search_table_result
                           if i[3].find(search) != -1)
    search_table_result = clean_sql_content(search_table_result, search)
    # search_table_result = ((i[0], i[1], i[2], i[3].replace(search, search_highlight)) for i in search_table_result)

    search_content_result = tuple(search_content_result) + tuple(
        search_table_result)

    return JsonResponse({
        "search_title_result": tuple(search_title_result),
        "search_content_result": search_content_result
    })
コード例 #8
0
ファイル: views.py プロジェクト: alan8365/EasyCoding
def profile_base(request, template, user):
    course_achievement_check(user)
    assessment_achievement_check(user)

    sql = """
        select sum(point)
        from assessment_assessmentcode as ac
        where ac.user_id = %d
    """ % user.id

    vote_sum = execute_sql(sql)[0][0]

    if vote_sum:
        user.vote_number = int(vote_sum)
    else:
        user.vote_number = 0

    user.save()

    assessment_level_completed = []

    for i in range(1, 6):
        sql = """
            SELECT COUNT(ac.id)
            FROM assessment_assessmentcode as ac, assessment_assessment as a 
            WHERE ac.user_id = %d and a.level = %d and ac.ass_id = a.id
        """ % (user.id, i)

        temp = execute_sql(sql)[0][0]

        assessment_level_completed.append(temp)

    temp = len(Assessment.objects.all()) - sum(assessment_level_completed) - 1
    assessment_level_completed.append(temp)

    all_vote_of_user = list(
        user.assessmentcode_set.all().exclude(ass__course__lesson__number=0))
    all_vote_of_user.sort()

    all_achievement_get = Achievement_get.objects.filter(user_id=user.id)
    all_achievement_get = [i.achievement for i in all_achievement_get]

    if len(all_achievement_get) > 2:
        all_achievement_get = list(all_achievement_get)
        all_achievement_get.sort(reverse=True)
        all_achievement_get = all_achievement_get[:2]

    if request.method == 'POST':
        update_img = request.FILES.get("update_img")

        fs = FileSystemStorage(
            location=os.path.join(BASE_DIR, "static/media/user_images"))

        fs.delete(user.username + ".png")
        filename = fs.save(user.username + ".png", update_img)
        update_url = fs.url(filename)

        user.image_url = "media/user_images/%s.png" % user.username
        user.save()

    return render(
        request, template, {
            "user": user,
            "assessment_level_completed": assessment_level_completed,
            "all_vote_of_user": all_vote_of_user,
            "all_achievement_get": all_achievement_get,
            "time": int(time())
        })