Esempio n. 1
0
    def testBasic(self):
        database = DataBase()

        doc1 = database.add_doc('jopa.odt', 'JOPA', 'odt', 0, True)
        doc1 = database.add_doc('jopa.odt', 'JOPA1', 'odt', 0, True)
        self.assertEqual(doc1.id, 1)
        self.assertEqual(doc1.title, 'JOPA1')

        doc2 = database.add_doc('jopa2.odt', 'JOPA2', 'odt', 0, False)
        self.assertEqual(doc2.id, 2)

        doc3 = database.add_doc('jopa3.odt', 'JOPA3', 'odt', 0, False)
        self.assertEqual(doc3.id, 3)

        tag1 = database.add_tag('FIO', 'Name and Surname')
        tag1 = database.add_tag('FIO', 'Name and Surname 2')
        self.assertEqual(tag1.id, 1)
        self.assertEqual(tag1.description, 'Name and Surname 2')

        tag2 = database.add_tag('date', u'Date')
        self.assertEqual(tag2.id, 2)

        tag3 = database.add_tag('tag3', u'Tag3')
        self.assertEqual(database.del_tag(tag3), True)

        link1 = database.add_link(doc1, tag1)
        link1 = database.add_link(doc1, tag1)
        self.assertEqual(link1.id, 1)

        link2 = database.add_link(doc1, tag2)
        link3 = database.add_link(doc2, tag2)
        link4 = database.add_link(doc3, tag1)
        database.del_link_id(link4.id)

        self.assertEqual((link2.id, link3.id), (2, 3))

        chain1 = database.add_chain(doc1, doc2)
        self.assertEqual(chain1.id, 1)
        chain1 = database.add_chain(doc1, doc2)
        self.assertEqual(chain1.id, 1)

        chain2 = database.add_chain(doc3, doc1)
        self.assertEqual(chain2.id, 2)

        self.assertEqual(database.check_add_chain(doc1.id, doc2.id), False)
        self.assertEqual(database.check_add_chain(doc2.id, doc1.id), True)

        num1 = database.add_number(doc1, self.user)
        self.assertEqual(num1.id, 1)

        num2 = database.add_number(doc1, self.user, num1)
        self.assertEqual(num2.id, 2)

        num3 = database.add_number(doc3, self.user)
        self.assertEqual(database.del_number(num3), True)

        num3 = database.add_number(doc3, self.user)
        database.change_number(num3, True)
        self.assertEqual(database.del_number(num3), False)

        data1 = database.add_data(num1, tag1.name, 'gggg')
        self.assertEqual(data1, True)

        data2 = database.add_data(num1, tag1.name, 'hhhh')
        self.assertEqual(data2, False)

        data2 = database.add_data(num2, tag1.name, 'hhhh')
        self.assertEqual(data2, True)

        database.add_data(num2, tag1.name, 'tag1')
        self.assertEqual(database.del_tag_from_datadoc(num2, tag1.name), True)

        database.numbers_from_doc(doc1)

        data1 = database.change_data(num1, tag1.name, 'lololo')
        self.assertEqual(data1.tag_value, 'lololo')

        r = database.change_number(num1, False)
        self.assertEqual(r, True)

        r = database.change_number(num1, True)
        self.assertEqual(r, True)

        docs = database.get_slave_docs(num1)
        self.assertEqual(docs[0].id, 2)

        slaves = database.get_all_need_slave(self.user)
        self.assertEqual(slaves[0][0].id, 2)

        numbers = database.numbers_from_doc(doc1)
        self.assertEqual((numbers[0].id, numbers[1].id), (1, 2))

        # Test doc permissions
        self.user1 = auth.models.User.objects.create_user('user1', '*****@*****.**', 'user1')
        self.group1 = auth.models.Group.objects.create()
        self.group1.name = 'group1'
        self.group1.save()

        self.assertEqual(database.check_user_perm(self.user1, doc1, False), False)

        database.add_user_perm(self.user1, doc1, False)

        self.assertEqual(database.check_user_perm(self.user1, doc1, False), True)
        self.assertEqual(database.check_user_perm(self.user1, doc1, True), False)

        database.add_user_perm(self.user1, doc2, True)
        self.assertEqual(database.check_user_perm(self.user1, doc2, True), True)
        self.user1.groups.add(self.group1)

        database.add_group_perm(self.group1, doc1, True)
        self.assertEqual(database.check_group_perm(self.group1, doc1, True), True)

        docs = database.doc.all()
        database.add_group_perm(self.group1, doc3, False)
        self.user.groups.add(self.group1)

        docs_f = database.perm_doc_filter(self.user1, docs, False)
        self.assertEqual(docs_f, [doc1, doc2, doc3])

        docs_f = database.perm_doc_filter(self.user1, docs, True)
        self.assertEqual(docs_f, [doc2, doc1])

        self.assertEqual(database.check_doc_id('1'), True)
        self.assertEqual(database.check_doc_id('100'), False)

        # False - in number
        self.assertEqual(database.del_tag(tag1), False)
