Esempio n. 1
0
def create_members(request, microcosm_id):

    if request.method == 'POST':
        pass
    elif request.method == 'GET':
        offset = int(request.GET.get('offset', 0))

        microcosm_url, params, headers = Microcosm.build_request(request.get_host(), id=microcosm_id,
            offset=offset, access_token=request.access_token)
        request.view_requests.append(grequests.get(microcosm_url, params=params, headers=headers))

        try:
            responses = response_list_to_dict(grequests.map(request.view_requests))
        except APIException as exc:
            return respond_with_error(request, exc)

        microcosm = Microcosm.from_api_response(responses[microcosm_url])

        view_data = {
            'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None,
            'site': Site(responses[request.site_url]),
            'site_section': 'memberships',
            'content': microcosm,
            'item_type': 'memberships',
            'pagination': build_pagination_links(responses[microcosm_url]['items']['links'], microcosm.items)
        }
        return render(request, members_form_template, view_data)
Esempio n. 2
0
def single_microcosm(request, microcosm_id):

    # Pagination offset of items within the microcosm.
    try:
        offset = int(request.GET.get('offset', 0))
    except ValueError:
        offset = 0

    microcosm_url, params, headers = Microcosm.build_request(request.get_host(), id=microcosm_id,
                                                             offset=offset, access_token=request.access_token)
    request.view_requests.append(grequests.get(microcosm_url, params=params, headers=headers))
    try:
        responses = response_list_to_dict(grequests.map(request.view_requests))
    except APIException as exc:
        return respond_with_error(request, exc)

    microcosm = Microcosm.from_api_response(responses[microcosm_url])

    view_data = {
        'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None,
        'site': Site(responses[request.site_url]),
        'content': microcosm,
        'item_type': 'microcosm',
        'pagination': build_pagination_links(responses[microcosm_url]['items']['links'], microcosm.items)
    }

    return render(request, microcosm_single_template, view_data)
Esempio n. 3
0
def list_members(request, microcosm_id):
    try:
        offset = int(request.GET.get('offset', 0))
    except ValueError:
        offset = 0

    microcosm_url, params, headers = Microcosm.build_request(request.get_host(), id=microcosm_id,
        offset=offset, access_token=request.access_token)
    request.view_requests.append(grequests.get(microcosm_url, params=params, headers=headers))
    try:
        responses = response_list_to_dict(grequests.map(request.view_requests))
    except APIException as exc:
        return respond_with_error(request, exc)
    microcosm = Microcosm.from_api_response(responses[microcosm_url])

    roles_url, params, headers = RoleList.build_request(request.META['HTTP_HOST'], id=microcosm_id,
        offset=offset, access_token=request.access_token)
    request.view_requests.append(grequests.get(roles_url, params=params, headers=headers))
    try:
        responses = response_list_to_dict(grequests.map(request.view_requests))
    except APIException as exc:
        return respond_with_error(request, exc)
    roles = RoleList.from_api_response(responses[roles_url])

    view_data = {
        'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None,
        'site': Site(responses[request.site_url]),
        'site_section': 'memberships',
        'content': microcosm,
        'memberships': roles,
        'item_type': 'microcosm',
        'pagination': build_pagination_links(responses[roles_url]['roles']['links'], roles.items)
    }

    return render(request, members_list_template, view_data)
