Beispiel #1
0
    def post(self, request):
        form = Form.for_model(Organisation).with_request(request)
        membership = Membership.objects.get(user=request.user)

        if membership.organisation.enterprise:
            return conflict("User already part of an organisation")

        if Organisation.objects.filter(name=form.name).exists():
            return conflict(f"Organisation name {form.name} taken")

        if Organisation.objects.filter(owner=request.user,
                                       enterprise=True).exists():
            return conflict("User already owner of organisation")

        organisation = Organisation.make(name=form.name,
                                         description=form.description,
                                         founder=request.user,
                                         owner=request.user,
                                         enterprise=True,
                                         personal=False)

        membership.delete()
        new_membership = Membership.make(user=request.user,
                                         organisation=organisation,
                                         moderator=True,
                                         confirmed=True,
                                         origin=Membership.ORIGIN_OWNER)

        return created(
            {'organisation': OrganisationSerialiser.serialise(organisation)})
Beispiel #2
0
    def post(self, request):
        form = Form.for_model(Item).with_request(request)
        membership = Membership.objects.get(user=request.user)

        if not is_item_external_id_unique_to_organisation(
                form.external_id, membership.organisation):
            return conflict(
                "There is already an item with this id linked to your organisation"
            )

        item = Item.make(organisation=membership.organisation,
                         description=form.description,
                         external_id=form.external_id,
                         tags=form.tags,
                         photos=form.photos)

        return created({'item': ItemSerialiser.serialise(item)})
Beispiel #3
0
    def post(self, request):
        form = Form.for_model(Survey).with_request(request)
        membership = Membership.objects.filter(user=request.user).first()

        survey = Survey.make(
            organisation=membership.organisation,
            description=form.description,
            survey_type=form.type,
            status=form.status or "PLANNED"
        )

        if form.survey_items:
            for survey_item in form.survey_items:
                # TODO create a survey item
                pass

        return created({'survey': SurveySerialiser.serialise(survey)})
Beispiel #4
0
    def post(self, request, survey):
        form = Form.for_model(SurveyItem).with_request(request)

        if not Item.objects.filter(id=form.item_id).exists():
            return not_found("Item for given id not found")

        item = Item.objects.get(id=form.item_id)

        if SurveyItem.objects.filter(survey=survey, item=item).exists():
            return conflict("This item has already been surveyed as part of this survey")

        survey_item = SurveyItem.make(
            survey=survey,
            item=item,
            notes=form.notes
        )

        return created({'survey_item': SurveyItemSerialiser.serialise(survey_item)})
Beispiel #5
0
    def post(self, request, profile):
        """
        To add a member to an organisation, we create a contract which needs confirming by the other party before
        the membership is applied.
        """
        membership = Membership.objects.get(user=profile.user)

        if not membership.organisation.enterprise:
            return not_permitted("Personal organisation can't have additional members")

        if membership.moderator:
            contract = Contract.make(
                contract_type=Contract.INVITE,
                organisation=membership.organisation,
                user=membership.user
            )
        else:
            return not_permitted("Only moderators can add members")

        return created({
            'message': "Request sent to user",
            'contract': ContractSerialiser.serialise(contract)
        })