def cap_rheader(r):

    rheader = None
    if r.representation == "html":
        record = r.record
        if record:
            T = current.T
            db = current.db
            s3db = current.s3db
            tablename = r.tablename
            if tablename == "cap_alert":
                alert_id = record.id
                itable = s3db.cap_info
                row = db(itable.alert_id == alert_id).select(itable.id,
                                                             limitby=(0, 1)).first()
                if record.is_template:
                    if not row:
                        error = DIV(T("An alert needs to contain at least one info item."),
                                    _class="error")
                    else:
                        error = ""

                    if r.component_name == "info" and r.component_id:
                        # Display "Copy" Button to copy record from the opened info
                        copy_btn = A(T("Copy Info Segment"),
                                     _href = URL(f = "template",
                                                 args = [r.id, "info", "create",],
                                                 vars = {"from_record": r.component_id,
                                                         },
                                                 ),
                                     _class = "action-btn"
                                     )
                    else:
                        copy_btn = None

                    tabs = [(T("Alert Details"), None),
                            (T("Information"), "info"),
                            #(T("Area"), "area"),
                            (T("Resource Files"), "resource"),
                            ]

                    rheader_tabs = s3_rheader_tabs(r, tabs)

                    rheader = DIV(TABLE(TR(TH("%s: " % T("Template")),
                                           TD(A(s3db.cap_template_represent(alert_id, record),
                                                _href=URL(c="cap", f="template",
                                                          args=[alert_id, "update"]))),
                                           ),
                                        ),
                                  rheader_tabs,
                                  error
                                  )
                    if copy_btn is not None:
                        rheader.insert(1, TR(TD(copy_btn)))
                else:
                    action_btn = None
                    msg_type_buttons = None
                    if not row:
                        error = DIV(T("You need to create at least one alert information item in order to be able to broadcast this alert!"),
                                    _class="error")
                        export_btn = ""
                    else:
                        error = ""
                        export_btn = A(DIV(_class="export_cap_large"),
                                       _href=URL(c="cap", f="alert", args=["%s.cap" % alert_id]),
                                       _target="_blank",
                                       )

                        has_permission = current.auth.s3_has_permission
                        # Display 'Submit for Approval', 'Publish Alert' or
                        # 'Review Alert' based on permission and deployment settings
                        if not current.request.get_vars.get("_next") and \
                           current.deployment_settings.get_cap_authorisation() and \
                           record.approved_by is None:
                            auth = current.auth
                            # Show these buttons only if there is atleast one area segment
                            area_table = s3db.cap_area
                            area_row = db(area_table.alert_id == alert_id).\
                                                select(area_table.id,
                                                       limitby=(0, 1)).first()
                            if area_row and has_permission("update", "cap_alert",
                                                           record_id=record.id):
                                action_btn = A(T("Submit for Approval"),
                                               _href = URL(f = "notify_approver",
                                                           vars = {"cap_alert.id": record.id,
                                                                   },
                                                           ),
                                               _class = "action-btn confirm-btn"
                                               )
                                current.response.s3.jquery_ready.append(
'''S3.confirmClick('.confirm-btn','%s')''' % T("Do you want to submit the alert for approval?"))

                                # For Alert Approver
                                if has_permission("approve", "cap_alert"):
                                    action_btn = A(T("Review Alert"),
                                                   _href = URL(args = [record.id,
                                                                       "review"
                                                                       ],
                                                               ),
                                                   _class = "action-btn",
                                                   )

                        if record.approved_by is not None:
                            if has_permission("create", "cap_alert"):
                                relay_alert = A(T("Relay Alert"),
                                                _data = "%s/%s" % (record.msg_type, r.id),
                                                _class = "action-btn cap-clone-update",
                                                )
                                if record.created_by:
                                    utable = db.auth_user
                                    row = db(utable.id == record.created_by).select(\
                                                            utable.organisation_id,
                                                            limitby=(0, 1)).first()
                                    row_ = db(utable.id == current.auth.user.id).select(\
                                                            utable.organisation_id,
                                                            limitby=(0, 1)).first()
                                    if row.organisation_id == row_.organisation_id:
                                        msg_type_buttons = TAG[""](
                                                TR(TD(A(T("Update Alert"),
                                                        _data = "Update/%s" % r.id,
                                                        _class = "action-btn cap-clone-update",
                                                        ))),
                                                TR(TD(A(T("Cancel Alert"),
                                                        _data = "Cancel/%s" % r.id,
                                                        _class = "action-btn cap-clone-update",
                                                        ))),
                                                TR(TD(A(T("Error Alert"),
                                                        _data = "Error/%s" % r.id,
                                                        _class = "action-btn cap-clone-update",
                                                        ))),
                                                TR(TD(A(T("All Clear"),
                                                        _data = "AllClear/%s" % r.id,
                                                        _class = "action-btn cap-clone-update",
                                                        ))),
                                                )
                                    else:
                                        msg_type_buttons = relay_alert
                                else:
                                    msg_type_buttons = relay_alert

                    tabs = [(T("Alert Details"), None),
                            (T("Information"), "info"),
                            (T("Area"), "area"),
                            (T("Resource Files"), "resource"),
                            ]

                    if r.representation == "html" and \
                       current.auth.s3_has_permission("update", "cap_alert",
                                                      record_id=alert_id) and \
                       r.record.approved_by is None:
                        # Show predefined areas tab if we have some defined for this event_type
                        row_ = db(itable.alert_id == alert_id).select(itable.event_type_id,
                                                                      limitby=(0, 1)).first()
                        if row_ is not None:
                            artable = s3db.cap_area
                            query = (artable.deleted != True) & \
                                    (artable.is_template == True) & \
                                    (artable.event_type_id == row_.event_type_id)
                            template_area_row = db(query).select(artable.id,
                                                                 limitby=(0, 1)).first()
                            if template_area_row:
                                tabs.insert(2, (T("Predefined Areas"), "assign"))

                    rheader_tabs = s3_rheader_tabs(r, tabs)

                    rheader = DIV(TABLE(TR(TH("%s: " % T("Alert")),
                                           TD(A(s3db.cap_alert_represent(alert_id, record),
                                                _href=URL(c="cap", f="alert",
                                                          args=[alert_id, "update"]))),
                                           ),
                                        TR(export_btn)
                                        ),
                                  rheader_tabs,
                                  error
                                  )

                    if action_btn:
                        rheader.insert(1, TR(TD(action_btn)))

                    if msg_type_buttons is not None:
                        rheader.insert(1, msg_type_buttons)

            elif tablename == "cap_area":
                # Used only for Area Templates
                tabs = [(T("Area"), None),
                        ]

                if current.deployment_settings.get_L10n_translate_cap_area():
                    tabs.insert(1, (T("Local Names"), "name"))

                rheader_tabs = s3_rheader_tabs(r, tabs)
                rheader = DIV(TABLE(TR(TH("%s: " % T("Area")),
                                       TD(A(s3db.cap_area_represent(record.id, record),
                                            _href=URL(c="cap", f="area",
                                                      args=[record.id, "update"]))),
                                       ),
                                    ),
                              rheader_tabs
                              )

            elif tablename == "cap_info":
                # Shouldn't ever be called
                tabs = [(T("Information"), None),
                        (T("Resource Files"), "resource"),
                        ]

                if cap_alert_is_template(record.alert_id):
                    rheader_tabs = s3_rheader_tabs(r, tabs)
                    table = r.table
                    rheader = DIV(TABLE(TR(TH("%s: " % T("Template")),
                                           TD(A(s3db.cap_template_represent(record.alert_id),
                                                _href=URL(c="cap", f="template",
                                                          args=[record.alert_id, "update"]))),
                                            ),
                                        TR(TH("%s: " % T("Info template")),
                                           TD(A(s3db.cap_info_represent(record.id, record),
                                                _href=URL(c="cap", f="info",
                                                          args=[record.id, "update"]))),
                                           )
                                        ),
                                  rheader_tabs,
                                  _class="cap_info_template_form"
                                  )
                    current.response.s3.js_global.append('''i18n.cap_locked="%s"''' % T("Locked"))
                else:
                    tabs.insert(1, (T("Areas"), "area"))
                    rheader_tabs = s3_rheader_tabs(r, tabs)
                    table = r.table

                    rheader = DIV(TABLE(TR(TH("%s: " % T("Alert")),
                                           TD(A(s3db.cap_alert_represent(record.alert_id),
                                                _href=URL(c="cap", f="alert",
                                                          args=[record.alert_id, "update"]))),
                                           ),
                                        TR(TH("%s: " % T("Information")),
                                           TD(A(s3db.cap_info_represent(record.id, record),
                                                _href=URL(c="cap", f="info",
                                                          args=[record.id, "update"]))),
                                           )
                                        ),
                                  rheader_tabs
                                  )

    return rheader

# =============================================================================
Urns marked for deletion.
dataset/ETHPy150Open sahana/eden/modules/s3db/cap.py/cap_rheader--crossover with--dataset/ETHPy150Open google/grr/grr/lib/aff4.py/DeletionPool.urns_for_deletion