Esempio n. 4
0
def list_members(request, microcosm_id):
    try:
        offset = int(request.GET.get('offset', 0))
    except ValueError:
        offset = 0

    microcosm_url, params, headers = Microcosm.build_request(
        request.get_host(),
        id=microcosm_id,
        offset=offset,
        access_token=request.access_token)
    request.view_requests.append(
        grequests.get(microcosm_url, params=params, headers=headers))
    try:
        responses = response_list_to_dict(grequests.map(request.view_requests))
    except APIException as exc:
        return respond_with_error(request, exc)
    microcosm = Microcosm.from_api_response(responses[microcosm_url])

    roles_url, params, headers = RoleList.build_request(
        request.META['HTTP_HOST'],
        id=microcosm_id,
        offset=offset,
        access_token=request.access_token)
    request.view_requests.append(
        grequests.get(roles_url, params=params, headers=headers))
    try:
        responses = response_list_to_dict(grequests.map(request.view_requests))
    except APIException as exc:
        return respond_with_error(request, exc)
    roles = RoleList.from_api_response(responses[roles_url])

    view_data = {
        'user':
        Profile(responses[request.whoami_url], summary=False)
        if request.whoami_url else None,
        'site':
        Site(responses[request.site_url]),
        'site_section':
        'memberships',
        'content':
        microcosm,
        'memberships':
        roles,
        'item_type':
        'microcosm',
        'pagination':
        build_pagination_links(responses[roles_url]['roles']['links'],
                               roles.items)
    }

    return render(request, members_list_template, view_data)
Esempio n. 5
0
def confirm(request):
    """
    View for moderation actions on a single item.
    """

    if request.method == 'GET':
        if request.GET.get('item_type') == 'conversation':
            url, params, headers = Conversation.build_request(
                request.get_host(),
                request.GET.get('item_id'),
                access_token=request.access_token
            )
            request.view_requests.append(grequests.get(url, params=params, headers=headers))
            responses = response_list_to_dict(grequests.map(request.view_requests))
            content = Conversation.from_api_response(responses[url])

        elif request.GET.get('item_type') == 'event':
            url, params, headers = Event.build_request(
                request.get_host(),
                request.GET.get('item_id'),
                access_token=request.access_token
            )
            request.view_requests.append(grequests.get(url, params=params, headers=headers))
            responses = response_list_to_dict(grequests.map(request.view_requests))
            content = Event.from_api_response(responses[url])

        elif request.GET.get('item_type') == 'microcosm':
            url, params, headers = Microcosm.build_request(
                request.get_host(),
                request.GET.get('item_id'),
                access_token=request.access_token
            )
            request.view_requests.append(grequests.get(url, params=params, headers=headers))
            responses = response_list_to_dict(grequests.map(request.view_requests))
            content = Microcosm.from_api_response(responses[url])

        view_data = {
            'user': Profile(responses[request.whoami_url], summary=False),
            'site': Site(responses[request.site_url]),
            'content': content,
            'item_type': request.GET.get('item_type'),
            'action': request.GET.get('action'),
        }

        if request.GET.get('action') == 'move':
            # Fetch list of microcosms to supply in form.
            view_data['microcosms'] = MicrocosmList.retrieve(
                request.get_host(),
                access_token=request.access_token
            )

        return render(request, 'forms/moderation_item.html', view_data)
Esempio n. 6
0
def edit_members(request, microcosm_id, group_id):

    if request.method == 'POST':
        pass
    elif request.method == 'GET':
        try:
            offset = int(request.GET.get('offset', 0))
        except ValueError:
            offset = 0

        microcosm_url, params, headers = Microcosm.build_request(request.get_host(), id=microcosm_id,
            offset=offset, access_token=request.access_token)
        request.view_requests.append(grequests.get(microcosm_url, params=params, headers=headers))

        role_url, params, headers = Role.build_request(request.get_host(), microcosm_id=microcosm_id,
            id=group_id, offset=offset, access_token=request.access_token)
        request.view_requests.append(grequests.get(role_url, params=params, headers=headers))

        criteria_url, params, headers = RoleCriteriaList.build_request(request.get_host(),
            microcosm_id=microcosm_id, id=group_id, offset=offset, access_token=request.access_token)
        request.view_requests.append(grequests.get(criteria_url, params=params, headers=headers))

        profiles_url, params, headers = RoleProfileList.build_request(request.get_host(),
            microcosm_id=microcosm_id, id=group_id, offset=offset, access_token=request.access_token)
        request.view_requests.append(grequests.get(profiles_url, params=params, headers=headers))

        try:
            responses = response_list_to_dict(grequests.map(request.view_requests))
        except APIException as exc:
            return respond_with_error(request, exc)

        microcosm = Microcosm.from_api_response(responses[microcosm_url])
        role = Role.from_api_response(responses[role_url])
        criteria = RoleCriteriaList(responses[criteria_url])
        profiles = RoleProfileList(responses[profiles_url])

        view_data = {
            'user': Profile(responses[request.whoami_url], summary=False) if request.whoami_url else None,
            'site': Site(responses[request.site_url]),
            'site_section': 'memberships',
            'content': microcosm,
            'role': role,
            'criteria': criteria,
            'profiles': profiles,
            'item_type': 'memberships',
            'state_edit': True,
            'pagination': build_pagination_links(responses[microcosm_url]['items']['links'], microcosm.items)
        }

        return render(request, members_form_template, view_data)
