示例#1
0
def find(self):
    str_skip = self.get_query_argument('skip')
    deactivated = self.get_query_argument('deactivated')
    search = self.get_query_argument('search')
    query = User.objects.order_by(
        'update_at', direction=DESCENDING).filter(status__ne='Deleted')
    get_all_flag = self.get_query_argument('get_all')

    if not deactivated:
        query.filter(status__ne='Deactivated')
    else:
        if search:
            q = {'$regex': '.*' + search + '.*', '$options': 'i'}
            arg = Q(first_name=q) | Q(last_name=q) | Q(email=q)
            query.filter(arg)

        skip_val = 0
        if str_skip:
            skip_val = int(str_skip)
        if get_all_flag is None:
            query.skip(skip_val).limit(15)

    users = yield query.find_all()

    self.render_json(users)
示例#2
0
    def filter(self, *arguments, **kwargs):
        '''
        Filters a queryset in order to produce a different set of document from subsequent queries.

        Usage::

            User.objects.filter(first_name="Bernardo").filter(last_name="Bernardo").find_all(callback=handle_all)
            # or
            User.objects.filter(first_name="Bernardo", starting_year__gt=2010).find_all(callback=handle_all)

        The available filter options are the same as used in MongoEngine.
        '''
        from motorengine.query_builder.node import Q, QCombination, QNot
        from motorengine.query_builder.transform import validate_fields

        if arguments and len(arguments) == 1 and isinstance(
                arguments[0], (Q, QNot, QCombination)):
            if self._filters:
                self._filters = self._filters & arguments[0]
            else:
                self._filters = arguments[0]
        else:
            validate_fields(self.__klass__, kwargs)
            if self._filters:
                self._filters = self._filters & Q(**kwargs)
            else:
                if arguments and len(arguments) == 1 and isinstance(
                        arguments[0], dict):
                    self._filters = Q(arguments[0])
                else:
                    self._filters = Q(**kwargs)

        return self
示例#3
0
    def test_gets_proper_query_when_or(self):
        query = Q(first_name="Someone") | Q(first_name="Else")
        query_result = query.to_query(User)

        expect(query_result).to_be_like(
            {'$or': [{
                'whatever': 'Someone'
            }, {
                'whatever': 'Else'
            }]})
示例#4
0
    def test_gets_proper_query_when_and(self):
        query = Q(embedded__test__lte="Test") & Q(first_name="Someone")
        query_result = query.to_query(User)

        expect(query_result).to_be_like({
            "embedded_document.other": {
                "$lte": "Test"
            },
            "whatever": "Someone"
        })
示例#5
0
    def test_can_query_using_not_in_with_real_users(self):
        self.create_test_users()

        names = ["Someone", "John"]

        query = ~Q(first_name__in=names) & Q(last_name="Heynemann")

        User.objects.filter(query).find_all(callback=self.stop)
        users = self.wait()

        expect(users).to_length(1)
        expect(users[0].first_name).to_equal("Bernardo")
