Exemple #1
0
    async def post_poll(model: RequestPoll, user=Depends(
            authenticator.get_current_superuser)):
        inserted = []

        for question in model.questions:
            uuid: Binary = generate_uuid()
            created_at: datetime = datetime.utcnow()
            ngram_forms = [x['value'] for x in question.forms if x]

            question_doc: Question = {
                '_id': uuid,
                'user_id': user.id,
                'created_at': created_at,
                'question': question.question,
                'forms': question.forms,
                'type': question.type,
                'ngrams': make_ngrams([question.question, ngram_forms])
            }

            res = await database['questions'].insert_one(question_doc)

            if res.acknowledged:
                inserted.append(UUID(bytes=res.inserted_id))

        if len(inserted) > 0:
            document = {'_id': generate_uuid(), 'questions': inserted}
            await database['polls'].insert_one(document)
Exemple #2
0
    async def post_event(request: Event,
                         user=Depends(authenticator.get_current_superuser)):
        event_times = generate_interval(request)
        request_doc = dict(request)
        inserted = []

        for event_time in event_times:
            uuid: Binary = generate_uuid()
            created_at: datetime = datetime.utcnow()

            event_doc: Event = {
                '_id': uuid,
                'author': user.name,
                'created_at': created_at,
                'event_time': event_time,
                'titles': request_doc['titles'],
                'location': request_doc['location'],
                'repeat_unit': request_doc['repeat_unit'],
                'descriptions': request_doc['descriptions'],
                'repeat_interval': request_doc['repeat_interval'],
                'min_participants': request_doc['min_participants'],
                'max_participants': request_doc['max_participants'],
                'contact_person': request_doc['contact_person'],
                'start_time': request_doc['start_time'],
                'end_time': request_doc['end_time'],
                'ngrams': make_ngrams([
                    request_doc['contact_person'],
                    request_doc['descriptions'],
                    request_doc['repeat_unit'],
                    request_doc['location'],
                    request_doc['titles']]
                )
            }

            res = await database['events'].insert_one(event_doc)

            if res.acknowledged:
                inserted.append(res.inserted_id)

        uuid: Binary = generate_uuid()

        calendar_doc: Calendar = {
            '_id': uuid,
            'events': inserted,
            'start_time': request_doc['start_time'],
            'end_time': request_doc['end_time']
        }

        res = await database['calendars'].insert_one(calendar_doc)

        if res.acknowledged:
            return {'detail': 'Events successfully created'}
        else:
            raise HTTPException(
                status_code=400, detail='Error creating calendar')
Exemple #3
0
    async def post_template(topic,
                            locale,
                            model: Template,
                            user=Depends(authenticator.get_current_superuser)):
        d = dict(model)
        d['_id'] = generate_uuid()
        d['topic'] = topic
        d['locale'] = locale
        d['ngrams'] = make_ngrams([d['subject'], d['message']])

        res = await database['templates'].insert_one(d)

        if res.acknowledged:
            return UUID(bytes=res.inserted_id)
        else:
            raise HTTPException(status_code=400,
                                detail='Mail template not created')
Exemple #4
0
    async def patch_poll(hex, model: RequestPoll, user=Depends(
            authenticator.get_current_superuser)):
        try:
            req_uuid = UUID(hex=hex, version=4)
        except (TypeError, ValueError):
            raise HTTPException(
                status_code=404, detail='Invalid identifier')

        updated = []

        for question in model.questions:
            ngram_forms = [x['value'] for x in question.forms if x]

            poll_doc: UpdateQuestion = {}
            poll_doc['modified_at']: datetime = datetime.utcnow()
            poll_doc['forms']: List[Dict[str, str]] = question.forms
            poll_doc['question']: str = question.question
            poll_doc['type']: str = question.type
            poll_doc['ngrams'] = make_ngrams([question.question, ngram_forms])

            query = {'_id': req_uuid}
            document = {'$set': poll_doc}

            res = await database['questions'].update_one(query, document)

            if res.acknowledged:
                updated.append(res.acknowledged)
            else:
                pass

        if len(updated) > 0:
            query = {'_id': req_uuid, 'questions': {'$in': updated}}
            document = {'$addToSet': {'questions': {'$each': updated}}}

            res = await database['polls'].update_one(query, document)

            if res.acknowledged:
                return {'detail': 'Updated poll'}

        raise HTTPException(status_code=400, detail='Error updating poll')
