Example #1
0
def commentCreate(request, id):
    #   # 댓글 작성 폼을 list에서 보여줌.
    comment_form = CommentForm(request.POST)
    if comment_form.is_valid():
        # 유저정보, 몇번 글 넣어야 하는지 전달할거니까 기다려
        movie = Movie.objects.get(id=id)
        comment = comment_form.save(commit=False)
        comment.user = request.user
        comment.movie = movie
        comment.save()

    return redirect('movies:detail', id)
Example #2
0
def detail(request, id):
    movie = Movie.objects.get(id=id)
    # # 댓글 폼
    comment_form = CommentForm()
    ratings = 0
    # 평점
    for score in movie.score_set.all():
        ratings += score.rating
    if ratings == 0:
        ratingAvg = 0
    else:
        ratingAvg = ratings / movie.score_set.all().count()
    ratingAvg = round(ratingAvg, 2)

    # 추천 영화 보여주기 //
    TMD_KEY = os.getenv('37a3092ff9c0a61c3819bc65e4ab09c5')
    RECOMMEND_URL = f"https://api.themoviedb.org/3/movie/{movie.tmd_id}/recommendations?api_key={TMD_KEY}&language=ko-KR"
    recommendData = requests.get(RECOMMEND_URL)
    resRecommends = recommendData.json().get('results')

    # 영화 추천 //
    # 추천 영화가 5개보다 작을때와 클때로 구분
    res = []
    if (len(resRecommends) > 5):
        for i in range(5):
            ig = "https://image.tmdb.org/t/p/w500" + resRecommends[i].get('poster_path')
            url = resRecommends[i].get('id')
            if Movie.objects.filter(tmd_id=url):
                # 추천 영화가 db에 있다면
                recommend = Movie.objects.get(tmd_id=url)
                ul = recommend.id
            else:
                # 없으면 현재 페이지 영화 id 사용
                ul = id
            res.append({'ig': ig, 'ul': ul})
    else:
        for j in range(len(resRecommends)):
            ig = "https://image.tmdb.org/t/p/w500" + resRecommends[j].get('poster_path')
            url = resRecommends[j].get('id')
            if Movie.objects.filter(tmd_id=url):
                # 추천 영화가 db에 있다면
                recommend = Movie.objects.get(tmd_id=url)
                ul = recommend.id
            else:
                # 없으면 현재 페이지 영화 id 사용
                ul = id
            res.append({'ig': ig, 'ul': ul})
    # // 영화 추천
    return render(request, 'movies/detail.html',
                  {'movie': movie, 'comment_form': comment_form, 'res': res, "ratingAvg": ratingAvg})
Example #3
0
	def get_context_data(self, **kwargs):
		context = super(MovieDetailView, self).get_context_data(**kwargs)
		context['movie'] = self.object
		context['comments'] = self.object.moviecomment_set.all()
		context['add_form'] = CommentForm({'author': self.request.user})
		context['editing_title'] = self.object.title

		try:
			mark = self.object.moviemark_set.get(author_id=self.request.user.id)
			form = RateForm(instance=mark)
		except MovieMark.DoesNotExist:
			form = RateForm()
		context['rate_form'] = form
		return context
Example #4
0
def add_comment(request_post, content_object):
    """
    Comment form validation and entry into the database
    :param request_post:
    :param content_object:
    :return:
    """
    form = CommentForm(request_post)
    if form.is_valid():
        form = form.save(commit=False)
        form.content_object = content_object
        if request_post.get('parent', None):
            form.parent_id = int(request_post.get('parent'))
        form.save()
    else:
        logger.error(form.errors)
Example #5
0
def add_comment(request, pk):
    post = get_object_or_404(MovieInfo, pk=pk)
    if request.method == 'POST':
        form = CommentForm(request.POST)

        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.save()
            return redirect('/movies/' + str(post.pk) + "/", pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'movies/add_comments.html', {'form': form})
Example #6
0
 def get(self, request, *args, **kwargs):
     self.form = CommentForm(instance=self.get_object())
     return super(EditComment, self).get(request, *args, **kwargs)
Example #7
0
def detail(request, id):
    movie = Movie.objects.get(id=id)
    # # 댓글 폼
    comment_form = CommentForm()
    ratings = 0
    # 평점
    for score in movie.score_set.all():
        ratings += score.rating
    if ratings == 0:
        ratingAvg = 0
    else:
        ratingAvg = ratings / movie.score_set.all().count()
    ratingAvg = round(ratingAvg, 2)

    # 추천 영화 보여주기 //
    TMD_KEY = '37a3092ff9c0a61c3819bc65e4ab09c5'
    RECOMMEND_URL = f"https://api.themoviedb.org/3/movie/{movie.tmd_id}/recommendations?api_key={TMD_KEY}&language=ko-KR"
    request = urllib.request.Request(RECOMMEND_URL)
    response = urllib.request.urlopen(request)
    rescode = response.getcode()
    if (rescode == 200):
        response_body = response.read()
        resRecommends = json.loads(response_body.decode('utf-8'))

    # 영화 추천 //
    # 추천 영화가 5개보다 작을때와 클때로 구분
    res = []
    resRecommend = resRecommends['result']
    if (len(resRecommend) > 5):
        for i in range(5):
            ig = "https://image.tmdb.org/t/p/w500" + resRecommends[i][
                'poster_path']
            url = resRecommend[i]['id']
            if Movie.objects.filter(tmd_id=url):
                # 추천 영화가 db에 있다면
                recommend = Movie.objects.get(tmd_id=url)
                ul = recommend.id
            else:
                # 없으면 현재 페이지 영화 id 사용
                ul = id
            res.append({'ig': ig, 'ul': ul})
    else:
        for j in range(len(resRecommend)):
            ig = "https://image.tmdb.org/t/p/w500" + resRecommend[j].get(
                'poster_path')
            url = resRecommend[j].get('id')
            if Movie.objects.filter(tmd_id=url):
                # 추천 영화가 db에 있다면
                recommend = Movie.objects.get(tmd_id=url)
                ul = recommend.id
            else:
                # 없으면 현재 페이지 영화 id 사용
                ul = id
            res.append({'ig': ig, 'ul': ul})
    # // 영화 추천
    return render(
        request, 'movies/detail.html', {
            'movie': movie,
            'comment_form': comment_form,
            'res': res,
            "ratingAvg": ratingAvg
        })