def find(self):

    start_timestamp = self.get_query_argument('start')
    end_timestamp = self.get_query_argument('end')
    branch = self.get_query_argument('branch')

    events = []
    start_date = datetime.fromtimestamp(int(start_timestamp))
    end_date = datetime.fromtimestamp(int(end_timestamp))
    while start_date < end_date:
        # reg_day = start_date.strftime('%a').lower()
        # reg_scheds = yield InstructorSchedule.objects.filter(day=reg_day, type='regular').find_all()
        start_date_string = start_date.strftime('%Y-%m-%d')
        start_date_filter = datetime.strptime(start_date_string, '%Y-%m-%d')
        # for sched in reg_scheds:
        #     events.append({
        #         'title': sched.instructor.admin.first_name + "'s class \n( Regular )",
        #         'start': start_date_string + sched.start.strftime(' %H:%M:%S'),
        #         'end': start_date_string + sched.end.strftime(' %H:%M:%S'),
        #         'instructor': sched.instructor,
        #         'start_time': sched.start.strftime('%I:%M %p'),
        #         'end_time': sched.end.strftime('%I:%M %p'),
        #         'id': str(sched._id),
        #         'type': 'regular',
        #         'day': reg_day,
        #         'ridersCount': (yield BookedSchedule.objects.filter(status='booked', date=start_date_filter, schedule=sched._id).count())
        #     })
        branch_filter = Q(branch=ObjectId(branch))
        if branch == '558272c288b5c73163343c45':
            branch_filter = Q(branch=ObjectId(branch)) | Q(branch__exists=False)

        spec_scheds = yield InstructorSchedule.objects.filter(date=start_date_filter).filter(branch_filter).find_all()
        for sched in spec_scheds:
            events.append({
                'title': sched.type + ' with ' + sched.instructor.admin.first_name +
                        (' and ' + sched.sub_instructor.admin.first_name if sched.sub_instructor else ''),
                'start': start_date_string + sched.start.strftime(' %H:%M:%S'),
                'end': start_date_string + sched.end.strftime(' %H:%M:%S'),
                'instructor': sched.instructor,
                'sub_instructor': sched.sub_instructor,
                'start_time': sched.start.strftime('%I:%M %p'),
                'end_time': sched.end.strftime('%I:%M %p'),
                'id': str(sched._id),
                'type': sched.type,
                'seats': sched.seats,
                'ridersCount': (yield BookedSchedule.objects.filter(status='booked', date=start_date_filter, schedule=sched._id).count())
            })
        start_date += timedelta(days=1)

    self.render_json(events)
示例#7
0
    def test_gets_proper_query_when_many_operators(self):
        query = (Q(embedded__test__lte="Test")
                 & Q(first_name="Someone")) | Q(first_name="Else")
        query_result = query.to_query(User)

        expect(query_result).to_be_like({
            '$or': [{
                'embedded_document.other': {
                    '$lte': 'Test'
                },
                'whatever': 'Someone'
            }, {
                'whatever': 'Else'
            }]
        })
示例#8
0
    def test_can_query_using_q_with_is_null(self):
        self.create_test_users()

        User.objects.filter(
            Q(nullable__is_null=True)).find_all(callback=self.stop)
        users = self.wait()
        expect(users).to_length(1)
        expect(users[0]._id).to_equal(self.user._id)

        User.objects.filter(
            Q(nullable__is_null=False)).find_all(callback=self.stop)
        users = self.wait()
        expect(users).to_length(2)
        expect(users[0]._id).to_equal(self.user2._id)
        expect(users[1]._id).to_equal(self.user3._id)
示例#9
0
    def get(self, id=None, callback=None, alias=None, **kwargs):
        '''
        Gets a single item of the current queryset collection using it's id.

        In order to query a different database, please specify the `alias` of the database to query.
        '''

        from motorengine import Q

        if id is None and not kwargs:
            raise RuntimeError(
                "Either an id or a filter must be provided to get")

        if id is not None:
            if not isinstance(id, ObjectId):
                id = ObjectId(id)

            filters = {"_id": id}
        else:
            filters = Q(**kwargs)
            filters = self.get_query_from_filters(filters)

        self.coll(alias).find_one(filters,
                                  projection=self._loaded_fields.to_query(
                                      self.__klass__),
                                  callback=self.handle_get(callback))
