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)})
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()
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)})
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)})
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)})
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)})
def patch(self, request): membership = Membership.objects.get(user=request.user) if not membership.is_owner: return not_permitted("Only owners can update the organisation") form = Form.for_model(Organisation).with_request(request) # Change the name if form.new_name: membership.organisation.change_name(form.new_name) # Change owner if form.new_owner: # Get the account for that user if not User.objects.filter(username=form.new_owner).exists(): return not_found(f"User {form.new_owner} doesn't exist") new_owner_user = User.objects.get(username=form.new_owner) # Check they are a member of this organisation if not Membership.objects.filter( user=new_owner_user, organisation=organisation).exists(): return not_found( f"User {form.new_owner} needs to be part of the organisation before becoming owner" ) new_owner_membership = Membership.objects.get( user=new_owner_user, organisation=organisation) if new_owner_membership.moderator: # Create the contract contract = Contract.make( contract_type=Contract.OWNERSHIP_TRANSFER, organisation=membership.organisation, user=new_owner_user) else: return not_permitted( "Only moderators can be transferred ownership") return accepted( {'organisation': OrganisationSerialiser.serialise(organisation)})