예제 #1
0
    def serveRecordPageDet(table, eid):
        checkBounds(table=table, eid=eid)

        path = f"""/{table}/{N.item}/{eid}"""
        if table in ALL_TABLES:
            context = getContext()
            auth.authenticate()
            topbar = Topbar(context).wrap()
            sidebar = Sidebar(context, path).wrap()
            if tablePerm(table):
                recordObj = mkTable(context, table).record(eid=eid,
                                                           withDetails=True,
                                                           **method())
                if recordObj.mayRead is not False:
                    record = recordObj.wrap()
                    return render_template(
                        INDEX,
                        topbar=topbar,
                        sidebar=sidebar,
                        material=record,
                    )
                flash(f"Unknown record in table {table}", "error")
                return redirectResult(f"""/{table}/{N.list}""", False)
        flash(f"Unknown table {table}", "error")
        return redirectResult(START, False)
예제 #2
0
 def serveTable(table, eid):
     checkBounds()
     action = G(request.args, N.action)
     actionRep = f"?action={action}" if action else E
     eidRep = f"""/{eid}""" if eid else E
     path = f"""/{table}/{N.list}{eidRep}{actionRep}"""
     if not action or action in LIST_ACTIONS:
         if table in ALL_TABLES:
             context = getContext()
             auth.authenticate()
             topbar = Topbar(context).wrap()
             sidebar = Sidebar(context, path).wrap()
             tableList = None
             if tablePerm(table, action=action):
                 tableList = mkTable(context, table).wrap(eid,
                                                          action=action)
             if tableList is None:
                 flash(f"{action or E} view on {table} not allowed",
                       "error")
                 return redirectResult(START, False)
             return render_template(
                 INDEX,
                 topbar=topbar,
                 sidebar=sidebar,
                 material=tableList,
             )
         flash(f"Unknown table {table}", "error")
     if action:
         flash(f"Unknown view {action}", "error")
     else:
         flash("Missing view", "error")
     return redirectResult(START, False)
예제 #3
0
def execute(context, task, eid):
    """Executes a workflow task.

    First a table object is constructed, based on the `table` property
    of the task, using `context`.

    Then a record object is constructed in that table, based on the `eid`
    parameter.

    If that all succeeds, all information is at hand to verify permissions
    and perform the task.

    Parameters
    ----------
    context: object
        A `control.context.Context` singleton
    task: string
        The name of the task
    eid: string(objectId)
        The id of the relevant record
    """

    taskInfo = G(TASKS, task)
    acro = G(taskInfo, N.acro)
    table = G(taskInfo, N.table)
    if table not in ALL_TABLES:
        flash(f"""Workflow {acro} operates on wrong table: "{table or E}""",
              "error")
        return (False, None)
    return mkTable(context, table).record(eid=eid).task(task)
예제 #4
0
    def serveField(table, eid, field):
        checkBounds(table=table, eid=eid, field=field)

        action = G(request.args, N.action)
        if action in FIELD_ACTIONS:
            context = getContext()
            auth.authenticate()
            if table in ALL_TABLES and tablePerm(table):
                recordObj = mkTable(context, table).record(eid=eid)
                if recordObj.mayRead is not False:
                    fieldObj = mkTable(context,
                                       table).record(eid=eid).field(field)
                    if fieldObj:
                        return fieldObj.wrap(action=action)
                    return noField(table, field)
                return noRecord(table)
            return noTable(table)
        return noAction(action)
예제 #5
0
    def tableEntry(
        self,
        table,
        prefix=None,
        item=None,
        postfix=None,
        action=None,
        withOptions=False,
        asTask=False,
    ):
        """Produce a table entry.

        A table entry is a link or button to show a table.

        Parameters
        ----------
        table: string
        prefix, item, postfix: string, optional `None`
            These make up the text of the link in that order.
            If `item` is left out, the tables.yaml file has a suitable
            string under the key `items`
        action: string {`my`, `our`, ...}, optional, `None`
            If left out, all items will be retrieved. Otherwise, a selection is made.
            See web.yaml under `listActions` for all possible values.
            See also `control.table.Table.wrap`.

        !!! caution
            The table entry will only be made if the user has permissions
            to list the detail table!

        Returns
        -------
        path: url
            The url that corresponds to this entry
        string(html)
            The wrapped entry
        """

        if not self.tablePerm(table):
            return (E, E)

        context = self.context

        tableObj = mkTable(context, table)
        item = tableObj.itemLabels[1] if item is None else item
        actionRep = f"?action={action}" if action else E
        prefixRep = f"{prefix} " if prefix else E
        postfixRep = f" {postfix}" if postfix else E

        return self.makeEntry(
            f"""{prefixRep}{item}{postfixRep}""",
            f"""/{table}/{N.list}{actionRep}""",
            withOptions=True,
            asTask=asTask,
        )
