diff --git a/db.sqlite3 b/db.sqlite3 index fc3e7f7..3e580a8 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/movies/admin.py b/movies/admin.py index 26e9efc..2f7dbb3 100644 --- a/movies/admin.py +++ b/movies/admin.py @@ -1,5 +1,5 @@ from django.contrib import admin -from .models import Movie, Color, MovieComment, Review, ReviewComment, UserColorRecord +from .models import Movie, Color, MovieComment, Review, ReviewComment, UserColorRecord,Quiz # Register your models here. admin.site.register(Movie) @@ -8,3 +8,4 @@ admin.site.register(Review) admin.site.register(ReviewComment) admin.site.register(UserColorRecord) +admin.site.register(Quiz) \ No newline at end of file diff --git a/movies/forms.py b/movies/forms.py index d65ab66..cf64c64 100644 --- a/movies/forms.py +++ b/movies/forms.py @@ -1,6 +1,6 @@ from django.db.models.base import Model from django.forms import ModelForm -from .models import MovieComment, Review, ReviewComment +from .models import MovieComment, Review, ReviewComment, Quiz class MovieCommentForm(ModelForm): class Meta: @@ -15,4 +15,9 @@ class Meta: class ReviewCommentForm(ModelForm): class Meta: model = ReviewComment - fields = ('content',) \ No newline at end of file + fields = ('content',) + +class QuizForm(ModelForm): + class Meta: + model = Quiz + fields = ('movie', 'draw') \ No newline at end of file diff --git a/movies/migrations/0012_quiz.py b/movies/migrations/0012_quiz.py new file mode 100644 index 0000000..d56f0c9 --- /dev/null +++ b/movies/migrations/0012_quiz.py @@ -0,0 +1,26 @@ +# Generated by Django 3.2.9 on 2021-11-24 16:35 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('movies', '0011_review_draw'), + ] + + operations = [ + migrations.CreateModel( + name='Quiz', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('draw', models.ImageField(blank=True, upload_to='images/')), + ('correct_user', models.ManyToManyField(related_name='corrected_quizs', to=settings.AUTH_USER_MODEL)), + ('movie', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='movies.movie')), + ('user', models.ForeignKey(default=13, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/movies/models.py b/movies/models.py index 4d8106c..09a528c 100644 --- a/movies/models.py +++ b/movies/models.py @@ -65,4 +65,10 @@ class ReviewComment(models.Model): class UserColorRecord(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) - color = models.CharField(max_length=50) \ No newline at end of file + color = models.CharField(max_length=50) + +class Quiz(models.Model): + movie = models.ForeignKey(Movie, on_delete=models.CASCADE) + user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=13) + draw = models.ImageField(blank=True, upload_to='images/') + correct_user = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="corrected_quizs") \ No newline at end of file diff --git a/movies/templates/movies/quiz.html b/movies/templates/movies/quiz.html new file mode 100644 index 0000000..befa86b --- /dev/null +++ b/movies/templates/movies/quiz.html @@ -0,0 +1,136 @@ +{% extends 'base.html' %} +{% load static %} + +{% block back_URL %} "{% url 'movies:index' 2 %}" {% endblock back_URL %} + +{% block content %} +
+
+
+
+ +
+
+ +
+

티어는 총 5단계이며, 일정 포인트를 충족하면 티어가 상승합니다.

+
+
+
+
+ +
+
+ +
+

당신의 포인트는 {{ request.user.point }}점 + {% if required_point %} + 이며, {{ required_point }}점을 올리면 다음 단계로 상승합니다. + {% else %} + 입니다. + {% endif %} +

+

리뷰 작성은 30점, 한줄평은 10점, 리뷰 댓글 작성은 5점이 부여됩니다.

+

문제 제출 시 30점이 부여됩니다.

+

밑의 문제들을 맞추면 각각 20점이 부여됩니다.

+
+
+
+
+ {% for quiz,quiz_examples in quizs_list %} + {% with solved_user=quiz.correct_user.all%} + {% if user in solved_user %} + {% else %} +
+
+
+
+
+
+ 로고 +
+
+
+
+

답을 알려주세요

+
+
+ {% csrf_token %} + {% for example in quiz_examples %} + {% if example == quiz %} + + {% else %} + + {% endif %} + {% endfor %} +
+ +
+
+
+
+ +
+
+ +
+
+
+ + {% endif %} + {% endwith %} + {% endfor %} +
+
+ +{% endblock content %} + +{% block option_js %} + +{% endblock option_js %} \ No newline at end of file diff --git a/movies/templates/movies/quiz_create.html b/movies/templates/movies/quiz_create.html new file mode 100644 index 0000000..014b938 --- /dev/null +++ b/movies/templates/movies/quiz_create.html @@ -0,0 +1,235 @@ +{% extends 'base.html' %} +{% load static %} +{% load bootstrap5 %} + +{% block option_import %} {% endblock option_import %} + +{% block back_URL %} "{% url 'movies:quiz' %}" {% endblock back_URL %} + +{% block content %} +
+
+
+
+
+ {% csrf_token %} +
+