Esempio n. 2
0
def new(request, id_doc = '0', id_num = '0'):
    """Create new document"""

    data = support.default_answer_data(request)
    if not data['auth']:
        return support.auth_error()

    database = DataBase()

    if id_doc == '0':
        if request.method == 'POST':
            id_doc = request.POST['id_doc']

            if not support.check_doc_perm(
                    request, 
                    database.doc.get(id = id_doc), 
                    True):
                return support.perm_error()

            num = '0'
            if database.doc.get(id = id_doc).main:
                return redirect('/documents/new/{0}/{1}/'.format(id_doc, num))
            else:
                raise Http404

        docs = database.doc.filter(main = True)

        docs = database.perm_doc_filter(request.user, docs, True)

        data['content'] = 'documents/choose.html'
        data['docs'] = docs
        return render_to_response('index.html', data)

    if not database.check_doc_id(id_doc):
        raise Http404

    doc = database.doc.get(id = id_doc)
    if not support.check_doc_perm(request, doc, True):
        return support.perm_error()

    if request.method == 'POST':
        if id_num == '0':
            if doc.main:
                num = database.add_number(doc, user = request.user)
                for tag_name in request.POST:
                    database.add_data(number = num,
                                      tag_name = tag_name,
                                      tag_value = request.POST[tag_name])

                return redirect('/documents/edit/{0}/'.format(num.id))
            else:
                raise Http404
        else:
            main_num = database.number.get(id = id_num)

            slaves = database.get_slave_docs(main_num)
            if doc in slaves:
                num = database.add_number(doc = doc,
                                          user = request.user,
                                          main_number = main_num)
                for tag_name in request.POST:
                    database.add_data(number = num, 
                                      tag_name = tag_name,
                                      tag_value = request.POST[tag_name])

                return redirect('/documents/edit/{0}/'.format(num.id))
            else:
                raise Http404


    data['Title_Doc'] = doc.title
    cur_links = database.link.filter(doc = doc)
    tags = []
    for link in cur_links:
        name = link.tag.name

        cycle = parse_docs.check_cycle(name)
        cycle_template = parse_docs.check_cycle_template(name)

        if cycle:
            name += '_0'

        if id_num != '0':
            num = database.number.get(id = id_num)
            try:
                value = database.data.get(number = num,
                                          tag_name = link.tag.name).tag_value
            except ObjectDoesNotExist:
                value = ''

        if cycle_template:
            value = '0'
        else:
            value = ''

        desc = link.tag.description

        if cycle:
            desc = database.tag.get(
                name = parse_docs.get_template_from_tagname(
                   name)).description +' | '+ desc

        tags.append({'name': name,
                     'desc': desc,
                     'value': value,
                     'cycle': cycle,
                     'cycle_template': cycle_template})

    data['tags'] = tags
    data['content'] = 'documents/new.html'

    return render_to_response('index.html', data)