示例#10
0
    def test_can_find_with_multiple_filters(self):
        yield from User.objects.create(email="*****@*****.**",
                                       first_name="Bernardo",
                                       last_name="Heynemann")

        yield from User.objects.create(email="*****@*****.**",
                                       first_name="Someone",
                                       last_name="Else")

        user = yield from User.objects.create(email="*****@*****.**",
                                              first_name="Bernardo",
                                              last_name="Heynemann")

        last_user = yield from User.objects.create(email="*****@*****.**",
                                                   first_name="Bernardo",
                                                   last_name="Silva")

        # filter and filter not
        users = yield from User.objects\
            .filter(email="*****@*****.**")\
            .filter_not(first_name="Someone").find_all()

        expect(users).to_be_instance_of(list)
        expect(users).to_length(1)

        first_user = users[0]
        expect(first_user._id).to_equal(user._id)

        # filter and filter not for Q
        from motorengine import Q
        users = yield from User.objects\
            .filter(email="*****@*****.**")\
            .filter_not(Q(first_name="Someone")).find_all()

        expect(users).to_be_instance_of(list)
        expect(users).to_length(1)

        first_user = users[0]
        expect(first_user._id).to_equal(user._id)

        # filter not and filter not
        users = yield from User.objects\
            .filter_not(last_name="Heynemann")\
            .filter_not(first_name="Someone").find_all()

        expect(users).to_be_instance_of(list)
        expect(users).to_length(1)

        first_user = users[0]
        expect(first_user._id).to_equal(last_user._id)

        # filter and filter
        users = yield from User.objects\
            .filter(last_name="Silva")\
            .filter(first_name="Bernardo").find_all()

        expect(users).to_be_instance_of(list)
        expect(users).to_length(1)
        expect(users[0]._id).to_equal(last_user._id)
示例#11
0
    async def get(self,
                  id=None,
                  alias=None,
                  lazy=False,
                  raw: bool = False,
                  **kwargs) -> Union[Dict, 'Document', None]:
        '''
        Gets a single item of the current queryset collection using it's id.

        In order to query a different database, please specify the `alias` of the database to query.
        Usage:
            user = Users.objects.get(ObjectId("x89831231313")) #get user by its id
            user = Users.objects.get("x89831231313") #get user by its id

            user = Users.objects.get(firstname="frank") #get one user by filter
            user = Users.objects.get(age__gt=30) #get one user by filter
        '''

        from motorengine import Q

        if id is None and not kwargs:
            raise RuntimeError(
                "Either an id or a filter must be provided to get")

        if id is not None:
            if not isinstance(id, ObjectId):
                id = ObjectId(id)

            filters = {"_id": id}
        else:
            filters = Q(**kwargs)
            filters = self.get_query_from_filters(filters)

        document_dict = await self.coll(alias).find_one(
            filters,
            projection=self._loaded_fields.to_query(self.__klass__),
        )

        if raw:
            return document_dict

        else:
            # return Document object for furthur operation such as remove, update
            if bool(document_dict) is False:
                return None

            else:
                document = self.__klass__.from_son(
                    document_dict,
                    # if _loaded_fields is not empty then
                    # document is partly loaded
                    _is_partly_loaded=bool(self._loaded_fields),
                    # set projections for references (if any)
                    _reference_loaded_fields=self._reference_loaded_fields)

                if (not lazy) or (not document.is_lazy):
                    await document.load_references()

                return document
示例#12
0
    def test_gets_proper_query_when_embedded_document_in_many_levels(self):
        query = Q(embedded__embedded2__test__lte="Test")
        query_result = query.to_query(User)

        expect(query_result).to_be_like(
            {"embedded_document.embedded2.else": {
                "$lte": "Test"
            }})
示例#13
0
    def test_gets_proper_query_when_embedded_document(self):
        query = Q(embedded__test__lte="Test")
        query_result = query.to_query(User)

        expect(query_result).to_be_like(
            {"embedded_document.other": {
                "$lte": "Test"
            }})
示例#14
0
    def test_can_query_using_q(self):
        self.create_test_users()

        User.objects.find_all(callback=self.stop)
        users = self.wait()
        expect(users).to_length(3)

        User.objects.filter(
            Q(first_name="Bernardo")).find_all(callback=self.stop)
        users = self.wait()
        expect(users).to_length(1)

        User.objects.filter(
            Q(first_name="Bernardo")
            | Q(first_name="Someone")).find_all(callback=self.stop)
        users = self.wait()
        expect(users).to_length(2)
示例#15
0
    def to_query(self):
        from motorengine import Q
        match_obj = {'$match': {}}

        query = self.aggregation.queryset.get_query_from_filters(Q(**self.filters))

        update(match_obj['$match'], query)

        return match_obj
