def test_programatically_add_row(self): """test appending different sized rows programatically""" #create some records self.db.put_record( Record({ "key1_1": "val1_1", "key1_2": "val1_2", "key1_3": "val1_3", "record_type": self.record_type })) self.db.put_record( Record({ "key1_1": "val2_1", "key1_2": "val2_2", "key1_3": "val2_3", "record_type": self.record_type })) #create a test widget with test database values cw = CouchGrid(self.dbname, record_type=self.record_type) #allow editing cw.append_row({"key1_1": "boo", "key1_2": "ray"}) #make sure there are three columns and two rows self.assertEqual(cw.get_model().get_n_columns(), 4) self.assertEqual(len(cw.get_model()), 3)
def test_selected_id_property(self): #create some records db = CouchDatabase(self.dbname, create=True) id1 = db.put_record( Record({ "key1_1": "val1_1", "key1_2": "val1_2", "key1_3": "val1_3", "record_type": self.record_type })) id2 = db.put_record( Record({ "key1_1": "val2_1", "key1_2": "val2_2", "key1_3": "val2_3", "record_type": self.record_type })) #build the CouchGrid cw = CouchGrid(self.dbname) cw.record_type = self.record_type #make sure the record ids are selected properly cw.selected_record_ids = [id1] self.assertEqual(cw.selected_record_ids[0], id1) cw.selected_record_ids = [id2] self.assertEqual(cw.selected_record_ids[0], id2)
def test_dont_delete_selected_rows(self): #create some records db = CouchDatabase(self.dbname, create=True) ids = [] for i in xrange(0, 10): ids.append( db.put_record( Record({ "key1_1": "val1_%s" % str(i), "iter_count": i, "record_type": self.record_type }))) #build the CouchGrid cw = CouchGrid(self.dbname, record_type=self.record_type) cw.selected_record_ids = [ids[0], ids[5], ids[9]] cw.remove_selected_rows(delete=False) cw.selected_record_ids = [ids[1], ids[4], ids[8]] cw.remove_selected_rows() self.assertEqual(self.db.get_record(ids[0]) is not None, True) self.assertEqual(self.db.get_record(ids[5]) is not None, True) self.assertEqual(self.db.get_record(ids[9]) is not None, True) self.assertEqual(self.db.get_record(ids[1]) is not None, True) self.assertEqual(self.db.get_record(ids[2]) is not None, True) self.assertEqual(self.db.get_record(ids[3]) is not None, True) self.assertEqual(self.db.get_record(ids[4]) is not None, True) self.assertEqual(self.db.get_record(ids[6]) is not None, True) self.assertEqual(self.db.get_record(ids[7]) is not None, True) self.assertEqual(self.db.get_record(ids[8]) is not None, True)
def _persist_dict_to_couch(self,dictionary): """ _persist_dict_to_couch - internal implementation. may be useful a subclass of CouchGrid, but not normally called directly. """ dictionary["record_type"] = self.record_type rec = Record(dictionary) #meh, best not to save an empty row if len(dictionary) > 1: doc_id = self._db.put_record(rec) dictionary["__desktopcouch_id"] = doc_id dictionary["__record_type"] = self.record_type del(dictionary["record_type"])
def test_single_col_from_database(self): #create some records self.db.put_record( Record({ "key1_1": "val1_1", "key1_2": "val1_2", "key1_3": "val1_3", "record_type": self.record_type })) self.db.put_record( Record({ "key1_1": "val2_1", "key1_2": "val2_2", "key1_3": "val2_3", "record_type": self.record_type })) #build the CouchGrid cw = CouchGrid(self.dbname) cw.keys = ["key1_1"] cw.record_type = self.record_type #make sure there are three columns and two rows self.assertEqual(cw.get_model().get_n_columns(), 2) self.assertEqual(len(cw.get_model()), 2)
def _load_preferences(self): # TODO: add preferences to the self._preferences dict default # preferences that will be overwritten if some are saved self._preferences = {"record_type": self._record_type} results = self._database.get_records(record_type=self._record_type, create_view=True) if len(results.rows) == 0: # No preferences have ever been saved, save them before returning. self._key = self._database.put_record(Record(self._preferences)) else: self._preferences = results.rows[0].value del self._preferences['_rev'] self._key = results.rows[0].value["_id"]
def test_optional_record_type_arg(self): """Test a simple creating a CouchGrid """ #create some records self.db.put_record( Record({ "key1_1": "val1_1", "key1_2": "val1_2", "key1_3": "val1_3", "record_type": self.record_type })) self.db.put_record( Record({ "key1_1": "val1_1", "key1_2": "val2_2", "key1_3": "val2_3", "record_type": self.record_type })) #create a test widget with test database values cw = CouchGrid(self.dbname, record_type=self.record_type) #make sure there are three columns and two rows self.assertEqual(cw.get_model().get_n_columns(), 4) self.assertEqual(len(cw.get_model()), 2)
def load(self): 'load from couchdb' self.update({"record_type": self._record_type}) results = self._database.get_records( record_type=self._record_type, create_view=True) if len(results.rows) == 0: # No preferences have ever been saved # save them before returning. self._key = self._database.put_record(Record(self)) else: self.update(results.rows[0].value) del self['_rev'] self._key = results.rows[0].value["_id"] self.emit('loaded', None)
def __load_preferences(self): #TODO: add prefernces to the self.__preferences dict #default preferences that will be overwritten if some are saved self.__preferences = {"record_type":self.__record_type, "wiiAddress": "00:17:AB:39:49:98", "filterSize": 15} results = self.__database.get_records(record_type=self.__record_type, create_view=True) # self.__key = self.__database.put_record(Record(self.__preferences)) if len(results.rows) == 0: #no preferences have ever been saved #save them before returning self.__key = self.__database.put_record(Record(self.__preferences)) else: self.__preferences = results.rows[0].value self.__key = results.rows[0].value["_id"]
def _edited(self, cell, path, new_val, col): """ _edited - internal signal handler. Updates the database if a cell in the Treeview has been edited. """ iter = self.list_store.get_iter(path) key = col.key dictionary = self.list_store.get_value(iter,len(self.keys)) if "__desktopcouch_id" not in dictionary: #the row has not been stored #create a document dictionary["record_type"] = self.record_type rec = Record(dictionary) doc_id = self._db.put_record(rec) dictionary["__desktopcouch_id"] = doc_id self.list_store.set_value(iter, len(self.keys), dictionary) else: #it has been saved #get the record id from the dictionary #then update the datbase with the change id = dictionary["__desktopcouch_id"] key = col.key self._db.update_fields(id,{key:new_val})
# [SNIPPET_DESCRIPTION: Fetch records from the desktop couch database] # [SNIPPET_AUTHOR: Huntly Cameron <*****@*****.**>] # [SNIPPET_DOCS: http://www.freedesktop.org/wiki/Specifications/desktopcouch/Documentation/SimpleGuide] # [SNIPPET_LICENSE: GPL] from desktopcouch.records.server import CouchDatabase from desktopcouch.records.record import Record #First, we want to put a record into a database and then get it out #create the database db = CouchDatabase("fetchrecordsexample", create=True) #Create some records record_type = "http://example.com/fetch-record-type.html" new_record = Record({ "afield": "a value", "anotherfield": "another value" }, record_type) another_record = Record( { "afield": "some value", "anotherfield": "some other value" }, record_type) #put our new records into the datbase db.put_record(new_record) db.put_record(another_record) #Fetch all the records in the database and display them results = db.get_records(record_type=record_type, create_view=True) for records in results:
# [SNIPPET_NAME: Add an attachment to a Desktopcouch Record] # [SNIPPET_CATEGORIES: DesktopCouch] # [SNIPPET_DESCRIPTION: Shows how to add an attachment to a Desktopcouch Record that will be stored and replicated by CouchDB] # [SNIPPET_AUTHOR: Manuel de la Pena <*****@*****.**>] # [SNIPPET_DOCS: http://www.themacaque.com/wiki/doku.php?id=desktopcouch:create_attachments] # [SNIPPET_LICENSE: GPL] import sys from desktopcouch.records.record import Record from desktopcouch.records.server import CouchDatabase # get the jpg to add from the command line if len(sys.argv) > 1: path = sys.argv[0] db = CouchDatabase("addattachment", create=True) record = Record(record_type="url") record.attach(path, "blob", "image/jpg") db.put_record(record) else: print "Please pass the path of the jpg to add." # got to /home/$USER/.local/share/desktop-couch/couchdb.html to see the # attached file
# [SNIPPET_NAME: Delete Records] # [SNIPPET_CATEGORIES: DesktopCouch] # [SNIPPET_DESCRIPTION: Delete records from the desktop couch database] # [SNIPPET_AUTHOR: Andy Breiner <*****@*****.**>] # [SNIPPET_DOCS: http://www.freedesktop.org/wiki/Specifications/desktopcouch/Documentation/SimpleGuide] # [SNIPPET_LICENSE: GPL] from desktopcouch.records.server import CouchDatabase from desktopcouch.records.record import Record #Similar to the fetchrecord.py by Huntly Cameron #See fetchrecord.py for additional information db = CouchDatabase("deleterecordsexample", create=True) record_type = "http://example.com/delete-record-type.html" record = Record({"first" : "Andy", "last" : "Breiner"}, record_type) db.put_record(record) record = Record({"first" : "Jono", "last" : "Bacon"}, record_type) db.put_record(record) results = db.get_records(record_type = record_type, create_view = True) for records in results: record = records.value print "First: %s" % record["first"] print "Last : %s" % record["last"] if record["first"] == "Andy": db.delete_record(record["_id"])