Esempio n. 1
0
 def delete_documents(self, db_name, col_name):
     ids = self.request.arguments["selected-docs"]
     try:
         ids = [loads(urllib.unquote(doc))['_id'] for doc in ids]
     except InvalidId:
         pass
     yield Op(db[db_name][col_name].remove, {'_id': {'$in': ids}})
     self.redirect(url_for(self.select_collection, db_name=db_name, col_name=col_name))
Esempio n. 2
0
 def update_document(self, db_name, col_name, doc):
     """ Process the json-editor created json and place it in the db.
     Updates when the _id is set, else it creates a new document. """
     # TODO: use mongo's updateOrInsert?
     data = loads(self.get_argument('doc-data-field'))
     if doc:
         query = loads(urllib.unquote(doc))
         yield Op(db[db_name][col_name].update, query, data)
     else:
         yield Op(db[db_name][col_name].insert, data)
     self.redirect(url_for(self.select_collection, db_name=db_name, col_name=col_name))
Esempio n. 3
0
    def json_table(self, db_name, col_name):
        start = int(self.get_argument('iDisplayStart', 0))
        count = int(self.get_argument('iDisplayLength', 10))
        query_str = self.get_argument('query', None)
        map_str = self.get_argument('map', None)
        reduce_str = self.get_argument('reduce', None)

        if query_str:
            query = loads(query_str)
        else:
            query = None

        search = self.get_argument('sSearch', "").lower()

        if map_str and reduce_str:
            db_data = yield Op(db[db_name][col_name].inline_map_reduce, map_str, reduce_str, query=query)
            total = yield Op(db[db_name][col_name].count)
            filtered = len(db_data)
        else:
            cursor = db[db_name][col_name].find(query)
            total, filtered, db_data = yield [Op(db[db_name][col_name].count), \
                                             Op(cursor.count), \
                                             Op(cursor.limit(count).skip(start).to_list)]
        data = []
        for entry in db_data:
            if entry.has_key('_id'):
                _id = entry['_id']
                del(entry['_id'])
            else:
                _id = ''
            delete_checkbox = '<input title="Mark for deletion" type="checkbox" class="row-selector" name="selected-docs" value="{}"/>'.format(
                              urllib.quote(dumps({'_id': _id})))
            edit_button = '<a title="Edit this document" class="btn btn-mini" href="{}"><i class="icon-edit"></i></a>'.format(
                              url_for(self.edit_document, db_name=db_name, col_name=col_name, doc=urllib.quote(dumps({'_id': _id}))))
            row = [delete_checkbox, edit_button, str(_id), dumps(entry)]
            if search:
                for field in row:
                    if search in field.lower():
                        data.append(row)
                        break
            else:
                data.append(row)

        self.finish({
            "sEcho": int(self.get_argument('sEcho')),
            "iTotalRecords": total,
            "iTotalDisplayRecords": filtered,
            "aaData": data
        })
Esempio n. 4
0
 def delete_collection(self, db_name):
     collection = self.get_argument("col-name")
     yield Op(db[db_name][collection].drop)
     self.redirect(url_for(self.list_collections, db_name=db_name))
Esempio n. 5
0
 def create_collection(self, db_name):
     col_name = self.get_argument("col-name")
     yield Op(db[db_name].create_collection, col_name)
     self.redirect(url_for(self.list_collections, db_name=db_name))
Esempio n. 6
0
 def delete_database(self):
     db_name = self.get_argument("db-name")
     yield Op(db.drop_database, db_name)
     self.redirect(url_for(self.database_index))