Example #1
0
File: views.py Project: TejasM/RTR
def end_session(request):
    if request.session.get('type') == 'creater':
        series = Session.objects.get(pk=int(request.session['session'])).series
        series.live = False
        series.save()
    elif request.session.get('type') == 'joiner':
        session = Session.objects.get(pk=int(request.session['session']))
        session.cur_num -= 1
        session.save()
        send_event('student-count-' + str(session.id), json.dumps({'count': session.cur_num}))

        statids = request.session.get('statids')
        if statids[-1] == ',':
            statids = statids[:len(statids) - 1]
        statids = statids.split(',')
        for stat_key in statids:
            stats = Stats.objects.get(pk=stat_key)
            stats.live = False
            stats.save()

    logout(request)
    request.session.clear()
    return redirect(reverse('rtr:index'))
Example #2
0
File: views.py Project: TejasM/RTR
def loginUser(request):
    #Collect post data
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    session = request.POST.get('session', '')
    typeSession = request.POST.get('type_session', '')
    typeLogin = request.POST.get('optionLogin', '')

    #Validate Post Data
    if session == '':
        messages.error(request, "Need to specify session id")

    user = None
    if typeLogin == 'registered' or (typeSession == "create" or typeSession == "view"):
        if username == '' or password == '':
            messages.error(request, "Need both password and username for registered login")
            return redirect(reverse('rtr:index'))
        #Check user authentication if required:
        else:
            user = authenticate(username=username, password=password)
            if user is not None:
                login(request, user)
            else:
                messages.error(request, "Incorrect Username and/or Password")
                return redirect(reverse('rtr:index'))

    #Redirect to where asked
    if typeSession == "create":
        try:
            series = Series.objects.get(series_id=session, live=False)
            series.live = True
            series.save()
            newSession = Session.objects.create(series=series, create_time=timezone.now(), stats_on="", user=user)
        except Series.DoesNotExist:
            try:
                Series.objects.get(series_id=session)
                messages.error(request, "Session already in progress")
                logout(request)
                return redirect(reverse('rtr:index'))
            except Series.DoesNotExist:
                newSeries = Series.objects.create(series_id=session, live=True)
                newSession = Session.objects.create(
                    series=newSeries, create_time=timezone.now(), stats_on="", user=user)
        request.session['session'] = str(newSession.id)
        request.session['session_name'] = session
        request.session['type'] = 'creater'
        return redirect(reverse("rtr:prof_settings"))

    elif typeSession == "join":
        try:
            series = Series.objects.get(series_id=session, live=True)
            session_name = session
            session = Session.objects.filter(series_id=series).latest('create_time')
            session.cur_num += 1
            if session.cur_num > session.max_num:
                session.max_num = session.cur_num
            session.save()
            send_event('student-count-' + str(session.id), json.dumps({'count': session.cur_num}))
            stats = session.stats_on.split(",")
            stat_ids = ""
            for stat in stats:
                newStats = Stats.objects.create(session=session, name=stat)
                stat_ids += str(newStats.id) + ","
            request.session['session_name'] = session_name
            request.session['session'] = str(session.id)
            request.session['type'] = 'joiner'
            request.session['statids'] = stat_ids
            user = authenticate(username="******", password="******")
            if user is not None:
                login(request, user)
            return redirect(reverse('rtr:audience_display'))
        except Series.DoesNotExist:
            messages.error(request, "Incorrect session, not currently running")
            if user is not None:
                logout(request)
            return redirect(reverse('rtr:index'))

    elif typeSession == 'view':
        try:
            try:
                series = Series.objects.get(series_id=session)
            except Series.DoesNotExist:
                messages.error(request, "No Such Session")
                return redirect(reverse('rtr:index'))
            request.session['session'] = str(session)
            return HttpResponseRedirect('/rtr/viewSeries/' + str(series.id))
        except Series.DoesNotExist:
            messages.error(request, "Incorrect session, not currently running")
            logout(request)
            return redirect(reverse('rtr:index'))
    else:
        messages.error(request, "Something Very Wrong Happened")
        if user is not None:
            logout(request)
        return redirect(reverse('rtr:index'))