Esempio n. 7
0
def root_microcosm(request):

    # Pagination offset of items within the microcosm.
    try:
        offset = int(request.GET.get('offset', 0))
    except ValueError:
        offset = 0

    microcosm_id = 0
    microcosm_url, params, headers = Microcosm.build_request(
        request.get_host(),
        id=microcosm_id,
        offset=offset,
        access_token=request.access_token)
    request.view_requests.append(
        grequests.get(microcosm_url, params=params, headers=headers))
    try:
        responses = response_list_to_dict(grequests.map(request.view_requests))
    except APIException as exc:
        return respond_with_error(request, exc)

    microcosm = Microcosm.from_api_response(responses[microcosm_url])

    view_data = {
        'user':
        Profile(responses[request.whoami_url], summary=False)
        if request.whoami_url else None,
        'site':
        Site(responses[request.site_url]),
        'content':
        microcosm,
        'item_type':
        'microcosm',
        'pagination':
        build_pagination_links(responses[microcosm_url]['items']['links'],
                               microcosm.items)
    }

    return render(request, microcosm_root_template, view_data)
Esempio n. 8
0
def edit_members(request, microcosm_id, group_id):

    if request.method == 'POST':
        pass
    elif request.method == 'GET':
        try:
            offset = int(request.GET.get('offset', 0))
        except ValueError:
            offset = 0

        microcosm_url, params, headers = Microcosm.build_request(
            request.get_host(),
            id=microcosm_id,
            offset=offset,
            access_token=request.access_token)
        request.view_requests.append(
            grequests.get(microcosm_url, params=params, headers=headers))

        role_url, params, headers = Role.build_request(
            request.get_host(),
            microcosm_id=microcosm_id,
            id=group_id,
            offset=offset,
            access_token=request.access_token)
        request.view_requests.append(
            grequests.get(role_url, params=params, headers=headers))

        criteria_url, params, headers = RoleCriteriaList.build_request(
            request.get_host(),
            microcosm_id=microcosm_id,
            id=group_id,
            offset=offset,
            access_token=request.access_token)
        request.view_requests.append(
            grequests.get(criteria_url, params=params, headers=headers))

        profiles_url, params, headers = RoleProfileList.build_request(
            request.get_host(),
            microcosm_id=microcosm_id,
            id=group_id,
            offset=offset,
            access_token=request.access_token)
        request.view_requests.append(
            grequests.get(profiles_url, params=params, headers=headers))

        try:
            responses = response_list_to_dict(
                grequests.map(request.view_requests))
        except APIException as exc:
            return respond_with_error(request, exc)

        microcosm = Microcosm.from_api_response(responses[microcosm_url])
        role = Role.from_api_response(responses[role_url])
        criteria = RoleCriteriaList(responses[criteria_url])
        profiles = RoleProfileList(responses[profiles_url])

        view_data = {
            'user':
            Profile(responses[request.whoami_url], summary=False)
            if request.whoami_url else None,
            'site':
            Site(responses[request.site_url]),
            'site_section':
            'memberships',
            'content':
            microcosm,
            'role':
            role,
            'criteria':
            criteria,
            'profiles':
            profiles,
            'item_type':
            'memberships',
            'state_edit':
            True,
            'pagination':
            build_pagination_links(responses[microcosm_url]['items']['links'],
                                   microcosm.items)
        }

        return render(request, members_form_template, view_data)
