summaryrefslogtreecommitdiff
path: root/fossee_manim/views.py
blob: 06c7b3b4706d72e4dff995424b855475e84f3bb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
from os import listdir, path, sep, makedirs, remove
from .forms import (
            UserRegistrationForm, UserLoginForm,
            ProfileForm, AnimationProposal,
            CommentForm, UploadAnimationForm
            )
from .models import (
            Profile, User, AnimationStats,
            has_profile, Animation, Comment,
            Category
            )
from datetime import datetime, date
from django.contrib.auth import login, logout, authenticate
from django.contrib.auth.decorators import login_required
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 django.http import HttpResponse, HttpResponseRedirect
from django.conf import settings
from django.core.files.uploadhandler import FileUploadHandler
from django.db.models import F, Subquery, OuterRef, Q
from zipfile import ZipFile
from textwrap import dedent
from requests import get
from random import sample
from .send_mails import send_email
import datetime as dt
import logging.config
import shutil
try:
    from StringIO import StringIO as string_io
except ImportError:
    from io import BytesIO as string_io

__author__ = "Akshen Doke"
__credits__ = ["Prabhu Ramachandran", "Aditya P.", "KhushalSingh Rajput",
                "Prathamesh Salunke", "Purusharth Saxsena", "Sharanya Achut", "Ankit Javalkar"
                ]

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:
        try:
            shutil.rmtree(path.join(
                     settings.MEDIA_ROOT, proposal_data.category.name,
                     proposal_data.title.replace(" ", "_")
                    ))
        except:
            logging.info("Proposal rejected")


    else:
        makedirs(path.join(settings.MEDIA_ROOT, proposal_data.category.name,
                 proposal_data.title.replace(" ", "_")
                 ))


def check_repo(link):
    try:
        return (get(link).status_code == 200)
    except:
        return False

def is_email_checked(user):
    if hasattr(user, 'profile'):
        return True if user.profile.is_email_verified else False
    else:
        return False


def is_superuser(user):
    return user.is_superuser


def index(request):
    '''Landing Page'''

    user = request.user
    form = UserLoginForm()
    if user.is_authenticated() and is_email_checked(user):
        return redirect('/proposal_status/')
    return render(request, "fossee_manim/index.html")


def is_reviewer(user):
    '''Check if the user has reviewer rights'''
    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():
        if user.groups.filter(name='reviewer').count() > 0:
            return redirect('/proposal_status/')
        return redirect('/proposal_status/')

    if request.method == "POST":
        form = UserLoginForm(request.POST)
        if form.is_valid():
            user = form.cleaned_data
            login(request, user)
            if user.groups.filter(name='reviewer').count() > 0:
                return redirect('/proposal_status/')
            return redirect('/proposal_status/')
        else:
            return render(request, 'fossee_manim/login.html', {"form": form})
    else:
        form = UserLoginForm()
        return render(request, 'fossee_manim/login.html', {"form": form,
                        'categories': categories })


def user_logout(request):
    '''Logout'''
    categories = Category.objects.all()
    logout(request)
    return render(request, 'fossee_manim/logout.html',
                {'categories': categories })


def activate_user(request, key=None):
    user = request.user
    if is_superuser(user):
        return redirect("/admin")
    if key is None:
        if user.is_authenticated() and user.profile.is_email_verified==0 and \
        timezone.now() > user.profile.key_expiry_time:
            status = "1"
            Profile.objects.get(user_id=user.profile.user_id).delete()
            User.objects.get(id=user.profile.user_id).delete()
            return render(request, 'fossee_manim/activation.html',
                        {'status':status})
        elif user.is_authenticated() and user.profile.is_email_verified==0:
            return render(request, 'fossee_manim/activation.html')
        elif user.is_authenticated() and user.profile.is_email_verified:
            status = "2"
            return render(request, 'fossee_manim/activation.html',
                        {'status':status})
        else:
            return redirect('/register/')

    try:
        user = Profile.objects.get(activation_key=key)
    except:
        return redirect('/register/')

    if key == user.activation_key:
        user.is_email_verified = True
        user.save()
        status = "0"
    else:
        logout(request)
        return redirect('/logout/')
    return render(request, 'fossee_manim/activation.html',
                {"status": status})


