Beispiel #1
0
def links(request):
    """View all links"""

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

    database = DataBase()

    if request.method == 'POST':
        if request.POST['do'] == 'delete':
            id_link = request.POST['id_link']
            if request.user.has_perm('main.delete_link'):
                database.del_link_id(id_link)

        elif request.POST['do'] == 'add':
            id_doc = request.POST['id_doc']
            id_tag = request.POST['id_tag']
            if request.user.has_perm('main.add_link'):
                database.add_link(database.doc.get(id = id_doc), 
                                  database.tag.get(id = id_tag))

    all_docs = database.doc.all()
    all_db_tags = database.tag.all()
    all_tags = []

    for db_tag in all_db_tags:
        if not parse_docs.check_cycle(db_tag.name):
            all_tags.append(db_tag)

    out = []

    for doc in all_docs:
        lns = []
        doc_links = database.link.filter(doc = doc)
        use_tags = []
        free_tags = []
        for link in doc_links:
            tag = link.tag
            if not parse_docs.check_cycle(tag.name):
                use_tags.append(tag)
                lns.append(link)

        for tag in all_tags:
            if tag not in use_tags:
                free_tags.append(tag)

        out.append( {'doc': doc, 'links': lns, 'free_tags': free_tags} )

    data['out'] = out
    data['content'] = 'tags/links.html'

    return render_to_response('index.html', data)
Beispiel #2
0
    def testBasic(self):
        self.assertEqual(parse_docs.check_cycle_template('cycle_line'), True)
        self.assertEqual(parse_docs.check_cycle_template('cycle_Line'), True)
        self.assertEqual(parse_docs.check_cycle_template('Cycle_Line'), False)
        self.assertEqual(parse_docs.check_cycle_template('Cycle_Line'), False)
        self.assertEqual(parse_docs.check_cycle_template('_tagname_'), False)

        self.assertEqual(parse_docs.check_cycle('cycle_line_TagName_'), True)
        self.assertEqual(parse_docs.check_cycle('_TagName_'), False)
        self.assertEqual(parse_docs.check_cycle('cycle_line_'), False)
        self.assertEqual(parse_docs.check_cycle('cycle_line'), False)

        self.assertEqual(parse_docs.get_template_from_tagname('cycle_line_tagname'), 'cycle_line')
        self.assertEqual(parse_docs.get_tagname_from_tag_with_num('cycle_line_tagname_13'), 'cycle_line_tagname')
        self.assertEqual(parse_docs.get_num_from_tag_with_num('cycle_line_tagname_13'), '13')
Beispiel #3
0
    def get_all_datatags(self, number):
        """Get all data Tags from document_number

        Input:
            number - main.Number
        Output:
            list of dict
                tag_name - text
                tag_value - text
                tag_description - (Template.desc +num+ |+ desc if cycle)
                cycle - True/False
                cycle_template - True/False
                tag_id
        """
        data_tags = self.data.filter(number = number)
        out = []
        for data_tag in data_tags:
            tag_name = data_tag.tag_name
            tag_value = data_tag.tag_value
            cycle = parse_docs.check_cycle(tag_name)
            cycle_template = parse_docs.check_cycle_template(tag_name)
            if cycle:
                name = parse_docs.get_tagname_from_tag_with_num(tag_name)
                tag = self.tag.get(name = name)
                template = parse_docs.get_template_from_tagname(name)
                tag_template = self.tag.get(name = template)
                num = parse_docs.get_num_from_tag_with_num(tag_name)
                tag_description = tag_template.description +' '+str(
                                  int(num)+1)+ ' |' + tag.description
                tag_id = tag.id
            else:
                tag = self.tag.get(name = tag_name)
                if cycle_template:
                    tag_value = str(int(tag_value)+1)

                tag_description = tag.description
                tag_id = tag.id

            out.append({
                'tag_name': tag_name,
                'tag_value': tag_value,
                'tag_description': tag_description,
                'cycle': cycle,
                'cycle_template': cycle_template,
                'tag_id': tag_id
                })

        return out
Beispiel #4
0
    def modify_cycle_tags(self, number, tag_name, count):
        """Decrement num tags, if >= count

        Input:
            number - Number
            tag_name - cycle_template
            count - count of rows in table
        """
        for data in self.data.filter(number = number):
            if parse_docs.check_cycle(data.tag_name):
                if parse_docs.get_template_from_tagname(
                        data.tag_name) == tag_name:
                    num = parse_docs.get_num_from_tag_with_num(
                                        data.tag_name)
                    if int(num) >= count:
                        num = str(int(num)-1)
                        data.tag_name = parse_docs.get_tagname_from_tag_with_num(
                            data.tag_name) + '_' + num
                        data.save()
