def save_email(subject, text_body, html_body, sequence, audience, tags):
    """ tags should be a comma separated list of tags """
    email_db = db.Email(subject=subject,
                        text_body=text_body,
                        html_body=html_body,
                        sequence=sequence,
                        audience=audience,
                        tags=tags)
    email_db.save()
    return get_email(id2uri(email_db.id))
Example #2
0
class Widget(db.MoraModel):
    # Primitives
    int_ = db.IntegerProperty(default=13)
    float_ = db.FloatProperty(default=1.3)
    bool_ = db.BooleanProperty(default=True)
    str_ = db.StringProperty(default='word')
    text = db.TextProperty(default='word word word')

    # Temporal
    date = db.DateProperty(default=datetime.date(1983, 10, 11))
    time = db.TimeProperty(default=datetime.time(1))
    datetime = db.DateTimeProperty(default=datetime.datetime(1983, 10, 11))

    # Binary data
    byte_str = db.ByteStringProperty(default=b'word')
    blob = db.BlobProperty(default=b'blobword')
    #blob_ref = db.BlobReferenceProperty(default='fake')

    # Special Google Data Protocol & GeoRSS GML Properties
    # These are XML like properties that correspond to GDP and GeoRSS
    geopt = db.GeoPtProperty(default=db.GeoPt(lat=1.3, lon=1.3))
    address = db.PostalAddressProperty(
        default=db.PostalAddress("1600 Ampitheater Pkwy., Mountain View, CA"))
    phone = db.PhoneNumberProperty(default=db.PhoneNumber("1 (206) 555-1212"))
    email = db.EmailProperty(default=db.Email("*****@*****.**"))
    im = db.IMProperty(default=db.IM("http://example.com/", "Larry97"))
    link = db.LinkProperty(default=db.Link("http://www.google.com/"))
    category = db.CategoryProperty(default=db.Category("kittens"))
    rating = db.RatingProperty(default=db.Rating(97))

    # Special User Property
    # TODO: test user property
    # user = db.UserProperty(default=users.User(
    #         email='*****@*****.**',
    #         federated_identity='fake'))

    # References
    reference = db.ReferenceProperty(None)
    self_reference = db.SelfReferenceProperty()

    # Lists
    list_ = db.ListProperty(int, default=[13])
    str_list = db.StringListProperty(default=['one'])
Example #3
0
 def insert(self) -> Tuple[str, bool]:
     nonce = get_random_bytes(16)
     session = db.get_session()
     email = db.Email(email=self.email)
     device = db.Device(pubkey=self.pubkey,
                        email=self.email,
                        nonce=nonce.hex(),
                        is_auth=False)
     session.add_all([email, device])
     try:
         session.commit()
         hex_encrypted_nonce = crypto.encrypt_rsa(nonce, self.pubkey).hex()
         mail.send_nonce(self.email, hex_encrypted_nonce)
         return hex_encrypted_nonce, True
     except Exception as e:
         print(e)
         session.rollback()
         return "", False
     finally:
         session.close()