Esempio n. 9
0
def confirm(request):
    """
    View for moderation actions on a single item.
    """

    if request.method == 'POST':
        if request.POST.get('item_type') == 'conversation':
            url, params, headers = Conversation.build_request(
                request.get_host(),
                request.POST.get('item_id'),
                access_token=request.access_token)
            request.view_requests.append(
                grequests.get(url, params=params, headers=headers))
            responses = response_list_to_dict(
                grequests.map(request.view_requests))
            content = Conversation.from_api_response(responses[url])

        elif request.POST.get('item_type') == 'event':
            url, params, headers = Event.build_request(
                request.get_host(),
                request.POST.get('item_id'),
                access_token=request.access_token)
            request.view_requests.append(
                grequests.get(url, params=params, headers=headers))
            responses = response_list_to_dict(
                grequests.map(request.view_requests))
            content = Event.from_api_response(responses[url])

        elif request.POST.get('item_type') == 'microcosm':
            url, params, headers = Microcosm.build_request(
                request.get_host(),
                request.POST.get('item_id'),
                access_token=request.access_token)
            request.view_requests.append(
                grequests.get(url, params=params, headers=headers))
            responses = response_list_to_dict(
                grequests.map(request.view_requests))
            content = Microcosm.from_api_response(responses[url])

        view_data = {
            'user': Profile(responses[request.whoami_url], summary=False),
            'site': Site(responses[request.site_url]),
            'content': content,
            'item_type': request.POST.get('item_type'),
            'action': request.POST.get('action'),
        }

        if request.POST.get('action') == 'move':
            # Fetch list of microcosms to supply in form.
            microcosms = MicrocosmList.retrieve(
                request.get_host(), access_token=request.access_token)

            if request.POST.get('item_type') == 'microcosm':
                newlist = []
                nuke = content.id
                found = False
                for m in microcosms.microcosms:
                    if m['id'] == nuke:
                        found = True
                        nuke = m['id']
                    elif m.get('parent_id') and m['parent_id'] == nuke:
                        found = True
                        nuke = m['id']
                    else:
                        if found:
                            nuke = 0
                        newlist.append(m)
                microcosms.microcosms = newlist

            view_data['microcosms'] = microcosms

        return render(request, 'forms/moderation_item.html', view_data)
