示例#1
0
 def _do_view(self, env, req, form):
     data = {'_dgettext': dgettext}
     form_id = form.resource.id
     data['page_title'] = get_resource_description(env, form.resource,
                                                   href=req.href)
     data['title'] = get_resource_shortname(env, form.resource)
     # prime list with current state
     subcontext, author, time = self.get_tracform_meta(form_id)[3:6]
     author = format_author(self.env, req, author, 'change')
     if not subcontext == '':
         data['subcontext'] = subcontext
     state = self.get_tracform_state(form_id)
     data['fields'] = self._render_fields(req, form_id, state)
     history = [{'author': author, 'time': time,
                 'old_state': state}]
     # add recorded old_state
     records = self.get_tracform_history(form_id)
     for author, time, old_state in records:
         author = format_author(self.env, req, author, 'change')
         history.append({'author': author, 'time': time,
                         'old_state': old_state})
     data['history'] = parse_history(history)
     # show reset button in case of existing data and proper permission
     data['allow_reset'] = req.perm(form.resource) \
                           .has_permission('FORM_RESET') and form.has_data
     add_stylesheet(req, 'tracforms/tracforms.css')
     return 'form.html', data, None
示例#2
0
    def reset_tracform(self, src, field=None, author=None, step=0, db=None):
        """Delete method for all TracForms db tables.

        Note, that we only delete recorded values and history here, while
        the form definition (being part of forms parent resource) is retained.
        Reset of single fields is not implemented, because this would require
        cumbersome and error prown history rewriting - not worth the hassle.
        """
        db = self._get_db(db)
        cursor = db.cursor()
        form_ids = []
        # identify form_id(s) to reset
        if is_number(src):
            form_ids.append(src)
        elif isinstance(src, tuple) and len(src) == 3:
            if src[-1] is None:
                # no subcontext given, reset all forms of the parent resource
                for form_id in self.get_tracform_ids(src[0], src[1], db=db):
                    form_ids.append(form_id)
            else:
                form_ids.append(self.get_tracform_meta(src, db=db)[0])
        db = self._get_db(db)
        cursor = db.cursor()
        # restore of old values for multiple forms is not meaningful
        if step == -1 and len(form_ids) == 1:
            form_id = form_ids[0]
            now = int(time.time())
            author, updated_on, old_state = self.get_tracform_history(
                                            form_id, db=db)[0] or \
                                            (author, now, '{}')
            if updated_on == now:
                # no history recorded, so only form values can be reset
                step = 0
            else:
                # copy last old_state to current
                cursor.execute("""
                    UPDATE forms
                        SET author=%s,
                            time=%s,
                            state=%s
                    WHERE   id=%s
                    """, (author, updated_on, old_state, form_id))
                history = []
                records = self.get_tracform_history(form_id, db=db)
                for history_author, history_time, old_state in records:
                    history.append({'author': history_author,
                                    'time': history_time,
                                    'old_state': old_state})
                history = parse_history(history, fieldwise=True)
                # delete restored history entry
                cursor.execute("""
                    DELETE
                    FROM    forms_history
                    WHERE   id=%s
                        AND time=%s
                    """, (form_id, updated_on))
                # rollback field info changes
                for field in history.keys():
                    changes = history[field]
                    if len(changes) > 0:
                        # restore last field info, unconditional by intention
                        # i.e. to not create entries, if track_fields is False
                        cursor.execute("""
                            UPDATE  forms_fields
                                SET author=%s,
                                    time=%s
                            WHERE   id=%s
                                AND field=%s
                            """, (changes[0]['author'], changes[0]['time'],
                                  form_id, field))
                    else:
                        # delete current field info
                        cursor.execute("""
                            DELETE
                            FROM    forms_fields
                            WHERE   id=%s
                               AND  field=%s
                            """, (form_id, field))
        if step == 0:
            # reset all fields and delete full history
            for form_id in form_ids:
                cursor.execute("""
                    DELETE
                    FROM    forms_history
                    WHERE   id=%s
                    """, (form_id,))
                cursor.execute("""
                    DELETE
                    FROM    forms_fields
                    WHERE   id=%s
                    """, (form_id,))
                # don't delete basic form reference but save the reset
                # as a form change to prevent creation of a new form_id
                # for further retention data
                cursor.execute("""
                    UPDATE  forms
                        SET author=%s,
                            time=%s,
                            state=%s
                    WHERE   id=%s
                    """, (author, int(time.time()), '{}', form_id))
        db.commit()