Example #4
0
    def testNoneProperties(self):
        none = MoraNone()
        none.save()

        ## Integer

        # should be None by default
        self.assertEqual(none.int_, None)
        self.assertEqual(none.as_json()['int_'], None)

        # check inserting an integer
        none.int_ = 1
        none.save()
        self.assertEqual(none.int_, 1)
        self.assertEqual(none.as_json()['int_'], 1)

        # insert None
        none.int_ = None
        none.save()
        self.assertEqual(none.int_, None)
        self.assertEqual(none.as_json()['int_'], None)

        ## Float

        # should be None by default
        self.assertEqual(none.float_, None)
        self.assertEqual(none.as_json()['float_'], None)

        # check inserting a float
        none.float_ = 1.3
        none.save()
        self.assertEqual(none.float_, 1.3)
        self.assertEqual(none.as_json()['float_'], 1.3)

        # insert None
        none.float_ = None
        none.save()
        self.assertEqual(none.float_, None)
        self.assertEqual(none.as_json()['float_'], None)

        ## Boolean

        # should be None by default
        self.assertEqual(none.bool_, None)
        self.assertEqual(none.as_json()['bool_'], None)

        # check inserting a boolean value
        none.bool_ = False
        none.save()
        self.assertEqual(none.bool_, False)
        self.assertEqual(none.as_json()['bool_'], False)

        # insert None
        none.bool_ = None
        none.save()
        self.assertEqual(none.bool_, None)
        self.assertEqual(none.as_json()['bool_'], None)

        ## String

        # should be None by default
        self.assertEqual(none.str_, None)
        self.assertEqual(none.as_json()['str_'], None)

        # check inserting a string
        none.str_ = 'insert a string'
        none.save()
        self.assertEqual(none.str_, 'insert a string')
        self.assertEqual(none.as_json()['str_'], 'insert a string')

        # insert None
        none.str_ = None
        none.save()
        self.assertEqual(none.str_, None)
        self.assertEqual(none.as_json()['str_'], None)

        ## Text

        # should be None by default
        self.assertEqual(none.txt, None)
        self.assertEqual(none.as_json()['txt'], None)

        # check inserting a string
        none.txt = 'insert a string'
        none.save()
        self.assertEqual(none.txt, 'insert a string')
        self.assertEqual(none.as_json()['txt'], 'insert a string')

        # insert None
        none.txt = None
        none.save()
        self.assertEqual(none.txt, None)
        self.assertEqual(none.as_json()['txt'], None)

        ## Byte String

        # should be None by default
        self.assertEqual(none.byte_str, None)
        self.assertEqual(none.as_json()['byte_str'], None)

        # check inserting a string
        none.byte_str = 'insert a string'
        none.save()
        self.assertEqual(none.byte_str, 'insert a string')
        self.assertEqual(none.as_json()['byte_str'], 'aW5zZXJ0IGEgc3RyaW5n')

        # insert None
        none.byte_str = None
        none.save()
        self.assertEqual(none.byte_str, None)
        self.assertEqual(none.as_json()['byte_str'], None)

        ## Blob

        # should be None by default
        self.assertEqual(none.blob, None)
        self.assertEqual(none.as_json()['blob'], None)

        # check inserting a string
        none.blob = 'insert a string'
        none.save()
        self.assertEqual(none.blob, 'insert a string')
        self.assertEqual(none.as_json()['blob'], 'aW5zZXJ0IGEgc3RyaW5n')

        # insert None
        none.blob = None
        none.save()
        self.assertEqual(none.blob, None)
        self.assertEqual(none.as_json()['blob'], None)

        ## Date

        # should be None by default
        self.assertEqual(none.date, None)
        self.assertEqual(none.as_json()['date'], None)

        # check inserting a date
        d = datetime.date(1983, 4, 5)
        none.date = d
        none.save()
        self.assertEqual(none.date, d)
        self.assertEqual(none.as_json()['date'],
                         date_to_datetime(d).isoformat('T') + '+00:00')

        # insert None
        none.date = None
        none.save()
        self.assertEqual(none.date, None)
        self.assertEqual(none.as_json()['date'], None)

        ## Time

        # should be None by default
        self.assertEqual(none.time, None)
        self.assertEqual(none.as_json()['time'], None)

        # check inserting a time
        t = datetime.time(1)
        none.time = t
        none.save()
        self.assertEqual(none.time, t)
        self.assertEqual(none.as_json()['time'],
                         time_to_datetime(t).isoformat('T') + '+00:00')

        # insert None
        none.time = None
        none.save()
        self.assertEqual(none.time, None)
        self.assertEqual(none.as_json()['time'], None)

        ## Datetime

        # should be None by default
        self.assertEqual(none.datetime, None)
        self.assertEqual(none.as_json()['datetime'], None)

        # check inserting datetime
        dt = datetime.datetime(1983, 4, 5)
        none.datetime = dt
        none.save()
        self.assertEqual(none.datetime, dt)
        self.assertEqual(none.as_json()['datetime'],
                         dt.isoformat('T') + '+00:00')

        # insert None
        none.datetime = None
        none.save()
        self.assertEqual(none.datetime, None)
        self.assertEqual(none.as_json()['datetime'], None)

        ## GeoPt

        # should be None by default
        self.assertEqual(none.geopt, None)
        self.assertEqual(none.as_json()['geopt'], None)

        # check inserting a GeoPt
        none.geopt = db.GeoPt('13.1,42.1')
        none.save()
        self.assertEqual(none.geopt, db.GeoPt('13.1,42.1'))
        self.assertEqual(none.as_json()['geopt'], {'lat': 13.1, 'lon': 42.1})

        # insert None
        none.geopt = None
        none.save()
        self.assertEqual(none.geopt, None)
        self.assertEqual(none.as_json()['geopt'], None)

        ## Postal Address

        # should be None by default
        self.assertEqual(none.address, None)
        self.assertEqual(none.as_json()['address'], None)

        # check inserting a PostalAddress
        none.address = db.PostalAddress('0001 Cemetery Lane\nNew York, NY')
        none.save()
        self.assertEqual(none.address,
                         db.PostalAddress('0001 Cemetery Lane\nNew York, NY'))
        self.assertEqual(none.as_json()['address'],
                         '0001 Cemetery Lane\nNew York, NY')

        # insert None
        none.address = None
        none.save()
        self.assertEqual(none.address, None)
        self.assertEqual(none.as_json()['address'], None)

        ## Phone Number

        # should be None by default
        self.assertEqual(none.phone, None)
        self.assertEqual(none.as_json()['phone'], None)

        # check inserting a PhoneNumber
        none.phone = db.PhoneNumber('555-4823')
        none.save()
        self.assertEqual(none.phone, db.PhoneNumber('555-4823'))
        self.assertEqual(none.as_json()['phone'], '555-4823')

        # insert None
        none.phone = None
        none.save()
        self.assertEqual(none.phone, None)
        self.assertEqual(none.as_json()['phone'], None)

        ## Email

        # should be None by default
        self.assertEqual(none.email, None)
        self.assertEqual(none.as_json()['email'], None)

        # check inserting an Email
        none.email = db.Email('*****@*****.**')
        none.save()
        self.assertEqual(none.email, db.Email('*****@*****.**'))
        self.assertEqual(none.as_json()['email'],
                         '*****@*****.**')

        # insert None
        none.email = None
        none.save()
        self.assertEqual(none.email, None)
        self.assertEqual(none.as_json()['email'], None)

        ## IM

        # should be None by default
        self.assertEqual(none.im, None)
        self.assertEqual(none.as_json()['im'], None)

        # check inserting an IM account
        none.im = db.IM('http://aim.com dlynch')
        none.save()
        self.assertEqual(none.im, db.IM('http://aim.com dlynch'))
        self.assertEqual(none.as_json()['im'], {
            'protocol': 'http://aim.com',
            'address': 'dlynch'
        })

        # insert None
        none.im = None
        none.save()
        self.assertEqual(none.im, None)
        self.assertEqual(none.as_json()['im'], None)

        ## Link

        # should be None by default
        self.assertEqual(none.link, None)
        self.assertEqual(none.as_json()['link'], None)

        # check inserting a Link
        none.link = db.Link('http://apple.com/')
        none.save()
        self.assertEqual(none.link, db.Link('http://apple.com/'))
        self.assertEqual(none.as_json()['link'], 'http://apple.com/')

        # insert None
        none.link = None
        none.save()
        self.assertEqual(none.link, None)
        self.assertEqual(none.as_json()['link'], None)

        ## Category

        # should be None by default
        self.assertEqual(none.category, None)
        self.assertEqual(none.as_json()['category'], None)

        # check inserting a Category
        none.category = db.Category('lolcatz')
        none.save()
        self.assertEqual(none.category, db.Category('lolcatz'))
        self.assertEqual(none.as_json()['category'], 'lolcatz')

        # insert None
        none.category = None
        none.save()
        self.assertEqual(none.category, None)
        self.assertEqual(none.as_json()['category'], None)

        ## Rating

        # should be None by default
        self.assertEqual(none.rating, None)
        self.assertEqual(none.as_json()['rating'], None)

        # check inserting a Rating
        none.rating = db.Rating(99)
        none.save()
        self.assertEqual(none.rating, db.Rating(99))
        self.assertEqual(none.as_json()['rating'], 99)

        # insert None
        none.rating = None
        none.save()
        self.assertEqual(none.rating, None)
        self.assertEqual(none.as_json()['rating'], None)

        ## Reference

        # should be None by default
        self.assertEqual(none.reference, None)
        self.assertEqual(none.as_json()['reference'], None)

        # check inserting a Reference
        base = Base()
        base.save()
        none.reference = base
        none.save()
        self.assertEqual(none.reference.key(), base.key())
        self.assertEqual(none.as_json()['reference'], str(base.key()))

        # insert None
        none.reference = None
        none.save()
        self.assertEqual(none.reference, None)
        self.assertEqual(none.as_json()['reference'], None)

        ## Self Reference

        # should be None by default
        self.assertEqual(none.self_reference, None)
        self.assertEqual(none.as_json()['self_reference'], None)

        # check inserting a SelfReference
        none1 = MoraNone()
        none1.save()

        none.self_reference = none1
        none.save()
        self.assertEqual(none.self_reference.key(), none1.key())
        self.assertEqual(none.as_json()['self_reference'], str(none1.key()))

        # insert None
        none.self_reference = None
        none.save()
        self.assertEqual(none.self_reference, None)
        self.assertEqual(none.as_json()['self_reference'], None)

        ## Blob Reference

        # should be None by default
        # self.assertEqual(none.blob_reference, None)
        # self.assertEqual(none.as_json()['blob_reference'], None)

        # check inserting a BlobReference
        # none.blob_reference = blobstore.BlobKey('fake')
        # none.save()
        # self.assertEqual(none.blob_reference.key(),
        #                  blobstore.BlobKey('fake'))
        # self.assertEqual(none.as_json()['blob_reference'],
        #                  str(blobstore.BlobKey('fake')))

        # insert None
        # none.blob_reference = None
        # none.save()
        # self.assertEqual(none.blob_reference, None)
        # self.assertEqual(none.as_json()['blob_reference'], None)

        ## List

        # should be [] by default, not None
        # TODO: verify this is true with GAE proper
        self.assertEqual(none.list_, [])
        self.assertEqual(none.as_json()['list_'], [])

        # check inserting a list
        none.list_ = [1]
        none.save()
        self.assertEqual(none.list_, [1])
        self.assertEqual(none.as_json()['list_'], [1])

        # can not insert None, must use empty list
        # TODO: verify this is true with GAI proper
        none.list_ = []
        none.save()
        self.assertEqual(none.list_, [])
        self.assertEqual(none.as_json()['list_'], [])

        ## String List

        # should be [] by default, not None
        # TODO: verify this is true with GAE proper
        self.assertEqual(none.str_list, [])
        self.assertEqual(none.as_json()['str_list'], [])

        # check inserting a string list
        none.str_list = ['a']
        none.save()
        self.assertEqual(none.str_list, ['a'])
        self.assertEqual(none.as_json()['str_list'], ['a'])

        # can not insert None, must use empty list
        # TODO: verify this is true with GAE proper
        none.str_list = []
        none.save()
        self.assertEqual(none.str_list, [])
        self.assertEqual(none.as_json()['str_list'], [])