Esempio n. 10
0
def moderate(request):
    """
    View for moderation actions on a single item.
    """
    microcosm_id = request.POST.get('microcosm_id')

    if request.method == 'POST':

        if request.POST.get('action') == 'move':

            if request.POST.get('item_type') == 'event':

                event = Event()
                event.id = int(request.POST.get('item_id'))
                event.microcosm_id = int(microcosm_id)
                event.meta = {'editReason': 'Moderator moved item'}
                event.update(request.get_host(), request.access_token)

            elif request.POST.get('item_type') == 'conversation':

                conversation = Conversation()
                conversation.id = int(request.POST.get('item_id'))
                conversation.microcosm_id = int(microcosm_id)
                conversation.meta = {'editReason': 'Moderator moved item'}
                conversation.update(request.get_host(), request.access_token)

            elif request.POST.get('item_type') == 'microcosm':

                microcosm = Microcosm()
                microcosm.id = int(request.POST.get('item_id'))
                microcosm.parent_id = int(microcosm_id)
                microcosm.meta = {'editReason': 'Moderator moved item'}
                microcosm.update(request.get_host(), request.access_token)

        else:
            # These are all PATCH requests and we need the item in question first
            if request.POST.get('item_type') == 'conversation':
                url, params, headers = Conversation.build_request(
                    request.get_host(),
                    request.POST.get('item_id'),
                    access_token=request.access_token)
            if request.POST.get('item_type') == 'event':
                url, params, headers = Event.build_request(
                    request.get_host(),
                    request.POST.get('item_id'),
                    access_token=request.access_token)
            if request.POST.get('item_type') == 'microcosm':
                url, params, headers = Microcosm.build_request(
                    request.get_host(),
                    request.POST.get('item_id'),
                    access_token=request.access_token)

            # And then to execute the PATCH against the item
            if request.POST.get('action') == 'delete':
                payload = json.dumps([{
                    'op': 'replace',
                    'path': '/meta/flags/deleted',
                    'value': True
                }])
                headers['Content-Type'] = 'application/json'
                requests.patch(url, payload, headers=headers)

            elif request.POST.get('action') == 'undelete':
                payload = json.dumps([{
                    'op': 'replace',
                    'path': '/meta/flags/deleted',
                    'value': False
                }])
                headers['Content-Type'] = 'application/json'
                requests.patch(url, payload, headers=headers)

            elif request.POST.get('action') == 'approve':
                payload = json.dumps([{
                    'op': 'replace',
                    'path': '/meta/flags/moderated',
                    'value': False
                }])
                headers['Content-Type'] = 'application/json'
                requests.patch(url, payload, headers=headers)

            elif request.POST.get('action') == 'pin':
                payload = json.dumps([{
                    'op': 'replace',
                    'path': '/meta/flags/sticky',
                    'value': True
                }])
                headers['Content-Type'] = 'application/json'
                requests.patch(url, payload, headers=headers)

            elif request.POST.get('action') == 'unpin':
                payload = json.dumps([{
                    'op': 'replace',
                    'path': '/meta/flags/sticky',
                    'value': False
                }])
                headers['Content-Type'] = 'application/json'
                requests.patch(url, payload, headers=headers)

            elif request.POST.get('action') == 'open':
                payload = json.dumps([{
                    'op': 'replace',
                    'path': '/meta/flags/open',
                    'value': True
                }])
                headers['Content-Type'] = 'application/json'
                requests.patch(url, payload, headers=headers)

            elif request.POST.get('action') == 'close':
                payload = json.dumps([{
                    'op': 'replace',
                    'path': '/meta/flags/open',
                    'value': False
                }])
                headers['Content-Type'] = 'application/json'
                requests.patch(url, payload, headers=headers)

        return HttpResponseRedirect(
            reverse('single-microcosm', args=(microcosm_id, )))
Esempio n. 11
0
def confirm(request):
    """
    View for moderation actions on a single item.
    """

    if request.method == 'POST':
        if request.POST.get('item_type') == 'conversation':
            url, params, headers = Conversation.build_request(
                request.get_host(),
                request.POST.get('item_id'),
                access_token=request.access_token
            )
            request.view_requests.append(grequests.get(url, params=params, headers=headers))
            responses = response_list_to_dict(grequests.map(request.view_requests))
            content = Conversation.from_api_response(responses[url])

        elif request.POST.get('item_type') == 'event':
            url, params, headers = Event.build_request(
                request.get_host(),
                request.POST.get('item_id'),
                access_token=request.access_token
            )
            request.view_requests.append(grequests.get(url, params=params, headers=headers))
            responses = response_list_to_dict(grequests.map(request.view_requests))
            content = Event.from_api_response(responses[url])

        elif request.POST.get('item_type') == 'microcosm':
            url, params, headers = Microcosm.build_request(
                request.get_host(),
                request.POST.get('item_id'),
                access_token=request.access_token
            )
            request.view_requests.append(grequests.get(url, params=params, headers=headers))
            responses = response_list_to_dict(grequests.map(request.view_requests))
            content = Microcosm.from_api_response(responses[url])

        view_data = {
            'user': Profile(responses[request.whoami_url], summary=False),
            'site': Site(responses[request.site_url]),
            'content': content,
            'item_type': request.POST.get('item_type'),
            'action': request.POST.get('action'),
        }

        if request.POST.get('action') == 'move':
            # Fetch list of microcosms to supply in form.
            microcosms = MicrocosmList.retrieve(
                request.get_host(),
                access_token=request.access_token
            )

            if request.POST.get('item_type') == 'microcosm':
                newlist = []
                nuke = content.id
                found = False
                for m in microcosms.microcosms:
                    if m['id'] == nuke:
                        found = True
                        nuke = m['id']
                    elif m.get('parent_id') and m['parent_id'] == nuke:
                        found = True
                        nuke = m['id']
                    else:
                        if found:
                            nuke = 0
                        newlist.append(m)
                microcosms.microcosms = newlist


            view_data['microcosms'] = microcosms

        return render(request, 'forms/moderation_item.html', view_data)
