示例#1
0
文件: views.py 项目: timmygill/mapp
def get_friend_rooms():
    rooms = map(lambda name: flask_redis.hgetall(name),
                flask_redis.smembers("forresthill-rooms"))
    rooms = sorted(rooms, key=lambda x: x['key'])

    friends_rooms = set()
    if current_user.is_authenticated:
        for room in rooms:
            room_machines = flask_redis.lrange(room['key'] + "-machines", 0,
                                               -1)
            for machineName in room_machines:
                machine = flask_redis.hgetall(machineName)
                if current_user.has_friend(machine['user']):
                    uun = current_user.get_friend(machine['user'])
                    friends_rooms.add((uun, room['key'], room['name']))
        friends_rooms = list(friends_rooms)

        # uun -> name
        names = ldap.get_names([f[0] for f in friends_rooms])
        for i in range(len(friends_rooms)):
            uun, b, c = friends_rooms[i]
            if uun in names:
                friends_rooms[i] = {
                    'uun': uun,
                    'name': names[uun],
                    'room_key': b,
                    'room_name': c
                }

        friends_rooms.sort(key=lambda x: x['name'])

    return friends_rooms
示例#2
0
def filtered_courses(category_id):

    courses = Category.query.filter_by(id=category_id).first().courses

    if request.method == 'POST':
        filteredCourses = []
        filter = request.form['course_name_filter']
        if filter is not '':
            for crs in courses:
                if filter.lower() in crs.name.lower():
                    filteredCourses.append(crs)
            courses = filteredCourses

    categories = Category.query.all()
    title = Category.query.filter_by(id=category_id).first().ctg_name
    ens = current_user.enrollments
    goal_courses = []
    for enrollment in ens:
        if enrollment.user_id == current_user.id and enrollment.state == False:
            goal_courses.append(
                Course.query.filter_by(id=enrollment.course_id).first())

    friends = []
    frns_id = current_user.get_friend()
    for id in frns_id:
        friends.append(User.query.filter_by(id=id).first())

    enrolled_courses = []
    # count enrolled courses for current_user to draw chart
    labels = []
    for ctg in categories:
        labels.append(ctg.ctg_name)

    values = []
    for ctg in categories:
        i = 0
        for enrollment in ens:
            crs = Course.query.filter_by(id=enrollment.course_id).first()
            if crs.related_category == ctg:
                i = i + 1
            # enrolled courses for current_user
            if enrollment.user_id == current_user.id and enrollment.state == True:
                if crs not in enrolled_courses:
                    enrolled_courses.append(crs)

        values.append(i)
    colors = ["#F7464A", "#46BFBD", "#FDB45C"]

    return render_template('landing_page/all_courses.html',
                           title=title,
                           courses=courses,
                           enrolled_courses=enrolled_courses,
                           categories=categories,
                           goals=goal_courses,
                           friends=friends,
                           max=12,
                           labels=labels,
                           values=values,
                           set=zip(values, labels, colors))