示例#16
0
    def test_can_query_using_q_for_list(self):
        yield User.objects.create(
            email="*****@*****.**",
            first_name="Bernardo",
            last_name="Heynemann",
            embedded=EmbeddedDocument(test="test"),
            numbers=[1, 2, 3],
        )

        user2 = yield User.objects.create(
            email="*****@*****.**",
            first_name="Someone",
            last_name="Else",
            embedded=EmbeddedDocument(test="test2"),
            numbers=[4, 5, 6],
        )

        yield User.objects.create(
            email="*****@*****.**",
            first_name="John",
            last_name="Doe",
            embedded=EmbeddedDocument(test="test3"),
            numbers=[7, 8, 9],
        )

        users = yield User.objects.filter(Q(numbers=[4])).find_all()
        expect(users).to_length(1)
        expect(users[0]._id).to_equal(user2._id)

        users = yield User.objects.filter(Q(numbers=[5])).find_all()
        expect(users).to_length(1)
        expect(users[0]._id).to_equal(user2._id)

        users = yield User.objects.filter(Q(numbers=[6])).find_all()
        expect(users).to_length(1)
        expect(users[0]._id).to_equal(user2._id)

        users = yield User.objects.filter(Q(numbers=[4, 5, 6])).find_all()
        expect(users).to_length(1)
        expect(users[0]._id).to_equal(user2._id)

        users = yield User.objects.filter(Q(numbers=[20])).find_all()
        expect(users).to_length(0)
示例#17
0
    def test_can_query_using_not_in(self):
        names = ["Someone", "John"]

        query = ~Q(first_name__in=names)
        query_result = query.to_query(User)

        expect(query_result).to_be_like(
            {'whatever': {
                '$not': {
                    '$in': ['Someone', 'John']
                }
            }})
示例#18
0
    def distinct(self, key, filter=None, callback=None, alias=None, **kwargs):
        from motorengine.query_builder.node import Q, QNode

        if filter is not None and not isinstance(filter, QNode):
            filter = Q(**filter)

        if self._filters:
            filter = self._filters if filter is None else self._filters & filter

        if filter is not None:
            filter = self.get_query_from_filters(filter)

        self.coll(alias).distinct(
            key, filter, callback=self.handle_distinct(callback), **kwargs
        )
示例#19
0
    def test_can_create_new_instance_with_id(self):
        user = EmployeeWithId(id="12345", emp_number="mynumber")
        user.save(callback=self.stop)

        result = self.wait()

        expect(result._id).not_to_be_null()
        expect(result.id).to_equal("12345")

        for i in range(10):
            EmployeeWithId.objects.filter(
                Q(id="12345")).find_all(callback=self.stop)
            result = self.wait()

            emp = result[0]

            #expect(emp._id).not_to_be_null()
            expect(emp.id).to_equal("12345")
示例#20
0
    def filter_not(self, *arguments, **kwargs):
        '''
        Filters a queryset to negate all the filters passed in subsequent queries.

        Usage::

            User.objects.filter_not(first_name="Bernardo").filter_not(last_name="Bernardo").find_all(callback=handle_all)
            # or
            User.objects.filter_not(first_name="Bernardo", starting_year__gt=2010).find_all(callback=handle_all)

        The available filter options are the same as used in MongoEngine.
        '''
        from motorengine.query_builder.node import Q, QCombination, QNot

        if arguments and len(arguments) == 1 and isinstance(arguments[0], (Q, QCombination)):
            self.filter(QNot(arguments[0]))
        else:
            self.filter(QNot(Q(**kwargs)))

        return self
示例#21
0
    def test_gets_proper_query_when_query_operator_used(self):
        query = Q(first_name__lte="Test")
        query_result = query.to_query(User)

        expect(query_result).to_be_like({"whatever": {"$lte": "Test"}})
示例#22
0
    def test_gets_proper_type(self):
        query = Q(first_name="Test")
        expect(query).to_be_instance_of(Q)

        query = Q(first_name="Test") | Q(first_name="Else")
        expect(query).to_be_instance_of(QCombination)
示例#23
0
    def test_gets_proper_query(self):
        query = Q(first_name="Test")
        query_result = query.to_query(User)

        expect(query_result).to_be_like({"whatever": "Test"})
