summaryrefslogtreecommitdiff
path: root/fossee_manim/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'fossee_manim/views.py')
-rw-r--r--fossee_manim/views.py222
1 files changed, 176 insertions, 46 deletions
diff --git a/fossee_manim/views.py b/fossee_manim/views.py
index d9b27b2..44f20c1 100644
--- a/fossee_manim/views.py
+++ b/fossee_manim/views.py
@@ -1,12 +1,13 @@
-from django.shortcuts import render
+from os import listdir, path, sep, makedirs, remove
from .forms import (
UserRegistrationForm, UserLoginForm,
- ProfileForm, AnimationProposal,
- CommentForm
+ ProfileForm, AnimationProposal,
+ CommentForm, UploadAnimationForm
)
from .models import (
Profile, User, AnimationStats,
- has_profile, Animation, Comment
+ has_profile, Animation, Comment,
+ Category
)
from datetime import datetime, date
from django.contrib.auth import login, logout, authenticate
@@ -15,24 +16,42 @@ from django.contrib import messages
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render, redirect
from django.utils import timezone
-from .send_mails import send_email
from django.http import HttpResponse, HttpResponseRedirect
-from textwrap import dedent
from django.conf import settings
-from os import listdir, path, sep
+from django.core.files.uploadhandler import FileUploadHandler
+from django.db.models import F, Subquery, OuterRef, Q
from zipfile import ZipFile
-from django.contrib import messages
-from django.db.models import Q
+from textwrap import dedent
+from requests import get
+from random import sample
+from .send_mails import send_email
import datetime as dt
-import os
+import shutil
try:
from StringIO import StringIO as string_io
except ImportError:
from io import BytesIO as string_io
+def makepath(proposal_data, reject=None):
+ if not path.exists(path.join(settings.MEDIA_ROOT,
+ proposal_data.category.name)):
+ makedirs(path.join(settings.MEDIA_ROOT,
+ proposal_data.category.name))
+
+ if reject:
+ shutil.rmtree(path.join(
+ settings.MEDIA_ROOT, proposal_data.category.name,
+ proposal_data.title.replace(" ", "_")
+ + str(proposal_data.id)))
+ else:
+ makedirs(path.join(settings.MEDIA_ROOT, proposal_data.category.name,
+ proposal_data.title.replace(" ", "_")
+ + str(proposal_data.id)))
+
+
def check_repo(link):
- return True if 'github.com' in link else False
+ return (get(link).status_code == 200)
def is_email_checked(user):
@@ -43,7 +62,7 @@ def is_email_checked(user):
def is_superuser(user):
- return True if user.is_superuser else False
+ return user.is_superuser
def index(request):
@@ -52,8 +71,8 @@ def index(request):
user = request.user
form = UserLoginForm()
if user.is_authenticated() and is_email_checked(user):
- if user.groups.filter(name='reviewer').count() > 0:
- return redirect('/view_profile/')
+ if user.groups.filter(name='reviewer').exists:
+ return redirect('/proposal_status/')
return redirect('/view_profile/')
elif request.method == "POST":
form = UserLoginForm(request.POST)
@@ -62,21 +81,24 @@ def index(request):
login(request, user)
if is_superuser(user):
return redirect("/admin")
- if user.groups.filter(name='reviewer').count() > 0:
- return redirect('/view_profile/')
+ if user.groups.filter(name='reviewer').exists():
+ return redirect('/proposal_status/')
return redirect('/view_profile/')
-
- return render(request, "fossee_manim/index.html", {"form": form})
+ anime = AnimationStats.objects.filter(animation__status='released').order_by('-id')[:5]
+ return render(request, "fossee_manim/index.html", {"form": form,
+ "anime" : anime
+ })
def is_reviewer(user):
'''Check if the user is having reviewer rights'''
- return True if user.groups.filter(name='reviewer').count() > 0 else False
+ return user.groups.filter(name='reviewer').exists()
def user_login(request):
'''User Login'''
user = request.user
+ categories = Category.objects.all()
if is_superuser(user):
return redirect('/admin')
if user.is_authenticated():
@@ -91,12 +113,13 @@ def user_login(request):
login(request, user)
if user.groups.filter(name='reviewer').count() > 0:
return redirect('/view_profile/')
- return redirect('/view_profile/')
+ return redirect('/how_to/')
else:
return render(request, 'fossee_manim/login.html', {"form": form})
else:
form = UserLoginForm()
- return render(request, 'fossee_manim/login.html', {"form": form})
+ return render(request, 'fossee_manim/login.html', {"form": form,
+ 'categories': categories })
def user_logout(request):
@@ -178,10 +201,12 @@ def user_register(request):
def view_profile(request):
""" view instructor and coordinator profile """
user = request.user
+ categories = Category.objects.all()
if is_superuser(user):
return redirect('/admin')
if is_email_checked(user) and user.is_authenticated():
- return render(request, "fossee_manim/view_profile.html")
+ return render(request, "fossee_manim/view_profile.html",
+ {'categories': categories})
else:
if user.is_authenticated():
return render(request, 'fossee_manim/activation.html')
@@ -198,6 +223,7 @@ def edit_profile(request):
""" edit profile details facility for reviewer and contributor """
user = request.user
+ categories = Category.objects.all()
if is_superuser(user):
return redirect('/admin')
if is_email_checked(user):
@@ -229,38 +255,42 @@ def edit_profile(request):
form_data.save()
return render(
- request, 'fossee_manim/profile_updated.html'
+ request, 'fossee_manim/profile_updated.html',
+ {'categories': categories}
)
else:
context['form'] = form
return render(request, 'fossee_manim/edit_profile.html', context)
else:
form = ProfileForm(user=user, instance=profile)
- return render(request, 'fossee_manim/edit_profile.html', {'form': form}
+ return render(request, 'fossee_manim/edit_profile.html', {'form': form,
+ 'categories': categories}
)
@login_required
def send_proposal(request):
user = request.user
+ categories = Category.objects.all()
if request.method == 'POST':
form = AnimationProposal(request.POST)
if form.is_valid():
form_data = form.save(commit=False)
form_data.contributor = user
- form_data.status = "pending"
+ form_data.status = "pending"
if check_repo(form_data.github):
form_data.save()
form.save_m2m()
+ # makepath(form_data)
else:
messages.warning(request, 'Please enter valid github details')
return render(request, 'fossee_manim/send_proposal.html',
- {'form': form})
+ {'form': form, 'categories': categories})
return redirect('/proposal_status/')
else:
form = AnimationProposal()
return render(request, 'fossee_manim/send_proposal.html',
- {'form': form})
+ {'form': form, 'categories': categories})
@login_required
@@ -269,13 +299,14 @@ def proposal_status(request):
profile = Profile.objects.get(user_id=user)
anime = {}
anime_list = {}
+ categories = Category.objects.all()
if profile.position == 'contributor':
anime = Animation.objects.filter(contributor_id=user)
else:
- anime_list = Animation.objects.filter(Q(status='pending') |
- Q(status='changes'))
+ anime_list = Animation.objects.order_by('-created')
return render(request, 'fossee_manim/proposal_status.html',
- {'anime': anime, 'anime_list': anime_list})
+ {'anime': anime, 'anime_list': anime_list,
+ 'categories': categories})
@login_required
@@ -284,28 +315,32 @@ def edit_proposal(request, proposal_id=None):
comment_form = CommentForm()
proposal = Animation.objects.get(id=proposal_id)
proposal_form = AnimationProposal(instance=proposal)
+ upload_form = UploadAnimationForm()
+ categories = Category.objects.all()
+ video = AnimationStats.objects.filter(animation=proposal_id)
try:
- comments = Comment.objects.all().order_by('-created_date')
+ comments = Comment.objects.filter(animation_id=proposal_id).order_by(
+ '-created_date'
+ )
except:
comments = None
if request.method == 'POST':
text = request.POST.get('comment')
- s1 = request.POST.get('release')
- s2 = request.POST.get('rejected')
+ status1 = request.POST.get('release')
+ status2 = request.POST.get('rejected')
- if s1 or s2 is not None:
- if s1:
+ if status1 or status2 is not None:
+ if status1:
proposal.status = 'released'
- proposal.reviewer = user
- proposal.save()
send_email(request, call_on='released',
- contributor=proposal.contributor)
+ contributor=proposal.contributor)
else:
proposal.status = 'rejected'
- proposal.reviewer = user
- proposal.save()
+ makepath(proposal, reject=1)
send_email(request, call_on='rejected',
- contributor=proposal.contributor)
+ contributor=proposal.contributor)
+ proposal.reviewer = user
+ proposal.save()
return redirect('/proposal_status/')
if text is not None:
@@ -313,11 +348,13 @@ def edit_proposal(request, proposal_id=None):
form_data = comment_form.save(commit=False)
form_data.commentor = user
form_data.animation = proposal
+ form_data.animation_status = proposal.status
if user.profile.position == 'reviewer':
proposal.status = 'changes'
+ proposal.save()
send_email(request, call_on='changes',
- contributor=proposal.contributor,
- proposal=proposal)
+ contributor=proposal.contributor,
+ proposal=proposal)
form_data.save()
return redirect('/edit_proposal/{}'.format(proposal_id))
proposal_form = AnimationProposal(request.POST, instance=proposal)
@@ -345,13 +382,106 @@ def edit_proposal(request, proposal_id=None):
comments = paginator.page(paginator.num_pages)
return render(request, 'fossee_manim/edit_proposal.html',
{'proposal_form': proposal_form,
- "comments": comments,
- "comment_form": comment_form})
+ "comments": comments,
+ "comment_form": comment_form,
+ "upload_form": upload_form,
+ 'video': video,
+ 'categories': categories})
def search(request):
+ categories = Category.objects.all()
if request.method == 'POST':
word = request.POST.get('sbox')
+ anime_list = AnimationStats.objects.filter(
+ Q(animation__title__contains=word) | Q(animation__description__contains=word)
+ | Q(animation__category__name__contains=word), animation__status='released')
- return render(request, 'fossee_manim/search_results.html')
+ return render(request, 'fossee_manim/search_results.html',
+ {'s_result': anime_list, 'categories': categories})
+
+
+@login_required
+def upload_animation(request, proposal_id=None):
+ if request.method == 'POST':
+ proposal = Animation.objects.get(id=proposal_id)
+ anim_stats = UploadAnimationForm(request.POST or None,
+ request.FILES or None)
+
+ # return redirect('/edit_proposal/{}'.format(proposal_id))
+ if anim_stats.is_valid():
+ anim = AnimationStats.objects.filter(
+ animation=proposal)
+ if anim.exists():
+ anobj = anim.first()
+ try:
+ remove(anobj.thumbnail.path)
+ except:
+ pass
+ remove(anobj.video_path.path)
+ anobj.delete()
+ anobj = AnimationStats.objects.create(
+ animation=proposal, video_path=request.FILES['video_path'])
+ else:
+ anobj = AnimationStats.objects.create(
+ animation=proposal, video_path=request.FILES['video_path'])
+ anobj._create_thumbnail()
+
+ return render(request, 'fossee_manim/upload_success.html')
+ else:
+ return redirect('/view_profile/')
+
+def video(request, aid=None):
+ user = request.user
+ video = AnimationStats.objects.filter(id=aid, animation__status="released")
+ if len(video):
+ comment_form = CommentForm()
+ # if views crosses limit comment the line below
+ video.update(views=F('views')+1)
+ video.update(like=F('like')+1)
+ anim_list = AnimationStats.objects.filter(animation__status="released")
+ suggestion_list = [x for x in anim_list if (
+ x.animation.category == video[0].animation.category)]
+ reviewer_id = video[0].animation.reviewer.id
+ comment_list = Comment.objects.filter(animation=video[0].animation)
+ comments = [x for x in comment_list if x.animation.status !=
+ ('pending' or 'changes')]
+ if request.method == 'POST':
+ if is_email_checked(user):
+ comment_form = CommentForm(request.POST)
+ form_data = comment_form.save(commit=False)
+ form_data.commentor = user
+ form_data.animation = video[0].animation
+ form_data.animation_status = video[0].animation.status
+ form_data.save()
+ return redirect('/video/{}'.format(aid))
+ else:
+ return redirect('/login/')
+ else:
+ return redirect('/view_profile/')
+
+ if len(suggestion_list)>3:
+ suggestion_list = sample(suggestion_list, 3)
+ else:
+ suggestion_list = [x for x in anim_list if x.id != int(aid)][:3]
+ categories = Category.objects.all()
+ return render(request, 'fossee_manim/video.html',
+ {'video': video, 'categories': categories,
+ 'reco': suggestion_list,
+ "comment_form": comment_form,
+ 'comments': comments})
+
+
+def search_category(request, cat=None):
+ cat_id = Category.objects.get(name=cat)
+ anim_list = AnimationStats.objects.filter(animation__status="released")
+ cat_video_list = [x for x in anim_list if (x.animation.category == cat_id)]
+ categories = Category.objects.all()
+ return render(request, 'fossee_manim/categorical_list.html',
+ {'categorial_list': cat_video_list, 'categories': categories
+ })
+
+
+def how_to(request):
+ return render(request, 'fossee_manim/how_to.html') \ No newline at end of file