Exemple #5
0
    async def patch_template(hex,
                             model: UpdateTemplate,
                             user=Depends(
                                 authenticator.get_current_superuser)):
        try:
            uuid = UUID(hex=hex, version=4)
        except TypeError:
            raise HTTPException(status_code=404,
                                detail='Mail template not found')

        d = dict(model)
        d['ngrams'] = make_ngrams([d['subject'], d['message']])
        document = {'$set': d}
        query = {'_id': uuid}

        res = await database['templates'].update_one(query, document)

        if res.modified_count > 0:
            return uuid
        else:
            raise HTTPException(status_code=400,
                                detail='Error updating template')
Exemple #6
0
    async def patch_event(hex: str,
                          request: UpdateEvent,
                          user=Depends(authenticator.get_current_superuser)):
        try:
            req_uuid = UUID(hex=hex, version=4)
        except (TypeError, ValueError):
            raise HTTPException(status_code=404, detail='Invalid identifier')

        query = {'events': {'$in': [req_uuid]}}
        res = await database['calendars'].find(query).to_list(length=1)

        if len(res) == 0:
            raise HTTPException(status_code=404,
                                detail='Calender was not found')

        updated = []
        inserted = []
        deleted = []

        event_times = generate_interval(request)
        existing_events = len(res[0]['events'])
        interval_events = len(event_times)

        for event_time_index, event_time in enumerate(event_times):
            modified_at: datetime = datetime.utcnow()
            request_doc = dict(request)

            try:
                poll_id = UUID(hex=request_doc['poll_id'])
            except (TypeError, ValueError):
                poll_id = None

            event_doc: Event = {
                '_id':
                uuid,
                'creator':
                user.id,
                'created_at':
                created_at,
                'title':
                request_doc['title'],
                'location':
                request_doc['location'],
                'repeat_unit':
                request_doc['repeat_unit'],
                'description':
                request_doc['description'],
                'repeat_interval':
                request_doc['repeat_interval'],
                'max_participants':
                request_doc['max_participants'],
                'readaccess':
                request_doc['readaccess'],
                'start_time':
                request_doc['start_time'],
                'end_time':
                request_doc['end_time'],
                'poll_id':
                poll_id,
                'ngrams':
                make_ngrams([
                    request_doc['contact_person'], request_doc['descriptions'],
                    request_doc['repeat_unit'], request_doc['location'],
                    request_doc['titles']
                ])
            }

            if existing_events > 0:
                event_doc['modified_by'] = user.id
                event_doc['modified_at'] = modified_at

                document = {'$set': event_doc}
                query = {'_id': {'$in': [res[0]['events'][event_time_index]]}}

                update_res = await database['events'].update_many(
                    query, document)

                if update_res.acknowledged:
                    updated.append(res[0]['events'][event_time_index])
            elif existing_events < 0 and existing_events < interval_events:
                created_at: datetime = datetime.utcnow()
                uuid: Binary = generate_uuid()

                event_doc['_id'] = uuid
                event_doc['author'] = user.name
                event_doc['created_at'] = created_at

                insert_res = await database['events'].insert_one(event_doc)

                if insert_res.acknowledged:
                    inserted.append(insert_res.inserted_id)

            existing_events = existing_events - 1
        interval_events = interval_events - 1

        delete_uuids = set(res[0]['events']) - set(updated)

        if len(delete_uuids) > 0:
            for uuid in delete_uuids:
                query = {'_id': uuid}

                delete_res = await database['events'].delete_one(query)

                if delete_res.deleted_count > 0:
                    deleted.append(uuid)

        if len(inserted) > 0:
            calendar_doc: Calendar = {
                'start_time': request_doc['start_time'],
                'end_time': request_doc['end_time']
            }

            query = {'events': {'$in': [req_uuid]}}
            document = {
                '$set': calendar_doc,
                '$addToSet': {
                    'events': {
                        '$each': inserted
                    }
                }
            }

            await database['calendars'].update_one(query, document)

        if len(updated) > 0:
            calendar_doc: Calendar = {
                'start_time': request_doc['start_time'],
                'end_time': request_doc['end_time']
            }

            query = {'events': {'$in': [req_uuid]}}
            document = {'$set': calendar_doc}

            await database['calendars'].update_one(query, document)

        if len(deleted) > 0:
            calendar_doc: Calendar = {
                'start_time': request_doc['start_time'],
                'end_time': request_doc['end_time']
            }

            query = {'events': {'$in': [req_uuid]}}
            document = {'$pullAll': {'events': deleted}}

            await database['calendars'].update_one(query, document)

        if len(updated) > 0 or len(inserted) > 0 or len(deleted) > 0:
            return {
                'updated': len(updated),
                'inserted': len(inserted),
                'deleted': len(deleted)
            }
        else:
            raise HTTPException(status_code=400, detail='Calendar not updated')