Beispiel #1
0
    def __get_data(self):
        query = {
            "$and": [{
                "$or": [{
                    "_deleted_at": {
                        "$exists": False
                    }
                }, {
                    "_deleted_at": None
                }]
            }, {
                "_attachments": {
                    "$ne": ""
                }
            }, {
                "_attachments": {
                    "$ne": []
                }
            }, {
                "$or": [{
                    "_attachments.download_url": {
                        "$regex": ".*media_file=media_file.*"
                    }
                }, {
                    "_attachments.download_small_url": {
                        "$exists": False
                    }
                }]
            }]
        }

        if self.__last_id is not None:
            query.update({"_id": {"$gt": self.__last_id}})

        return xform_instances.find(query).limit(self.BATCH_SIZE)
 def handle(self, *args, **kwargs):
     q = {"$and": [{"_deleted_at": {"$exists": True}},
          {"_deleted_at": {"$ne": None}}]}
     cursor = xform_instances.find(q)
     c = 0
     for record in cursor:
         date_deleted = datetime_from_str(record[DELETEDAT])
         id = record[ID]
         if Instance.set_deleted_at(id, deleted_at=date_deleted):
             c += 1
         print "deleted on ", date_deleted
     print "-------------------------------"
     print "Updated %d records." % c
Beispiel #3
0
 def handle(self, *args, **kwargs):
     q = {
         "$and": [{
             "_deleted_at": {
                 "$exists": True
             }
         }, {
             "_deleted_at": {
                 "$ne": None
             }
         }]
     }
     cursor = xform_instances.find(q)
     c = 0
     for record in cursor:
         date_deleted = datetime_from_str(record[DELETEDAT])
         id = record[ID]
         if Instance.set_deleted_at(id, deleted_at=date_deleted):
             c += 1
         print "deleted on ", date_deleted
     print "-------------------------------"
     print "Updated %d records." % c
def query_mongo(username,
                id_string,
                query,
                fields,
                sort,
                start=0,
                limit=DEFAULT_LIMIT,
                count=False,
                hide_deleted=True,
                fs_uuid=None,
                fs_project_uuid=None,
                site_id=None):
    USERFORM_ID = u'_userform_id'
    STATUS = u'_status'
    DEFAULT_BATCHSIZE = 1000

    fields_to_select = {USERFORM_ID: 0}
    # TODO: give more detailed error messages to 3rd parties
    # using the API when json.loads fails
    if isinstance(query, basestring):
        query = json.loads(query, object_hook=json_util.object_hook)
    query = query if query else {}
    query = dict_for_mongo(query)
    query[USERFORM_ID] = u'%s_%s' % (username, id_string)

    # check if query contains and _id and if its a valid ObjectID
    # if '_uuid' in query and ObjectId.is_valid(query['_uuid']):
    #     query['_uuid'] = ObjectId(query['_uuid'])

    query.pop('_userform_id')
    if fs_uuid is not None:
        query = {
            "$and": [
                query, {
                    "$or": [{
                        "_uuid": fs_uuid
                    }, {
                        "fs_uuid": fs_uuid
                    }, {
                        "_uuid": str(fs_uuid)
                    }, {
                        "fs_uuid": str(fs_uuid)
                    }]
                }
            ]
        }
        # query['_uuid'] = { '$in': [fs_uuid, str(fs_uuid)] } #fs_uuid
    if fs_project_uuid is not None:
        if site_id is None:
            query['fs_project_uuid'] = {
                '$in': [fs_project_uuid, str(fs_project_uuid)]
            }  #fs_project_uuid
        elif site_id and count:
            query['fs_project_uuid'] = {
                '$in': [fs_project_uuid, str(fs_project_uuid)]
            }
            #query = query.update({'fs_project_uuid': {'$in': [fs_project_uuid, str(fs_project_uuid)] }, 'fs_site': { '$in': [site_id, str(site_id)] }})
        elif site_id:
            query['fs_project_uuid'] = {
                '$in': [fs_project_uuid, str(fs_project_uuid)]
            }

    # if hide_deleted:
    #     # display only active elements
    #     # join existing query with deleted_at_query on an $and
    #     query = {"$and": [query, {"_deleted_at": None}]}

    # fields must be a string array i.e. '["name", "age"]'
    if isinstance(fields, basestring):
        fields = json.loads(fields, object_hook=json_util.object_hook)
    fields = fields if fields else []

    # TODO: current mongo (2.0.4 of this writing)
    # cant mix including and excluding fields in a single query
    if type(fields) == list and len(fields) > 0:
        fields_to_select = dict([(_encode_for_mongo(field), 1)
                                 for field in fields])
    if isinstance(sort, basestring):
        sort = json.loads(sort, object_hook=json_util.object_hook)
    sort = sort if sort else {}
    # if fs_uuid is not None:
    #     cursor = xform_instances.find({"$or":[ {"_uuid":fs_uuid}, {"fs_uuid":fs_uuid}, {"_uuid":str(fs_uuid)}, {"fs_uuid":str(fs_uuid)}]}, fields_to_select)
    # else:
    #     cursor = xform_instances.find({"$or":[ {"fs_project_uuid":fs_project_uuid}, {"fs_project_uuid": str(fs_project_uuid)}]}, fields_to_select)

    cursor = xform_instances.find(query, fields_to_select)
    if count:
        return [{"count": cursor.count()}]

    if start < 0 or limit < 0:
        raise ValueError(_("Invalid start/limit params"))

    cursor.skip(start).limit(limit)
    if type(sort) == dict and len(sort) == 1:
        sort_key = sort.keys()[0]
        # TODO: encode sort key if it has dots
        sort_dir = int(sort[sort_key])  # -1 for desc, 1 for asc
        cursor.sort(_encode_for_mongo(sort_key), sort_dir)
    # set batch size
    cursor.batch_size = DEFAULT_BATCHSIZE
    return cursor