Example #1
0
def getUsers(request):
    userLists = []
    tmp = []

    for user in User.all():
        tmp.append(user)
        if len(tmp) == 16:
            userLists.append(tmp)
            tmp = []

    if tmp != []:
        userLists.append(tmp)

    users = []
    for lst in userLists:
        users.append(div(*map(UserCard, lst), cls="page-break row"))

    return HttpResponse(
        Page(
            h2("Users", cls="page-title"),
            div(cls="actions",
                contents=[
                    h.button("+ Create Admin",
                             cls="button button-blue create-admin",
                             onclick="createUser('admin')"),
                    h.button("+ Create Participant",
                             cls="button create-participant",
                             onclick="createUser('participant')")
                ]), div(cls="user-cards", contents=users)))
Example #2
0
def displayMessages(request, *args, **kwargs):
    user = User.getCurrent(request)

    messages = []
    if INBOX in request.path:
        if user.isAdmin():
            inbox = {}
            Message.forEach(lambda msg: inbox.update({msg.id: msg})
                            if msg.isAdmin else None)

            # Remove from inbox messages that have been responded to
            Message.forEach(lambda msg: inbox.pop(msg.replyTo)
                            if msg.replyTo in inbox else None)
            messages = list(inbox.values())
        else:
            Message.forEach(lambda msg: messages.append(msg)
                            if (msg.toUser and msg.toUser.id == user.id or msg.
                                fromUser == user or msg.isGeneral) else None)

    elif PROCESSED in request.path:

        def addReply(msg):
            if msg.replyTo in replies:
                replies[msg.replyTo].append(msg)
            else:
                replies[msg.replyTo] = [msg]

        # Find replies
        replies = {}
        Message.forEach(lambda msg: addReply(msg) if msg.replyTo else None)

        messages = [[Message.get(id)] + replies[id] for id in replies.keys()]

    elif ANNOUNCEMENT in request.path:
        Message.forEach(lambda msg: messages.append(msg)
                        if msg.isGeneral else None)

    if len(messages) > 0 and not isinstance(messages[0], list):
        messages = [[msg] for msg in messages]

    messages = [
        *map(lambda msglist: MessageCard(msglist, user),
             sorted(messages, key=lambda msglist: -msglist[0].timestamp))
    ]

    adminDetails = []
    if user.isAdmin():
        userOptions = [
            *map(lambda usr: h.option(usr.username, value=usr.id), User.all())
        ]
        adminDetails = [
            h.h5("To"),
            h.select(cls="form-control recipient",
                     contents=[h.option("general"), *userOptions]),
            h.input(type="hidden", id="replyTo"),
            h.h5("Message")
        ]

    if user.isAdmin():
        filter = div(
            a(href='inbox', contents="Inbox"),
            ' | ',
            a(href='processed', contents="Handled"),
            ' | ',
            a(href='announcements', contents="Announcements"),
        )
    else:
        filter = div()

    return HttpResponse(
        Page(
            h2("Messages", cls="page-title"),
            div(cls="actions",
                contents=[
                    h.button("+ Send Message",
                             cls="button create-message",
                             onclick="createMessage()")
                ]),
            filter,
            Modal(
                "Send Message",
                div(*adminDetails, h.textarea(cls="message col-12")),
                div(
                    h.button(
                        "Cancel", **{
                            "type": "button",
                            "class": "button button-white",
                            "data-dismiss": "modal"
                        }),
                    h.button(
                        "Send", **{
                            "type": "button",
                            "class": "button",
                            "onclick": "sendMessage()"
                        }))),
            div(cls="message-cards", contents=messages),
        ))