예제 #6
0
    def doTask(self, task, recordObj):
        """Execute a workflow task on a record.

        The permission to execute the task will be checked first.

        !!! hint
            Workflow tasks are described above.

        Parameters
        ----------
        recordObj: object
            The record must be passed as a record object.

        Returns
        -------
        url | `None`
            To navigate to after the action has been performed.
            If the action has not been performed, `None` is returned.
        """

        context = recordObj.context
        table = recordObj.table
        eid = recordObj.eid
        kind = recordObj.kind
        (contribId, ) = self.info(N.contrib, N._id)

        taskInfo = G(TASKS, task)
        acro = G(taskInfo, N.acro)

        urlExtra = E

        executed = False
        if self.permission(task, kind=kind):
            operator = G(taskInfo, N.operator)
            if operator == N.add:
                dtable = G(taskInfo, N.detail)
                tableObj = mkTable(context, dtable)
                deid = tableObj.insert(
                    masterTable=table, masterId=eid, force=True) or E
                if deid:
                    urlExtra = f"""/{N.open}/{dtable}/{deid}"""
                    executed = True
            elif operator == N.set:
                field = G(taskInfo, N.field)
                value = G(taskInfo, N.value)
                if recordObj.field(field, mayEdit=True).save(value):
                    executed = True
            if executed:
                flash(f"""<{acro}> executed""", "message")
            else:
                flash(f"""<{acro}> failed""", "error")
        else:
            flash(f"""<{acro}> not permitted""", "error")

        return f"""/{N.contrib}/{N.item}/{contribId}{urlExtra}""" if executed else None
예제 #7
0
    def serveRecordTitle(table, eid):
        checkBounds(table=table, eid=eid)

        if table in ALL_TABLES:
            context = getContext()
            auth.authenticate()
            if tablePerm(table):
                recordObj = mkTable(context, table).record(eid=eid,
                                                           withDetails=False,
                                                           **method())
                if recordObj.mayRead is not False:
                    return recordObj.wrap(expanded=-1)
        return noRecord(table)
예제 #8
0
    def serveTableInsert(table):
        checkBounds(table=table)

        newPath = f"""/{table}/{N.list}"""
        if table in ALL_TABLES and table not in MASTERS:
            context = getContext()
            auth.authenticate()
            eid = None
            if tablePerm(table):
                eid = mkTable(context, table).insert()
            if eid:
                newPath = f"""/{table}/{N.item}/{eid}"""
                flash("item added")
        else:
            flash(f"Cannot add items to {table}", "error")
        return redirectResult(newPath, eid is not None)
예제 #9
0
    def serveRecordDelete(table, eid):
        checkBounds(table=table, eid=eid)

        if table in ALL_TABLES:
            context = getContext()
            auth.authenticate()
            good = False
            if tablePerm(table):
                good = mkTable(context, table).record(eid=eid).delete()
                newUrlPart = f"?{N.action}={N.my}" if table in USER_TABLES else E
                newPath = f"""/{table}/{N.list}{newUrlPart}"""
            if good:
                flash("item deleted")
            else:
                flash("item not deleted", "error")
            return redirectResult(newPath, good)
        flash(f"Unknown table {table}", "error")
        return redirectResult(START, False)
예제 #10
0
    def serveTableInsertDetail(table, eid, dtable):
        checkBounds(table=table, eid=eid, dtable=dtable)

        newPath = f"""/{table}/{N.item}/{eid}"""
        dEid = None
        if (table in USER_TABLES_LIST[0:2] and table in DETAILS
                and dtable in DETAILS[table]):
            context = getContext()
            auth.authenticate()
            if tablePerm(table):
                dEid = mkTable(context, dtable).insert(masterTable=table,
                                                       masterId=eid)
            if dEid:
                newPath = f"""/{table}/{N.item}/{eid}/{N.open}/{dtable}/{dEid}"""
        if dEid:
            flash(f"{dtable} item added")
        else:
            flash(f"Cannot add a {dtable} here", "error")
        return redirectResult(newPath, dEid is not None)
예제 #11
0
    def serveRecordDeleteDetail(table, masterId, dtable, eid):
        checkBounds(table=table, masterId=masterId, dtable=dtable, eid=eid)

        newPath = f"""/{table}/{N.item}/{masterId}"""
        good = False
        if (table in USER_TABLES_LIST[0:2] and table in DETAILS
                and dtable in DETAILS[table]):
            context = getContext()
            auth.authenticate()
            if tablePerm(table):
                recordObj = mkTable(context, dtable).record(eid=eid)

                wfitem = recordObj.wfitem
                if wfitem:
                    good = recordObj.delete()

        if good:
            flash(f"{dtable} detail deleted")
        else:
            flash(f"{dtable} detail not deleted", "error")
        return redirectResult(newPath, good)