def index(): obj = DictionaryNames() data = obj.get_list({'code': {'$ne': 'dict_names'}}, sort=[('oid', 1), ('code', 1)]) try: return render_template('{0}/index.html'.format(module.name), data=data) except TemplateNotFound: abort(404)
class DictionaryNamesTestCase(unittest.TestCase): def setUp(self): app.config['TESTING'] = True app.config['MONGODB_DB'] = TEST_DB self.app = app.test_client() self.obj = DictionaryNames() def tearDown(self): db.drop_collection(self.obj.code) def test_get_list(self): with self.assertRaises(ValueError): result = self.obj.get_list() #self.assertEqual(result.count(), 0) #self.assertEqual(list(result), list()) def test_add(self): data = dict(code='test', name=u'Тестовый справочник') _id = self.obj.add(data) self.assertIsNotNone(_id) result = self.obj.get_by_id(_id) data.update(dict(_id=_id)) self.assertDictEqual(data, result) def test_update(self): data = dict(code='test2', name=u'Тестовый справочник2') _id = self.obj.add(data) self.assertIsNotNone(_id) update_data = dict(code='test2', name=u'Тестовый справочник2 (updated)', updated=datetime.now()) self.obj.update(_id, update_data) result = self.obj.get_by_id(_id) update_data.update(dict(_id=_id)) result.pop('updated') update_data.pop('updated') self.assertDictEqual(result, update_data) def test_delete(self): code = 'test3' data = dict(code=code, name=u'Тестовый справочник3') _id = self.obj.add(data) self.assertIsNotNone(_id) self.obj.delete(code) result = self.obj.get_by_code(code) self.assertIsNone(result) def test_update_by_code(self): code = 'test4' data = dict(code=code, name=u'Тестовый справочник4') _id = self.obj.add(data) self.assertIsNotNone(_id) update_data = dict(code=code, name=u'Тестовый справочник4 (updated)', updated=datetime.now()) self.obj.update_by_code(code, update_data) result = self.obj.get_by_code(code) update_data.update(dict(_id=_id)) result.pop('updated') update_data.pop('updated') self.assertDictEqual(result, update_data)
def get(self, code): """Получение списка справочников или информации по конкретному справочнику""" collection = DictionaryNames() if code is None: # return list of Dictionaries try: result = collection.get_list() except TypeError, e: raise InvalidAPIUsage(e.message, status_code=400) except ValueError, e: return vesta_jsonify(dict())
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)