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 signup(request):
    if request.user.is_authenticated:
        return conflict("Already logged in")

    if request.method == 'POST':
        form = Form.for_model(User).with_request(request)

        if User.objects.filter(username=form.username).exists():
            return conflict("This username already exists")

        user = User.objects.create_user(username=form.username,
                                        email=form.email,
                                        password=form.password)

        organisation = Organisation.make(owner=user,
                                         founder=user,
                                         personal=True,
                                         enterprise=False,
                                         name=str(uuid.uuid4()),
                                         description="Personal organisation")

        profile = Profile.objects.create(user=user)

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

        login(request, user)
        return ok({'message': 'User account successfully created'})
    else:
        return method_not_allowed()
Beispiel #3
0
    def patch(self, request, item):
        form = Form.for_model(Item).with_request(request)
        membership = Membership.objects.get(user=request.user)

        # Check if this item already exists for the organisation or profile
        if form.external_id:
            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.set_external_id(form.external_id)

        return accepted({'item': ItemSerialiser.serialise(item)})
Beispiel #4
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 #5
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)})