Beispiel #5
0
    def del_tag_from_datadoc(self, number, tag_name, row = '-1'):
        """Del one tag, from data of number, return True if ok"""

        try:
            # Test and change number (for date - change)
            if self.change_number(number = number):
                # if can change - delete
                if parse_docs.check_cycle_template(tag_name):
                    for data in self.data.filter(number = number):
                        if parse_docs.check_cycle(data.tag_name):
                            if parse_docs.get_template_from_tagname(
                                    data.tag_name) == tag_name:
                                if row != '-1' and parse_docs.get_num_from_tag_with_num(
                                        data.tag_name) == row:
                                    data.delete()
                                elif row == '-1':
                                    data.delete()

                    table = self.data.get(number = number,
                                          tag_name = tag_name)

                    if row == '-1':
                        table.tag_value = 0

                    table.tag_value = str(int(table.tag_value)-1)
                    if table.tag_value == '-1':
                        table.delete()
                    else:
                        table.save()
                        self.modify_cycle_tags(number,
                                               tag_name,
                                               int(row))

                else:
                    self.data.get(number = number, 
                                  tag_name = tag_name).delete()

                return True
            else:
                return False
        except ObjectDoesNotExist:
            return False
Beispiel #6
0
def show(request):
    """View all tags"""

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

    database = DataBase()

    if request.method == 'POST':
        if request.POST['do'] == 'delete':
            id_tag = request.POST['id_tag']
            if request.user.has_perm('main.delete_tag'):
                database.del_tag(database.tag.get(id = id_tag))

        if request.POST['do'] == 'add':
            name = request.POST['name']
            desc = request.POST['description']
            if request.user.has_perm('main.add_tag'):
                database.add_tag(name, desc)

    all_db_tags = database.tag.all()
    all_tags = []

    for db_tag in all_db_tags:
        if not parse_docs.check_cycle(db_tag.name):
            all_tags.append(db_tag)

    out = []

    for tag in all_tags:
        numbers = database.numbers_from_tag(tag)
        out.append( {'tag': tag, 'numbers': numbers} )

    data['out'] = out
    data['content'] = 'tags/show.html'

    return render_to_response('index.html', data)
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)
def edit(request, num = '0'):
    """Edit data of number-document"""

    data = support.default_answer_data(request)
    if not data['auth']:
        return support.auth_error()
    if num == '0':
        return redirect('/documents/edit/')

    database = DataBase()

    num = database.number.get(id = num)

    if not support.check_doc_perm(request, num.doc, True):
        return support.perm_error()

    if request.method == 'POST':
        if request.POST['do'] == 'change':
            for tag in request.POST:
                if tag == 'do':
                    continue

                if request.user.has_perm('main.change_data'):
                    tag_value = request.POST[tag]
                    if parse_docs.check_cycle_template(tag):
                        tag_value = str(int(tag_value))

                    database.change_data(number = num,
                                         tag_name = tag,
                                         tag_value = tag_value)

            return redirect('/documents/edit/{0}/'.format(num.id))

        if request.POST['do'] == 'add_tag':
            if request.user.has_perm(
                    'main.add_data') and request.user.has_perm(
                    'main.change_number'):
                cur_tag = database.tag.get(id = request.POST['id_tag'])
                if parse_docs.check_cycle_template(cur_tag.name):
                    cur_tag_value = request.POST['tag_value']
                    try:
                        count_add = int(cur_tag_value)
                    except ValueError:
                        count_add = 0

                    if count_add != 0:
                        tags = database.tag.all()
                        add_tags = []
                        for tag in tags:
                            if parse_docs.check_cycle(tag.name):
                                if parse_docs.get_template_from_tagname(
                                        tag.name) == cur_tag.name:
                                    add_tags.append(tag)

                    for i in xrange(count_add):

                        data_tags = database.get_all_datatags(num)
                        table_num = '0'
                        for data_tag in data_tags:
                            if parse_docs.check_cycle_template(data_tag['tag_name']):
                                if cur_tag.name == data_tag['tag_name']:
                                    table_num = str(int(data_tag['tag_value']))
                                    break

                        for add_tag in add_tags:
                            if database.add_data(
                                    number = num,
                                    tag_name = add_tag.name + '_' + table_num,
                                    tag_value = ''
                                    ):
                                database.change_number(number = num)

                        if table_num == '0':
                            if database.add_data(
                                    number = num,
                                    tag_name = cur_tag.name,
                                    tag_value = '0'):
                                database.change_number(number = num)
                        else:
                            database.change_data(number = num,
                                            tag_name = cur_tag.name,
                                            tag_value = table_num)
                else:
                    if database.add_data(
                           number = num,
                           tag_name = cur_tag.name,
                           tag_value = request.POST['tag_value']):
                        database.change_number(number = num)

    all_data = database.get_all_datatags(num)
    all_database_tags = database.tag.all()
    doc = num.doc

    data['Title_Doc'] = doc.title
    data['template'] = doc.print_form
    data['Number'] = num.id
    data['Date'] = num.date_change
    data['Author'] = '{0} [{1}]'.format(num.user.username,
                                        num.user.get_full_name())
    data['held_status'] = num.held_status
    if data['held_status']:
        data['date_held'] = num.date_held

    showthis = []
    test_tags = []
    for data_tag in all_data:
        showthis.append(data_tag)
        test_tags.append(database.tag.get(id = data_tag['tag_id']))

    all_tags = []
    for tag in all_database_tags:
        if tag not in test_tags:
            if not parse_docs.check_cycle(tag.name):
                all_tags.append(tag)
        else:
            if parse_docs.check_cycle_template(tag.name):
                all_tags.append(tag)

    data['all_tags'] = all_tags
    data['showthis'] = showthis

    data['content'] = 'documents/edit.html'

    return render_to_response('index.html', data)