def register(): form = bottle.request.form em = EntityManager() #check email if len(em.find('User',{'email':form.get_value('email')})) > 0: form.errors.append('The Email is already in use on this site, please select a different one') #check username if len(em.find('User',{'username':form.get_value('username')})) > 0: form.errors.append('The Username is already in use on this site, please select a different one') print form.errors if form.is_valid(): auth_service = AuthService(em) user = auth_service.create_user(form.get_value('email'), form.get_value('password'), form.get_value('username'),True) em.save('User', user) if getattr(settings, 'REGISTER_SUCCESS_URL', None): url = settings.REGISTER_SUCCESS_URL else: url = '/' return bottle.redirect(url) viewdata = { 'form':form.get_html(row_class='form-group', submit_btn_class="btn btn-primary", submit_btn_text='Register', submit_container_class='') } return bottle.template('register.tpl', vd=viewdata)
def save_item(item, newtagname): em = EntityManager() if item._id: #recall existing files for this item existingItem = em.find_one_by_id('Item', item._id) if existingItem is not None: for f in existingItem.files: item.files.append(f) if newtagname: existingTag = em.find_one('Tag', {'name':newtagname}) if existingTag: newTagId = existingTag._id else: t = Tag() t.name = newtagname t = em.save('Tag', t) newTagId = t._id if not item.tagIds: item.tagIds = [] item.tagIds.append(str(newTagId)) em.save('Item', item)
def index(session): form = bottle.request.form if form.is_valid(): i = form.hydrate_entity(Item()) #associate any uploaded files: em = EntityManager() for f in em.find('File', {'session_id': session['session_id']}): f.session_id = None em.save('File', f) i.files.append(f) save_item(i, form.get_value('newTag')) return bottle.redirect('/items') for item in form.formitems: if item.name == 'tagIds': item.select_list_items = [(str(t._id), t.name) for t in get_tags()] vd = common_view_data(session) vd.update({ 'form': form.get_html(row_class='form-group', submit_btn_class='btn btn-primary') }) return bottle.template('item.tpl', vd=vd)
def index(id, bool): em = EntityManager() i = em.find_one_by_id('Image', id) i.isHomepagePic = bool == '1' em.save('Image', i) return '1'
def index(session): form = bottle.request.form if form.is_valid(): em = EntityManager() t = form.hydrate_entity(Tag()) if EntityManager().find_raw('Tag', objfilter={'name': t.name}, count=True) == 0: em.save('Tag', t) return bottle.redirect('/tags') else: form.errors.append('A Tag with that name already exists') vd = common_view_data(session) vd.update({ 'form': form.get_html(row_class='form-group', submit_btn_class='btn btn-primary') }) return bottle.template('tag.tpl', vd=vd)
class EntityManagerTest(unittest.TestCase): def setUp(self): self.em = EntityManager() def save_entity(self, _id=None, title='Test', items=[]): i = Item() if _id: i._id = _id i.title = title i.content = 'Some content' i.tagIds = [1,2,3] i.added = datetime.now() i.items = items return self.em.save('Item', i) def test_save(self): entity = self.save_entity() #test 'save' returns the saved entity self.assertIsInstance(entity, Item) def test_find(self): self.save_entity(title='Test1') sleep(1) self.save_entity(title='Test2') items = self.em.find('Item') self.assertEqual(len(items), 2) items = self.em.find('Item', sort=[('added',1)]) self.assertEqual(items[0].title, 'Test1') self.assertEqual(items[1].title, 'Test2') items2 = self.em.find('Item', sort=[('added',-1)]) self.assertEqual(items2[0].title, 'Test2') self.assertEqual(items2[1].title, 'Test1') def test_find_raw_count(self): self.save_entity(title='Test1') self.save_entity(title='Test2') item_count = self.em.find_raw('Item', count=True) self.assertEqual(item_count, 2) def test_find_one_by_id(self): _id = ObjectId() self.save_entity(_id=_id) item = self.em.find_one_by_id('Item', _id) #test that an ObjectId _id returns correctly self.assertIsInstance(item, Item) item = self.em.find_one_by_id('Item', str(_id)) #test that a string _id returns correctly self.assertIsInstance(item, Item) def test_find_one(self): self.save_entity(title='Test1') self.save_entity(title='Test2') item = self.em.find_one('Item', {'title':'Test2'}) self.assertEqual(item.title, 'Test2') def test_nested_objects(self): self.save_entity(title='Test1', items = [Item(),Item(),Item()]) item = self.em.find_one('Item', {'title':'Test1'}) self.assertEqual(len(item.items), 3) for i in item.items: self.assertIsInstance(i, Item) def test_entity_to_json_safe_dict(self): i = Item() i.title = 'Nested Item' self.save_entity(title='Test1', items = [i,i,i]) item = self.em.find_one('Item', {'title':'Test1'}) jsondict = self.em.entity_to_json_safe_dict(item) self.assertIsInstance(jsondict, dict) self.assertEqual(jsondict['title'], 'Test1') #test nested object are still available self.assertEqual(jsondict['items'][0]['title'], 'Nested Item') #shouldnt be able to json serialise a datetime self.assertRaises(TypeError, json.dumps, item) #should be able to json serialise Item after entity_to_json_safe_dict call self.assertIsInstance(json.dumps(jsondict), str) def test_remove_one(self): self.save_entity(title='Test1') self.save_entity(title='Test2') self.save_entity(title='Test3') items = self.em.find('Item') self.assertEqual(len(items), 3) self.assertEqual(items[0].title, 'Test1') self.assertEqual(items[1].title, 'Test2') self.assertEqual(items[2].title, 'Test3') self.em.remove_one('Item', items[1]._id) items = self.em.find('Item') self.assertEqual(len(items), 2) self.assertEqual(items[0].title, 'Test1') self.assertEqual(items[1].title, 'Test3') def test_fuzzy_text_search(self): self.save_entity(title='These are some apples') self.save_entity(title='these are some pears') self.save_entity(title='theseare some bananas') # case insensitive items = self.em.fuzzy_text_search('Item', 'These', 'title') self.assertEqual(len(items), 3) items = self.em.fuzzy_text_search('Item', 'some apple', 'title') self.assertEqual(len(items), 3) items = self.em.fuzzy_text_search('Item', 'apples', 'title') self.assertEqual(len(items), 1) # spelling mistake items = self.em.fuzzy_text_search('Item', 'applss', 'title') self.assertEqual(len(items), 1) def tearDown(self): client = MongoClient(DBHOST, port=DBPORT) client[DBNAME].Item.drop()