Esempio n. 12
0
def moderate(request):
    """
    View for moderation actions on a single item.
    """
    microcosm_id = request.POST.get('microcosm_id')

    if request.method == 'POST':

        if request.POST.get('action') == 'move':

            if request.POST.get('item_type') == 'event':

                event = Event()
                event.id = int(request.POST.get('item_id'))
                event.microcosm_id = int(microcosm_id)
                event.meta = {'editReason': 'Moderator moved item'}
                event.update(request.get_host(), request.access_token)

            elif request.POST.get('item_type') == 'conversation':

                conversation = Conversation()
                conversation.id = int(request.POST.get('item_id'))
                conversation.microcosm_id = int(microcosm_id)
                conversation.meta = {'editReason': 'Moderator moved item'}
                conversation.update(request.get_host(), request.access_token)

            elif request.POST.get('item_type') == 'microcosm':

                microcosm = Microcosm()
                microcosm.id = int(request.POST.get('item_id'))
                microcosm.parent_id = int(microcosm_id)
                microcosm.meta = {'editReason': 'Moderator moved item'}
                microcosm.update(request.get_host(), request.access_token)

        else:
            # These are all PATCH requests and we need the item in question first
            if request.POST.get('item_type') == 'conversation':
                url, params, headers = Conversation.build_request(
                    request.get_host(),
                    request.POST.get('item_id'),
                    access_token=request.access_token
                )
            if request.POST.get('item_type') == 'event':
                url, params, headers = Event.build_request(
                    request.get_host(),
                    request.POST.get('item_id'),
                    access_token=request.access_token
                )
            if request.POST.get('item_type') == 'microcosm':
                url, params, headers = Microcosm.build_request(
                    request.get_host(),
                    request.POST.get('item_id'),
                    access_token=request.access_token
                )

            # And then to execute the PATCH against the item
            if request.POST.get('action') == 'delete':
                payload = json.dumps([{'op': 'replace', 'path': '/meta/flags/deleted', 'value': True}])
                headers['Content-Type'] = 'application/json'
                requests.patch(url, payload, headers=headers)

            elif request.POST.get('action') == 'undelete':
                payload = json.dumps([{'op': 'replace', 'path': '/meta/flags/deleted', 'value': False}])
                headers['Content-Type'] = 'application/json'
                requests.patch(url, payload, headers=headers)

            elif request.POST.get('action') == 'approve':
                payload = json.dumps([{'op': 'replace', 'path': '/meta/flags/moderated', 'value': False}])
                headers['Content-Type'] = 'application/json'
                requests.patch(url, payload, headers=headers)

            elif request.POST.get('action') == 'pin':
                payload = json.dumps([{'op': 'replace', 'path': '/meta/flags/sticky', 'value': True}])
                headers['Content-Type'] = 'application/json'
                requests.patch(url, payload, headers=headers)

            elif request.POST.get('action') == 'unpin':
                payload = json.dumps([{'op': 'replace', 'path': '/meta/flags/sticky', 'value': False}])
                headers['Content-Type'] = 'application/json'
                requests.patch(url, payload, headers=headers)

            elif request.POST.get('action') == 'open':
                payload = json.dumps([{'op': 'replace', 'path': '/meta/flags/open', 'value': True}])
                headers['Content-Type'] = 'application/json'
                requests.patch(url, payload, headers=headers)

            elif request.POST.get('action') == 'close':
                payload = json.dumps([{'op': 'replace', 'path': '/meta/flags/open', 'value': False}])
                headers['Content-Type'] = 'application/json'
                requests.patch(url, payload, headers=headers)

        return HttpResponseRedirect(reverse('single-microcosm', args=(microcosm_id,)))