def user_register(request):
    '''User Registration form'''
    if request.method == 'POST':
        form = UserRegistrationForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            username, password, key = form.save()
            new_user = authenticate(username=username, password=password)
            login(request, new_user)
            user_position = request.user.profile.position
            send_email(
                       request, call_on='Registration',
                       key=key
                      )

            return render(request, 'fossee_manim/activation.html')
        else:
            if request.user.is_authenticated():
                return redirect('/view_profile/')
            categories = Category.objects.all()
            return render(
                request, "fossee_manim/register.html",
                {"form": form, 'categories': categories})
    else:
        categories = Category.objects.all()
        if request.user.is_authenticated() and is_email_checked(request.user):
            return redirect('/view_profile/')
        elif request.user.is_authenticated():
            return render(request, 'fossee_manim/activation.html')
        form = UserRegistrationForm()
    return render(request, "fossee_manim/register.html", {'form': form, 'categories': categories})


@login_required
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",
                      {'categories': categories})
    else:
        if user.is_authenticated():
            return render(request, 'fossee_manim/activation.html')
        else:
            try:
                logout(request)
                return redirect('/login/')
            except:
                return redirect('/register/')


@login_required
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) and user.is_authenticated():
        if is_reviewer(user):
            template = 'fossee_manim/view_profile.html'
        else:
            template = 'fossee_manim/proposal_status.html'
    else:
        try:
            logout(request)
            return redirect('/login/')
        except:
            return redirect('/register/')

    context = {'template': template}
    if has_profile(user) and is_email_checked(user):
        profile = Profile.objects.get(user_id=user.id)
    else:
        profile = None

    if request.method == 'POST':
        form = ProfileForm(request.POST, user=user, instance=profile)
        if form.is_valid():
            form_data = form.save(commit=False)
            form_data.user = user
            form_data.user.first_name = request.POST['first_name']
            form_data.user.last_name = request.POST['last_name']
            form_data.user.save()
            form_data.save()

            return render(
                        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,
                      'categories': categories}
                      )


@login_required
def send_proposal(request):
    user = request.user
    categories = Category.objects.all()
    if is_email_checked(user) and user.is_authenticated():
        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.save()
            else:
                return render(request, 'fossee_manim/send_proposal.html',
                                {'form': form, 'categories': categories})
            return redirect('/proposal_status/')
        else:
            form = AnimationProposal()
            return render(request, 'fossee_manim/send_proposal.html',
                      {'form': form, 'categories': categories})
    else:
        return redirect('/register/')


@login_required
def proposal_status(request):
    user = request.user
    if is_email_checked(user) and user.is_authenticated():
        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).order_by('-created')
        else:
            anime_list = Animation.objects.order_by('-created')

        # Show upto 9 proposals per page
        paginator_c = Paginator(list(anime), 9)
        paginator_r  = Paginator(list(anime_list), 9)
        page = request.GET.get('page')
        try:
            anime  = paginator_c.page(page)
            anime_list = paginator_r.page(page)
        except PageNotAnInteger:
        # If page is not an integer, deliver first page.
            anime  = paginator_c.page(1)
            anime_list  = paginator_r.page(1)
        except EmptyPage:
            # If page is out of range(e.g 999999), deliver last page.
            anime  = paginator_c.page(paginator_c.num_pages)
            anime_list  = paginator_r.page(paginator_r.num_pages)

        return render(request, 'fossee_manim/proposal_status.html',
                  {'anime': anime, 'anime_list': anime_list,
                   'categories': categories})
    else:
        return redirect('/login/')


