def commit():
    """ REST Controller """

    # Check if user is affiliated to an Organisation
    if not is_affiliated():
        tablename = "req_commit_person"
        table = s3db[tablename]
        # Unaffiliated people can't commit on behalf of others
        table.person_id.writable = False
        # & can only make single-person commitments
        # (This should have happened in the main commitment)
        s3db.configure(tablename,
                       insertable = False,
                       )

    if request.args in "assign":
        def skill_default_filter(selector, tablename=None):
            # Lookup Skills in the Request
            commit_id = request.args[0]
            ctable = s3db.req_commit
            rstable = s3db.req_req_skill
            query = (ctable.id == commit_id) & \
                    (ctable.req_id == rstable.req_id)
            multi_skills = db(query).select(rstable.skill_id,
                                            )
            skills = []
            for row in multi_skills:
                m = row.skill_id
                for s in m:
                    skills.append(s)
            return skills
        s3base.s3_set_default_filter("competency.skill_id",
                                     skill_default_filter,
                                     tablename = "hrm_human_resource")

    def prep(r):
        if r.interactive and r.record:
            # Commitments created through UI should be done via components
            if r.component:
                if r.component_name == "commit_item":
                    # Dropdown not Autocomplete
                    s3db.req_commit_item.req_item_id.widget = None

                    # Limit commit items to items from the request
                    s3db.req_commit_item.req_item_id.requires = \
                        IS_ONE_OF(db, "req_req_item.id",
                                  s3db.req_item_represent,
                                  filterby = "req_id",
                                  filter_opts = [r.record.req_id],
                                  orderby = "req_req_item.id",
                                  sort = True,
                                  )

                elif r.component_name == "commit_skill":
                    # Limit commit skills to skills from the request
                    skills_filter(r.record.req_id)

            else:
                # No Component
                table = r.table
                s3.crud.submit_button = T("Save Changes")
                if r.record.type == 1: # Items
                    # Limit site_id to facilities the user has permissions for
                    auth.permitted_facilities(table=table,
                                              error_msg=T("You do not have permission for any facility to make a commitment.") )

                    table.site_id.comment = A(T("Set as default Site"),
                                              _id="req_commit_site_id_link",
                                              _target="_blank",
                                              _href=URL(c="default",
                                                        f="user",
                                                        args=["profile"]))

                    jappend = s3.jquery_ready.append
                    jappend('''
$('#req_commit_site_id_link').click(function(){
 var site_id=$('#req_commit_site_id').val()
 if(site_id){
  var url = $('#req_commit_site_id_link').attr('href')
  var exists=url.indexOf('?')
  if(exists=='-1'){
   $('#req_commit_site_id_link').attr('href',url+'?site_id='+site_id)
  }
 }
 return true
})''')
                    # Dropdown not Autocomplete
                    s3db.req_commit_item.req_item_id.widget = None

                    # Options updater for inline items
                    jappend('''
$.filterOptionsS3({
 'trigger':{'alias':'commit_item','name':'req_item_id'},
 'target':{'alias':'commit_item','name':'item_pack_id'},
 'scope':'row',
'lookupPrefix':'req',
'lookupResource':'req_item_packs',
'lookupKey':'req_item_id',
'lookupField':'id',
'msgNoRecords':i18n.no_packs,
'fncPrep':S3.supply.fncPrepItem,
'fncRepresent':S3.supply.fncRepresentItem
})''')
                    # Custom Form
                    s3forms = s3base.s3forms
                    crud_form = s3forms.S3SQLCustomForm(
                            "site_id",
                            "date",
                            "date_available",
                            "committer_id",
                            s3forms.S3SQLInlineComponent(
                                "commit_item",
                                label = T("Items"),
                                fields = ["req_item_id",
                                          "item_pack_id",
                                          "quantity",
                                          "comments"
                                          ]
                            ),
                            "comments",
                        )
                    s3db.configure("req_commit",
                                   crud_form = crud_form,
                                   )

                elif r.record.type == 3: # People
                    # Limit site_id to sites the user has permissions for
                    auth.permitted_facilities(table=r.table,
                          error_msg=T("You do not have permission for any facility to make a commitment."))
                    table.site_id.comment = A(T("Set as default Site"),
                                              _id="req_commit_site_id_link",
                                              _target="_blank",
                                              _href=URL(c="default",
                                                        f="user",
                                                        args=["profile"]))
                    # Limit organisation_id to organisations the user has permissions for
                    #auth.permitted_organisations(table=r.table, redirect_on_error=False)
                    #table.organisation_id.readable = True
                    #table.organisation_id.writable = True

                    # Limit commit skills to skills from the request
                    skills_filter(r.record.req_id)

                    # Custom Form
                    s3forms = s3base.s3forms
                    crud_form = s3forms.S3SQLCustomForm(
                            #"organisation_id",
                            "site_id",
                            "date",
                            "date_available",
                            "committer_id",
                            s3forms.S3SQLInlineComponent(
                                "commit_skill",
                                label = T("People"),
                                fields = ["quantity",
                                          "skill_id",
                                          "comments"
                                          ]
                            ),
                            "comments",
                        )
                    s3db.configure("req_commit",
                                   crud_form = crud_form,
                                   )

                else:
                    # Commits to Other requests can have an Organisation
                    # Limit organisation_id to organisations the user has permissions for
                    auth.permitted_organisations(table=r.table, redirect_on_error=False)
                    table.organisation_id.readable = True
                    table.organisation_id.writable = True
                    # Non-Item commits shouldn't have a From Inventory
                    # @ToDo: Assets do?
                    table.site_id.readable = False
                    table.site_id.writable = False

        return True
    s3.prep = prep

    def postp(r, output):
        if r.interactive and r.method != "import":
            if not r.component:
                req_types = settings.get_req_req_type()
                if "Stock" in req_types:
                    # Items
                    s3_action_buttons(r)
                    if len(req_types) != 1 and (get_vars.type != "1"):
                        # Restrict these Action Buttons to just those which are Items Requests
                        table = r.table
                        query = (table.deleted == False) & \
                                (table.type == 1)
                        rows = db(query).select(table.id)
                        restrict = [str(row.id) for row in rows]
                    else:
                        # All Requests are Items requests so no need to restrict
                        restrict = None
                    action = dict(label = s3_unicode(T("Prepare Shipment")).encode("utf8"),
                                  url = URL(f="send_commit",
                                            args=["[id]"],
                                            ),
                                  _class = "action-btn send-btn dispatch",
                                  )
                    if restrict is not None:
                        action["restrict"] = restrict
                    s3.actions.append(action)
                    confirm = T("Are you sure you want to send this shipment?")
                    s3.jquery_ready.append('''S3.confirmClick('.send-btn','%s')''' % confirm)

        return output
    s3.postp = postp

    return s3_rest_controller(rheader = commit_rheader)

# -----------------------------------------------------------------------------
dataset/ETHPy150Open sahana/eden/controllers/req.py/commit/swapped operands of `in`