영화고르기

+
+ +
+
+ +
+
+ +
+
+ +
+
+
+ +
+
+ +
+ +
+ +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+ +
+
+
+
+
+{% endblock content %} + + +{% block option_js %} + +{% endblock option_js %} \ No newline at end of file diff --git a/movies/urls.py b/movies/urls.py index e87f12e..69a0aa4 100644 --- a/movies/urls.py +++ b/movies/urls.py @@ -22,4 +22,8 @@ path('usercolor_update//', views.usercolor_update, name="usercolor_update"), path('voice_process/', views.voice_process, name="voice_process"), + + path('quiz_create/', views.quiz_create, name="quiz_create"), + path('quiz/', views.quiz, name="quiz"), + path('quiz_check/', views.quiz_check, name="quiz_check"), ] diff --git a/movies/views.py b/movies/views.py index bdc42c2..a751c4b 100644 --- a/movies/views.py +++ b/movies/views.py @@ -1,11 +1,13 @@ from django.shortcuts import render,redirect -from .models import Movie, MovieComment, Review, ReviewComment, UserColorRecord -from .forms import ReviewCommentForm, ReviewForm, MovieCommentForm +from .models import Movie, MovieComment, Review, ReviewComment, UserColorRecord, Quiz +from .forms import ReviewCommentForm, ReviewForm, MovieCommentForm, QuizForm from django.contrib.auth.decorators import login_required from django.views.decorators.http import require_POST, require_http_methods, require_safe import math from django.http import JsonResponse from django.db.models import Count +import json +import random @login_required @require_safe @@ -348,3 +350,105 @@ def review_update(request,review_pk): } return render(request,'movies/review_update.html', context) +@login_required +@require_http_methods(['GET','POST']) +def quiz_create(request): + # Profile Color Create + colors = request.user.usercolorrecord_set.all() # Color List + last_color = colors[len(colors)-1] # Picked Color + colors = reversed(colors) # Color Sort(Recently) + + movies = Movie.objects.all() + + if request.method == "POST": + form = QuizForm(request.POST, request.FILES) + if form.is_valid(): + quiz = form.save(commit=False) + quiz.user = request.user + quiz.save() + request.user.point += 30 + request.user.save() + return redirect('movies:index', 2) + else: + form = QuizForm() + context = { + 'form': form, + 'last_color': last_color, + 'colors': colors, + 'movies': movies, + } + return render(request, 'movies/quiz_create.html', context) + +@login_required +@require_http_methods(['GET','POST']) +def quiz(request): + quizs = Quiz.objects.all() + point = request.user.point + + required_point = 0 + if request.user.is_staff or point > 201: + pass + elif point > 120: + required_point = 201 - point + elif point > 60: + required_point = 121 - point + elif point > 20: + required_point = 61 - point + else: + required_point = 21 - point + + if request.method == "POST": + form = QuizForm(request.POST) + if form.is_valid(): + form.save() + else: + pass + + movies = Movie.objects.all() + movies_ints = [i for i in range(6,106)] + + quizs_list = [] + for i in quizs: + examples = random.sample(movies_ints,4) # Random for 1 Quiz + ran_num = random.choice([1,2,3,4,5,6]) + examples.insert(ran_num,200) + + temps = [] + for x in examples: + if x == 200: + temps.append(i) + else: + temps.append(movies[x-6]) + quizs_list.append([i,temps]) + + context = { + 'quizs_list': quizs_list, + 'required_point': required_point, + } + + return render(request,'movies/quiz.html',context) + +@login_required +@require_http_methods(['GET','POST']) +def quiz_check(request): + post_body = json.loads(request.body.decode('utf-8')) + + question_pk = post_body.get('question_pk') + answer = post_body.get('answer') + + quiz = Quiz.objects.get(pk=question_pk) + correct = False + + if int(quiz.movie.pk) == int(answer): + print("정답") + correct = True + + request.user.point += 20 + quiz.correct_user.add(request.user) + request.user.save() + + context = { + "correct": correct, + } + + return JsonResponse(context) \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index e8c879a..12dc8f7 100644 --- a/templates/base.html +++ b/templates/base.html @@ -81,7 +81,7 @@ {% comment %} User & Login Button {% endcomment %}
- + 티어 올리기 {% comment %} User Tier {% endcomment %} {% if user.is_staff %}