示例#3
0
文件: views.py 项目: timmygill/mapp
def map_routine(which_room):
    room = flask_redis.hgetall(str(which_room))
    room_machines = flask_redis.lrange(room['key'] + "-machines", 0, -1)
    machines = {m: flask_redis.hgetall(m) for m in room_machines}
    num_rows = max([int(machines[m]['row']) for m in machines])
    num_cols = max([int(machines[m]['col']) for m in machines])

    num_machines = len(machines.keys())
    num_used = 0

    rows = []
    uuns = set()

    cascaders = get_cascaders()
    cascaders_here = set()

    for r in range(0, num_rows + 1):
        unsorted_cells = []
        for c in range(0, num_cols + 1):
            default_cell = {'hostname': None, 'col': c, 'row': r}
            cell = [
                v for (k, v) in machines.items()
                if int(v['row']) == r and int(v['col']) == c
            ]
            if not cell:
                cell = default_cell
            else:
                cell = cell[0]

            try:
                if cell['user'] is not "" or cell['status'] == "offline":
                    num_used += 1
            except Exception:
                pass

            if 'user' in cell:
                if cell['user'] == "":
                    del cell['user']
                else:
                    uun = find_cascader(cascaders, cell['user'])
                    if uun:
                        cascaders_here.add(uun)
                        cell["cascader"] = uun
                    # else:
                    # del cell["cascader"]

                    uun = current_user.get_friend(cell['user'])
                    if uun:
                        uuns.add(uun)
                        cell["user"] = uun
                    else:
                        cell["user"] = "******"

            unsorted_cells.append(cell)

        cells = unsorted_cells
        rows.append(cells)

    uuns.update(cascaders_here)

    uun_names = ldap.get_names(list(uuns))

    for y in range(len(rows)):
        for x in range(len(rows[y])):
            cell = rows[y][x]
            if "user" in cell:
                uun = cell["user"]
                if uun in uun_names:
                    rows[y][x]["friend"] = uun_names[uun]

            if "cascader" in cell:
                uun = cell["cascader"]
                if uun in uun_names:
                    rows[y][x]["cascader"] = uun_names[uun]

    num_free = num_machines - num_used

    low_availability = num_free <= 0.3 * num_machines

    last_update = float(flask_redis.get("last-update"))

    # Annotate friends with "here" if they are here
    room_key = room['key']
    friends = get_friend_rooms()
    friends_here, friends_elsewhere = (0, 0)
    for i in range(len(friends)):
        if friends[i]['room_key'] == room_key:
            friends[i]['here'] = True
            friends_here += 1
        else:
            friends_elsewhere += 1

    return {
        "friends":
        friends,
        "friends_here_count":
        friends_here,
        "friends_elsewhere_count":
        friends_elsewhere,
        "cascaders_here_count":
        len(cascaders_here),
        "cascaders_elsewhere_count":
        get_cascader_elsewhere_count(cascaders, which_room),
        "room":
        room,
        "rows":
        rows,
        "num_free":
        num_free,
        "num_machines":
        num_machines,
        "low_availability":
        low_availability,
        "last_update":
        last_update
    }
示例#4
0
def all_courses():

    courses = Course.query.all()

    if request.method == 'POST':
        if 'course_name_filter' in request.form:
            filteredCourses = []
            filter = request.form['course_name_filter']
            if filter is not '':
                for crs in courses:
                    if filter.lower() in crs.name.lower():
                        filteredCourses.append(crs)
                courses = filteredCourses
        if 'course_name_desc_filter' in request.form:
            filteredCourses = []
            filter = request.form['course_name_desc_filter']
            if filter is not '':
                for crs in courses:
                    if filter.lower() in crs.name.lower() or filter.lower(
                    ) in crs.description.lower():
                        filteredCourses.append(crs)
                courses = filteredCourses
        if 'goal_name_btn' in request.form:
            goal_id = request.form['goal_name_cancel']
            print('******************', goal_id,
                  '*************________________***********')
            g = Course.query.filter_by(id=goal_id).first()
            en = Enrollment.query.filter(
                and_(Enrollment.related_course == g,
                     Enrollment.related_user == current_user)).first()
            db.session.delete(en)
            db.session.commit()
            return redirect(url_for('all_courses'))
        if 'unfollow_btn' in request.form:
            friend_id = request.form['friend_name_unfollow']
            print('******************', friend_id,
                  '*************________________***********')
            current_user.remove_friend(friend_id)
            db.session.add(current_user)
            db.session.commit()
            return redirect(url_for('all_courses'))

    categories = Category.query.all()

    ens = current_user.enrollments
    goal_courses = []
    for enrollment in ens:
        if enrollment.user_id == current_user.id and enrollment.state == False:
            goal_courses.append(
                Course.query.filter_by(id=enrollment.course_id).first())

    friends = []
    frns_id = current_user.get_friend()
    for id in frns_id:
        friends.append(User.query.filter_by(id=id).first())

    # if request.method == 'POST':
    #     for category in categories:
    #         if category.ctg_name in request.form:
    #             courses = category.courses

    enrolled_courses = []
    # count enrolled courses for current_user to draw chart
    labels = []
    for ctg in categories:
        labels.append(ctg.ctg_name)

    values = []
    for ctg in categories:
        i = 0
        for enrollment in ens:
            crs = Course.query.filter_by(id=enrollment.course_id).first()
            if crs.related_category == ctg:
                i = i + 1
            # enrolled courses for current_user
            if enrollment.user_id == current_user.id and enrollment.state == True:
                if crs not in enrolled_courses:
                    enrolled_courses.append(crs)

        values.append(i)
    colors = ["#F7464A", "#46BFBD", "#FDB45C"]

    return render_template('landing_page/all_courses.html',
                           title='All Courses',
                           courses=courses,
                           enrolled_courses=enrolled_courses,
                           categories=categories,
                           goals=goal_courses,
                           friends=friends,
                           max=12,
                           labels=labels,
                           values=values,
                           set=zip(values, labels, colors))