@login_required
def edit_proposal(request, proposal_id=None):
    user = request.user
    if is_email_checked(user) and user.is_authenticated():
        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)
        if len(video)>0:
            msg = ('Previously a video was uploaded for '+ video[0].animation.title)
        else:
            msg = ('No video uploaded')
        try:
            comments = Comment.objects.filter(animation_id=proposal_id).order_by(
                                        '-created_date'
                                        )
        except:
            comments = None
        if request.method == 'POST':
            text = request.POST.get('comment')
            status1 = request.POST.get('release')
            status2 = request.POST.get('rejected')
            status3 = request.POST.get('proposal_form')

            if status1 or status2 or status3 is not None:
                if status1:
                    proposal.status = 'released'
                    send_email(request, call_on='released',
                            contributor=proposal.contributor)
                elif status3:
                    send_email(request, call_on='proposal_form',
                     contributor=proposal.contributor)
                    return redirect('/proposal_status/')
                else:
                    proposal.status = 'rejected'
                    makepath(proposal, reject=1)
                    send_email(request, call_on='rejected',
                            contributor=proposal.contributor)
                proposal.reviewer = user
                proposal.save()
                return redirect('/proposal_status/')

            if text is not None:
                comment_form = CommentForm(request.POST)
                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)
                form_data.save()
                return redirect('/edit_proposal/{}'.format(proposal_id))
            proposal_form = AnimationProposal(request.POST, instance=proposal)
            if proposal_form.is_valid():
                p_f = proposal_form.save(commit=False)
                p_f.contributor = user
                p_f.save()
                proposal_form.save_m2m()
        else:
            if user.profile.position == 'contributor':
                if user.id != proposal.contributor_id:
                    return redirect('/logout/')
    else:
        return redirect('/register/')

    if comments is not None:
            #Show upto 12 Workshops per page
            paginator = Paginator(comments, 9)
            page = request.GET.get('page')
            try:
                comments = paginator.page(page)
            except PageNotAnInteger:
            #If page is not an integer, deliver first page.
                comments = paginator.page(1)
            except EmptyPage:
                #If page is out of range(e.g 999999), deliver last page.
                comments = paginator.page(paginator.num_pages)
    return render(request, 'fossee_manim/edit_proposal.html',
                      {'proposal_form': proposal_form,
                       "comments": comments,
                       "comment_form": comment_form,
                        "upload_form": upload_form,
                        'video': video,
                        'categories': categories,
                        'msg': msg})


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__outline__contains=word)
            | Q(animation__category__name__contains=word) | Q(animation__subcategory__contains=word),
            animation__status='released')

    return render(request, 'fossee_manim/search_results.html',
                  {'s_result': anime_list, 'categories': categories})


@login_required
def upload_animation(request, proposal_id=None):
    user = request.user
    if is_email_checked(user) and user.is_authenticated():
        if request.method == 'POST':
            proposal = Animation.objects.get(id=proposal_id)
            anim_stats = UploadAnimationForm(request.POST or None,
                                            request.FILES or None)
            if anim_stats.is_valid():
                try:
                    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')
                except:
                    messages.warning(request, 'Please Upload a valid File')
                    return redirect('/edit_proposal/{}'.format(proposal_id))

            else:
                return redirect('/view_profile/')
    else:
        return redirect('/login/')


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 not in
                    ('pending',  '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 guidelines(request):
    categories = Category.objects.all()
    return render(request, 'fossee_manim/guidelines.html', {'categories': categories})

def about(request):
    categories = Category.objects.all()
    return render(request, 'fossee_manim/about.html', {'categories': categories})

def honorarium(request):
    categories = Category.objects.all()
    return render(request, 'fossee_manim/honorarium.html', {'categories': categories})

def faqs(request):
    categories = Category.objects.all()
    return render(request, 'fossee_manim/faqs.html', {'categories': categories})