Example #1
0
def admin(request):
    """ Defines the administrator view for BearApps.
        Helpdesk and Financing officials are directed to this page.
    """
    if 'user' not in request.session:
        return HttpResponseRedirect('/')

    user = User.objects.get(name=request.session['user'])
    if user.user_type != "ADMIN":
        if user.user_type == "GENERAL":
            return HttpResponseRedirect('/browse/')
        return HttpResponseRedirect('/manage/')

    #don't paste any code above here!
    if request.method == 'POST':
        if "new" in request.POST:
            new_chartstring = Chartstring(
                nickname=request.POST['nickname'],
                chartstring=request.POST['chartstring'],
                budget=request.POST['amount'],
                remaining=request.POST['amount'],
                manager=user)
            new_chartstring.group = Group.objects.get(
                                    name=request.POST['group'])
            new_chartstring.save()

    all_users = User.objects.all()

    groups = Group.objects.all()
    user_groups = user.groups.all()
    # If an admin isn't in every group, add them.
    for group in groups:
        if group not in user_groups:
            user.groups.add(group)

    user_summary = [(member, member.user_apps_set.all())
                        for member in all_users if member != user]

    chartstrings = Chartstring.objects.all()

    messages = get_Notifications(user)

    con = Context({
        'username': user.name,
        'user_summary': user_summary,
        'chartstrings': chartstrings,
        'groups': groups,
        'messages': messages,
        'notifications': len(messages),
        })
    con.update(csrf(request))
    return render_to_response('admin.html', con)
Example #2
0
def classselect(request):
    """ Defines the registration view for first-time users.
        Note: the user will not be registered if they enter
        a username that already exists in our database.
    """
    con = Context()

    user = request.session['user']

    if "chooseclass" in request.POST:
        # Try clause checks if all fields are filled out.
        try:
            nickname=request.POST['chartstringname']
            chartstring = request.POST['chartstring']
            groupname = request.POST['groupname']
            if chartstring != '':
                userobj = User.objects.get(name=user)
                userobj.user_type = 'MANAGER'
		userobj.save()
                new_chartstring = Chartstring(
                    nickname=request.POST['chartstringname'],
                    chartstring=request.POST['chartstring'],
                    budget=request.POST['chartstringamount'],
                    remaining=request.POST['chartstringamount'],
                    manager=userobj)
                if groupname != '':
                    try:
                        add_group = Group.objects.get(name=groupname)
                    except ObjectDoesNotExist:
                        add_group = Group.objects.create(name=groupname)
                else:
                    try:
                        add_group = Group.objects.get(name=nickname)
                    except ObjectDoesNotExist:
                        add_group = Group.objects.create(name=nickname)
                userobj.groups.add(add_group)
                new_chartstring.group = add_group
                new_chartstring.save()
            else:
                if groupname != '':
                    try:
                        userobj = User.objects.get(name=user)
                        add_group = Group.objects.get(name=groupname)
                        userobj.groups.add(add_group)
                    except ObjectDoesNotExist:
                        con['does_not_exist'] = True
                        con.update(csrf(request))
                        return render_to_response('classselect.html', con)
                    else:
                        managers = User.objects.filter(
                            groups=add_group,
                            user_type="MANAGER")
                        for manager in managers:
                            add_Notification(
                                user=manager,
                                code="new_user",
                                info={'group': add_group, 'requestor': userobj})
        except (MultiValueDictKeyError, ObjectDoesNotExist):
            con['empty_fields'] = True
            con.update(csrf(request))
            return render_to_response('classselect.html', con)
	# Resets request.method, so that POST data is no longer stored.
        request.method = None

        if User.objects.get(name=user).user_type == 'GENERAL':
            return HttpResponseRedirect('/browse/')
        elif User.objects.get(name=user).user_type == 'ADMIN':
            return HttpResponseRedirect('/admin/')
        else:
            return HttpResponseRedirect('/manage/')
    elif "cancel" in request.POST:
        return HttpResponseRedirect('/')
    con.update(csrf(request))
    return render_to_response('classselect.html', con)
