def get_street(code): obj = Dictionary(STREET_CODE) find = {'identcode': code} try: result = obj.get_list(find) except ValueError, e: raise InvalidAPIUsage(e.message, status_code=404)
def get_city(code): obj = Dictionary(CITY_CODE) find = {'identcode': code} try: cities = obj.get_list(find) except ValueError, e: raise InvalidAPIUsage(e.message, status_code=404)
def search_city(value, limit=None): obj = Dictionary(CITY_CODE) find = { 'is_actual': '1', '$or': [{ 'name': prepare_find_params(value) }, { 'identcode': value }] } try: cities = obj.get_list(find, 'level', limit) except ValueError, e: raise InvalidAPIUsage(e.message, status_code=404)
def doc_edit(code, _id): dict_names = DictionaryNames() current = dict_names.get_by_code(code) linked = None linked_code = None linked_docs = [] linked_dict = dict() linked_dicts = dict_names.get_list({'version': {'$exists': True}}) obj = Dictionary(code) document = obj.get_document({'_id': _id}) linked_dict = _get_linked_dict(document, current) if linked_dict: linked_code = linked_dict['code'] linked = Dictionary(linked_code) linked_docs = linked.get_list() if request.method == 'POST': data = dict() if request.form: for key, value in request.form.items(): data[key] = value linked_id = data.pop('linked') _post_linked_code = data.pop('linked_collection') data.update({'_id': _id}) if _post_linked_code: if _post_linked_code != linked_code: linked_code = _post_linked_code data.update({'linked_collection': linked_code}) else: obj.unset_attr(_id, 'linked_collection') if linked_id: linked = Dictionary(linked_code) linked_document = linked.get_document({'_id': linked_id}) data.update({linked_code: linked_document}) else: obj.unset_attr(_id, linked_code) obj.unset_attr(_id, 'linked_collection') try: obj.add_document(data) except Exception, e: flash(e, 'error') else: flash(u'Документ сохранён', 'info') return redirect(url_for('.doc_edit', code=code, _id=_id))
def dict_view(_id): obj = DictionaryNames() info = obj.get_by_id(_id) collections = Collections() if not info or 'code' not in info or info['code'] not in collections.get_list(): flash(u'Коллекция не существует или пуста') abort(404) collection = Dictionary(info['code']) data = obj.get_list({'version': {'$exists': True}}) fields = _get_fields(info['code']) try: return render_template('{0}/dict_view.html'.format(module.name), info=info, data=data, fields=fields, documents=collection.get_list(sort='id', limit=1000)) except TemplateNotFound: abort(404)
def search_street(city_code, value, limit=None): obj = Dictionary(STREET_CODE) prepared = prepare_find_params(value) find = { 'identparent': city_code, 'is_actual': '1', '$or': [{ 'name': prepared }, { 'fulltype': prepared }, { 'shorttype': prepared }, { 'identcode': value }] } try: result = obj.get_list(find, limit=limit) except ValueError, e: raise InvalidAPIUsage(e.message, status_code=404)
class DictionaryTestCase(unittest.TestCase): def setUp(self): app.config['TESTING'] = True app.config['MONGODB_DB'] = TEST_DB self.app = app.test_client() self.collection = 'test_dictionary' self.obj = Dictionary(self.collection) def drop_collection(self): db.drop_collection(self.collection) def tearDown(self): db.drop_collection(self.collection) def test_get_list_empty(self): self.drop_collection() with self.assertRaises(ValueError): result = self.obj.get_list() #self.assertEqual(result.count(), 0) def test_add_document(self): data = dict(code='test', name=u'Тестовый документ') _id = self.obj.add_document(data) self.assertIsNotNone(_id) result = self.obj.get_document(dict(_id=_id)) data.update(dict(_id=_id)) self.assertDictEqual(data, result) def test_add_documents(self): self.drop_collection() data = [dict(name=u'Документ1', code='document1'), dict(name=u'Документ2'), dict(name=u'Документ3', alias='document2'), dict(name=u'Документ4', code='document4', created=datetime.now())] _ids = self.obj.add_documents(data) self.assertIsNotNone(_ids) self.assertIsInstance(_ids, list) result = list(self.obj.get_list()) self.assertEqual(len(result), len(data)) self.assertEqual(result[0]['code'], data[0]['code']) data_2 = data[2] data_2.update(dict(_id=_ids[2])) self.assertEqual(result[2], data_2) def test_get_document(self): data = dict(code='test5', name=u'Тестовый документ5') _id = self.obj.add_document(data) self.assertIsNotNone(_id) result = self.obj.get_document(dict(_id=_id)) data.update(dict(_id=_id)) self.assertDictEqual(data, result) def test_count_documents(self): self.drop_collection() data = [dict(name=u'Документ1', code='document1'), dict(name=u'Документ2'), dict(name=u'Документ3', alias='document2'), dict(name=u'Документ4', code='document4', created=datetime.now())] _ids = self.obj.add_documents(data) self.assertIsNotNone(_ids) self.assertIsInstance(_ids, list) result = self.obj.count() self.assertEqual(result, len(data)) result = self.obj.count(dict(_id=_ids[0])) self.assertEqual(result, 1) result = self.obj.count(dict(_id='not_exists_id')) self.assertEqual(result, 0) def test_delete(self): data = dict(code='test_delete', name=u'Тестовый документ') _id = self.obj.add_document(data) self.assertIsNotNone(_id) self.obj.delete(_id) result = self.obj.get_document(dict(_id=_id)) self.assertIsNone(result) def test_exists(self): self.drop_collection() data = [dict(name=u'Документ1', code='document1'), dict(name=u'Документ2'), dict(name=u'Документ3', alias='document2'), dict(name=u'Документ4', code='document4', created=datetime.now())] _ids = self.obj.add_documents(data) self.assertIsNotNone(_ids) self.assertIsInstance(_ids, list) result = self.obj.exists() self.assertTrue(result) result = self.obj.count(dict(_id=_ids[0])) self.assertTrue(result) result = self.obj.count(dict(_id='not_exists_id')) self.assertFalse(result)
def list_documents(self, code, find=None): obj = Dictionary(code) try: result = obj.get_list(find) except ValueError, e: raise InvalidAPIUsage(e.message, status_code=404)
def _get_fields(code): obj = Dictionary(code) document = list(obj.get_list(limit=1)) if document[0]: return sorted(document[0].keys()) return []
def get_documents(code=None): if code: obj = Dictionary(code) data = obj.get_list() return vesta_jsonify(result=list(data))
request.form.get('origin_field'), request.form.get('linked_field')) info = obj.get_by_id(_id) except Exception, e: flash(e, 'error') else: flash(u'Справочник успешно обновлён', 'info') return redirect(url_for('.dict_edit', _id=_id)) data = dict() fields = dict() documents = list() collection = Dictionary(info['code']) linked_dict = obj.get_list({'version': {'$exists': True}}) if info['code'] in collections.get_list(): fields = _get_fields(info['code']) documents = collection.get_list(sort=[('id', 1)]) try: return render_template('{0}/dict_edit.html'.format(module.name), info=info, data=linked_dict, fields=fields, documents=documents) except TemplateNotFound: abort(404) @module.route('/dict_view/<_id>/') def dict_view(_id): obj = DictionaryNames() info = obj.get_by_id(_id) collections = Collections()