def model(self):

        T = current.T
        db = current.db

        UNKNOWN_OPT = current.messages.UNKNOWN_OPT

        configure = self.configure
        define_table = self.define_table

        # Message priority
        msg_priority_opts = {3 : T("High"),
                             2 : T("Medium"),
                             1 : T("Low"),
                             }

        # ---------------------------------------------------------------------
        # Message Super Entity - all Inbound & Outbound Messages
        #

        message_types = Storage(msg_email = T("Email"),
                                msg_facebook = T("Facebook"),
                                msg_rss = T("RSS"),
                                msg_sms = T("SMS"),
                                msg_twitter = T("Twitter"),
                                msg_twitter_result = T("Twitter Search Results"),
                                )

        tablename = "msg_message"
        self.super_entity(tablename, "message_id",
                          message_types,
                          # Knowing which Channel Incoming Messages
                          # came in on allows correlation to Outbound
                          # messages (campaign_message, deployment_alert, etc)
                          self.msg_channel_id(),
                          s3_datetime(default="now"),
                          Field("body", "text",
                                label = T("Message"),
                                ),
                          Field("from_address",
                                label = T("From"),
                                ),
                          Field("to_address",
                                label = T("To"),
                                ),
                          Field("inbound", "boolean",
                                default = False,
                                label = T("Direction"),
                                represent = lambda direction: \
                                            (direction and [T("In")] or \
                                                           [T("Out")])[0],
                                ),
                          )

        # @todo: make lazy_table
        table = db[tablename]
        table.instance_type.readable = True
        table.instance_type.writable = True

        configure(tablename,
                  list_fields = ["instance_type",
                                 "from_address",
                                 "to_address",
                                 "body",
                                 "inbound",
                                 ],
                  )

        # Reusable Field
        message_represent = S3Represent(lookup=tablename, fields=["body"])
        message_id = S3ReusableField("message_id", "reference %s" % tablename,
                                     ondelete = "RESTRICT",
                                     represent = message_represent,
                                     requires = IS_EMPTY_OR(
                                        IS_ONE_OF_EMPTY(db, "msg_message.id")),
                                     )

        self.add_components(tablename,
                            msg_attachment = "message_id",
                            deploy_response = "message_id",
                            )

        # ---------------------------------------------------------------------
        # Outbound Messages
        #

        # Show only the supported messaging methods
        MSG_CONTACT_OPTS = current.msg.MSG_CONTACT_OPTS

        # Maximum number of retries to send a message
        MAX_SEND_RETRIES = current.deployment_settings.get_msg_max_send_retries()

        # Valid message outbox statuses
        MSG_STATUS_OPTS = {1 : T("Unsent"),
                           2 : T("Sent"),
                           3 : T("Draft"),
                           4 : T("Invalid"),
                           5 : T("Failed"),
                           }

        opt_msg_status = S3ReusableField("status", "integer",
                                         notnull=True,
                                         requires = IS_IN_SET(MSG_STATUS_OPTS,
                                                              zero=None),
                                         default = 1,
                                         label = T("Status"),
                                         represent = lambda opt: \
                                                     MSG_STATUS_OPTS.get(opt,
                                                                 UNKNOWN_OPT))

        # Outbox - needs to be separate to Message since a single message
        # sent needs different outbox entries for each recipient
        tablename = "msg_outbox"
        define_table(tablename,
                     # FK not instance
                     message_id(),
                     # Person/Group to send the message out to:
                     self.super_link("pe_id", "pr_pentity"),
                     # If set used instead of picking up from pe_id:
                     Field("address"),
                     Field("contact_method", length=32,
                           default = "EMAIL",
                           label = T("Contact Method"),
                           represent = lambda opt: \
                                       MSG_CONTACT_OPTS.get(opt, UNKNOWN_OPT),
                           requires = IS_IN_SET(MSG_CONTACT_OPTS,
                                                zero=None),
                           ),
                     opt_msg_status(),
                     # Used to loop through a PE to get it's members
                     Field("system_generated", "boolean",
                           default = False,
                           ),
                     # Give up if we can't send after MAX_RETRIES
                     Field("retries", "integer",
                           default = MAX_SEND_RETRIES,
                           readable = False,
                           writable = False,
                           ),
                     *s3_meta_fields())

        configure(tablename,
                  list_fields = ["id",
                                 "message_id",
                                 "pe_id",
                                 "status",
                                 ],
                  orderby = "msg_outbox.created_on desc",
                  )

        # ---------------------------------------------------------------------
        # Pass names back to global scope (s3.*)
        return dict(msg_message_id = message_id,
                    msg_message_represent = message_represent,
                    )

    # -------------------------------------------------------------------------
dataset/ETHPy150Open sahana/eden/modules/s3db/msg.py/S3MessageModel.model/original