def apply_method(self, r, **attr): """ API entry point @param r: the S3Request instance @param attr: controller attributes for the request """ if r.http in ("GET", "POST", "DELETE"): if r.record: # Initialize CRUD form self.settings = current.response.s3.crud self.sqlform = sqlform = self._config("crud_form") if not sqlform: from s3forms import S3SQLDefaultForm self.sqlform = S3SQLDefaultForm() # Render page output = self.profile(r, **attr) elif r.representation not in ("dl", "aadata"): # Redirect to the List View redirect(r.url(method="")) else: # No point redirecting r.error(404, current.ERROR.BAD_RECORD) else: r.error(405, current.ERROR.BAD_METHOD) return output
def _form(self, r, widget, **attr): """ Generate a Form widget @param r: the S3Request instance @param widget: the widget as a tuple: (label, type, icon) @param attr: controller attributes for the request """ label = widget.get("label", "") if label: label = current.T(label) icon = widget.get("icon", "") if icon: icon = ICON(icon) context = widget.get("context", None) tablename = widget.get("tablename", None) resource, context = self._resolve_context(r, tablename, context) record = resource.select(["id"], limit=1, as_rows=True).first() if record: record_id = record.id else: record_id = None if record_id: readonly = not current.auth.s3_has_permission("update", tablename, record_id) else: readonly = not current.auth.s3_has_permission("create", tablename) sqlform = widget.get("sqlform", None) if not sqlform: sqlform = resource.get_config("crud_form") if not sqlform: from s3forms import S3SQLDefaultForm sqlform = S3SQLDefaultForm() get_config = current.s3db.get_config if record_id: # Update form onvalidation = get_config(tablename, "create_onvalidation") or \ get_config(tablename, "onvalidation") onaccept = get_config(tablename, "create_onaccept") or \ get_config(tablename, "onaccept") else: # Create form onvalidation = get_config(tablename, "create_onvalidation") or \ get_config(tablename, "onvalidation") onaccept = get_config(tablename, "create_onaccept") or \ get_config(tablename, "onaccept") form = sqlform(request = r, resource = resource, record_id = record_id, readonly = readonly, format = "html", onvalidation = onvalidation, onaccept = onaccept, ) _class = self._lookup_class(r, widget) # Render the widget output = DIV(H4(icon, label, _class="profile-sub-header"), DIV(form, _class="form-container thumbnail"), _class=_class) return output