示例#24
0
def query(self, date, ins, branch):
    branch = str(branch) if isinstance(branch, ObjectId) else branch
    the_date = date
    gmt8 = GMT8()

    date = datetime.strptime(
        datetime.now().strftime('%Y-%m-%d') if not date else date, '%Y-%m-%d')

    counts = {}
    sched_releases = {}

    wr = yield Setting.objects.get(key='week_release')

    week_release = tornado.escape.json_decode(wr.value)
    week_release['date'] = datetime.strptime(
        re.sub('\+\d{2}:\d{2}$', '', week_release['date']),
        '%Y-%m-%d %H:%M:%S.%f')

    updateWeek = week_release['date'] + timedelta(
        days=-week_release['date'].weekday() + 6)
    updateWeek = updateWeek.replace(tzinfo=gmt8)

    days = {
        'mon': 0,
        'tue': 1,
        'wed': 2,
        'thu': 3,
        'fri': 4,
        'sat': 5,
        'sun': 6
    }

    rtime = week_release['time'].split(':')

    now = datetime.now(tz=gmt8)
    dDay = now.weekday()
    next_mon = now + timedelta(days=-dDay + 1, weeks=1)
    next_mon = next_mon.replace(tzinfo=gmt8)

    release_date = now + timedelta(days=-dDay + days[week_release['day']])
    release_date = release_date.replace(hour=int(rtime[0]),
                                        minute=int(rtime[1]),
                                        second=0,
                                        tzinfo=gmt8)

    next_release = release_date + timedelta(weeks=1)

    branch_filter = Q(branch=ObjectId(branch))
    if branch == DEFAULT_BRANCH_ID:
        branch_filter = Q(branch=ObjectId(branch)) | Q(branch__exists=False)

    scheds = {}
    week_days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun', 'nmon']
    for day in week_days:

        sched_query = InstructorSchedule.objects.filter(date=date)

        if branch:
            sched_query.filter(branch_filter)

        if ins:
            qfilter = Q(instructor=ObjectId(ins)) | Q(
                sub_instructor=ObjectId(ins))
            sched_query.filter(qfilter).limit(4)

        sched = yield sched_query.order_by(
            'start', direction=ASCENDING).find_all(lazy=True)
        if date.replace(tzinfo=gmt8) + timedelta(days=1) > now:
            for s in sched:
                sched_date = date.replace(hour=s.start.hour,
                                          minute=s.start.minute,
                                          second=0,
                                          tzinfo=gmt8)
                if (sched_date < now or sched_date > next_mon):
                    sched_releases[str(s._id)] = False
                elif updateWeek < now and (now < release_date and date.replace(
                        tzinfo=gmt8) > release_date) or (
                            now > release_date
                            and date.replace(tzinfo=gmt8) > next_release):
                    sched_releases[str(s._id)] = False
                else:
                    sched_releases[str(s._id)] = True

                if sched_date > now or sched_releases[str(s._id)]:
                    waitlist_count = (yield BookedSchedule.objects.filter(
                        status='waitlisted', schedule=s._id).count())
                    book_count = 0
                    if waitlist_count == 0:
                        book_count = (yield BookedSchedule.objects.filter(
                            status='booked', schedule=s._id).count())
                    counts[str(s._id)] = {
                        'books': book_count,
                        'waitlist': waitlist_count
                    }

        date += timedelta(days=1)

        if branch:
            default_branch = yield Branch.objects.get(ObjectId(branch))

            for i, s in enumerate(sched):
                if sched[i].branch is None:
                    sched[i].branch = default_branch

        scheds[day] = sched

    scheds['counts'] = counts
    scheds['releases'] = sched_releases
    scheds['now'] = str(now)
    scheds['date'] = the_date
    scheds['branch_id'] = branch

    return scheds
示例#25
0
    def test_gets_proper_query_when_list_field(self):
        query = Q(numbers=[10])
        query_result = query.to_query(User)

        expect(query_result).to_be_like({"numbers": {"$all": [10]}})