Example #3
0
def manage(request):
    """ Defines the manager view for BearApps. PIs and RSOs are
        directed to this view to manage their user requests and
        chartstrings/budgets.
    """
    # Setup sockets to notify license servers
    context = zmq.Context()
    socket = context.socket(zmq.PUB)
    socket.connect("tcp://127.0.0.1:23272")

    if 'user' not in request.session:
        return HttpResponseRedirect('/')

    user = User.objects.get(name=request.session['user'])
    if user.user_type != "MANAGER":
        if user.user_type == "GENERAL":
            return HttpResponseRedirect('/browse/')
        return HttpResponseRedirect('/admin/')
    # Sorts all groups & all users in alphabetical order.
    groups = sorted(user.groups.all(), key=lambda group: group.name)
    all_users = sorted(User.objects.all(), key=lambda user: user.name)
    # all_users.remove(user)

    # groups contains all of the manager's groups
    # all_users contains all of the users in the manager's groups excluding the manager
    if request.method == 'POST':
        if "approve" in request.POST:
            app = request.POST['app']
            price = App.objects.get(href_name=app).price
            chartstring = Chartstring.objects.get(
                chartstring=request.POST['chartstring'])
            user_requested = User.objects.get(SID=request.POST['user'])

            # Write change to database.
            app_object = App.objects.get(href_name=app)
            app = user_requested.user_apps_set.get(app=app_object)
            app.chartstring = chartstring
            chartstring.remaining = chartstring.remaining - price
            chartstring.save()
            app.date = date.today()
            app.status = 'APPROVED'
            app.save()

            add_Notification(user=user_requested,
                            info={'app': app_object},
                            code='approve')

            # Notify any subscribed servers of database change.
            socket.send_unicode(request.POST['app'])

        elif "revoke" in request.POST:
            app = request.POST['app']
            user_requested = User.objects.get(SID=request.POST['user'])
            # Write change to database.
            app_object = App.objects.get(href_name=app)
            app = user_requested.user_apps_set.get(app=app_object)
            app.delete()

            add_Notification(user=user_requested,
                            info={'app': app_object},
                            code='revoke')

            # Notify any subscribed servers of database change.
            socket.send_unicode(request.POST['app'])

        elif "reject" in request.POST:
            app = request.POST['app']
            app_object = App.objects.get(href_name=app)
            user_requested = User.objects.get(SID=request.POST['user'])
            app = user_requested.user_apps_set.get(app=app_object)
            app.delete()
            add_Notification(user=user_requested,
                            info={'app': app_object},
                            code='reject')

        elif "new" in request.POST:
            new_chartstring = Chartstring(
                nickname=request.POST['nickname'],
                chartstring=request.POST['chartstring'],
                budget=request.POST['amount'],
                remaining=request.POST['amount'],
                manager=user)
            new_chartstring.group = Group.objects.get(
                                    name=request.POST['group'])
            new_chartstring.save()
        elif "newgroup" in request.POST:
            try:
                new_group = Group(name=request.POST['groupname'])
                new_group.save()
                user.groups.add(new_group)
                user.save()
            except:
                new_group = Group.objects.get(name=request.POST['groupname'])
                user.groups.add(new_group)
                user.save()

        return HttpResponseRedirect('manage')

    chart_history = {chartstring: chartstring.user_apps_set.all() for chartstring in Chartstring.objects.all()}

    members = filter(lambda member: len(set(member.groups.all()).intersection(set(groups))) > 0, all_users)

    users_of_app = {}
    for member in members:
        chartstrings = filter(lambda chartstring: chartstring.group in user.groups.all() and chartstring.group in member.groups.all(), Chartstring.objects.all())
        user_apps = filter(lambda user_app: user_app.group in user.groups.all(), member.user_apps_set.all())

        for user_app in user_apps:
            if user_app.app in users_of_app:
                users_of_app[user_app.app].append((member, user_app.status, chartstrings))
            else:
                users_of_app[user_app.app] = [(member, user_app.status, chartstrings)]

    temp_chartstrings = []
    chartstrings = []

    for group in groups:
        temp_chartstrings = sorted(Chartstring.objects.filter(group=group),
                        key=lambda chartstring: chartstring.nickname.lower())
        for chartstring in temp_chartstrings:
            [chartstrings.append(chartstring) for chartstring in temp_chartstrings if not chartstrings.count(chartstring)]

    members_by_group = {group: [member for member in
                        User.objects.filter(groups=group)
                        if member != user] for group in groups}

    messages = get_Notifications(user)

    con = Context({
        'username': request.session['user'],
        'groups': groups,
        'users_of_app': users_of_app,
        'members_by_group': members_by_group,
        'chartstrings': chartstrings,
        'chart_history': chart_history,
        'messages': messages,
        'notifications': len(messages),
        })

    con.update(csrf(request))

    return render_to_response('manage.html', con)