Beispiel #1
0
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))
Beispiel #2
0
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)