示例#3
0
    def reset_tracform(self, src, field=None, author=None, step=0, db=None):
        """Delete method for all TracForms db tables.

        Note, that we only delete recorded values and history here, while
        the form definition (being part of forms parent resource) is retained.
        Reset of single fields is not implemented, because this would require
        cumbersome and error prown history rewriting - not worth the hassle.
        """
        db = self._get_db(db)
        cursor = db.cursor()
        form_ids = []
        # identify form_id(s) to reset
        if is_number(src):
            form_ids.append(src)
        elif isinstance(src, tuple) and len(src) == 3:
            if src[-1] is None:
                # no subcontext given, reset all forms of the parent resource
                for form_id in self.get_tracform_ids(src[0], src[1], db=db):
                    form_ids.append(form_id)
            else:
                form_ids.append(self.get_tracform_meta(src, db=db)[0])
        db = self._get_db(db)
        cursor = db.cursor()
        # restore of old values for multiple forms is not meaningful
        if step == -1 and len(form_ids) == 1:
            form_id = form_ids[0]
            now = int(time.time())
            author, updated_on, old_state = self.get_tracform_history(
                                            form_id, db=db)[0] or \
                                            (author, now, '{}')
            if updated_on == now:
                # no history recorded, so only form values can be reset
                step = 0
            else:
                # copy last old_state to current
                cursor.execute(
                    """
                    UPDATE forms
                        SET author=%s,
                            time=%s,
                            state=%s
                    WHERE   id=%s
                    """, (author, updated_on, old_state, form_id))
                history = []
                records = self.get_tracform_history(form_id, db=db)
                for history_author, history_time, old_state in records:
                    history.append({
                        'author': history_author,
                        'time': history_time,
                        'old_state': old_state
                    })
                history = parse_history(history, fieldwise=True)
                # delete restored history entry
                cursor.execute(
                    """
                    DELETE
                    FROM    forms_history
                    WHERE   id=%s
                        AND time=%s
                    """, (form_id, updated_on))
                # rollback field info changes
                for field in history.keys():
                    changes = history[field]
                    if len(changes) > 0:
                        # restore last field info, unconditional by intention
                        # i.e. to not create entries, if track_fields is False
                        cursor.execute(
                            """
                            UPDATE  forms_fields
                                SET author=%s,
                                    time=%s
                            WHERE   id=%s
                                AND field=%s
                            """, (changes[0]['author'], changes[0]['time'],
                                  form_id, field))
                    else:
                        # delete current field info
                        cursor.execute(
                            """
                            DELETE
                            FROM    forms_fields
                            WHERE   id=%s
                               AND  field=%s
                            """, (form_id, field))
        if step == 0:
            # reset all fields and delete full history
            for form_id in form_ids:
                cursor.execute(
                    """
                    DELETE
                    FROM    forms_history
                    WHERE   id=%s
                    """, (form_id, ))
                cursor.execute(
                    """
                    DELETE
                    FROM    forms_fields
                    WHERE   id=%s
                    """, (form_id, ))
                # don't delete basic form reference but save the reset
                # as a form change to prevent creation of a new form_id
                # for further retention data
                cursor.execute(
                    """
                    UPDATE  forms
                        SET author=%s,
                            time=%s,
                            state=%s
                    WHERE   id=%s
                    """, (author, int(time.time()), '{}', form_id))
        db.commit()