Beispiel #1
0
class UserLocation(db.EmbeddedDocument):
    date = db.DateTimeField(default=datetime.datetime.utcnow)
    position = db.GeoPointField()
    duration = db.IntField(default=0)

    def __str__(self):
        return str(self.position.join(','))
Beispiel #2
0
class Spot(db.Document):
    name = db.StringField(required=True)
    user = db.ReferenceField(User)
    location = db.GeoPointField(required=True)
    wifi = db.IntField(min_value=0, max_value=5)
    power = db.BooleanField()
    category = db.StringField()
    comments = db.ListField(db.StringField())
    creation_date = db.DateTimeField(default=datetime.datetime.utcnow)
    user_url = db.StringField()

    meta = {'ordering': ['-creation_date']}

    def __str__(self):
        return self.name

    def to_dict(self):
        data = self.to_mongo()
        data['creation_date'] = str(self.creation_date.timestamp())
        data['id'] = str(self.id)
        del data['_id']
        data['user'] = str(data['user'])
        return data

    def to_json(self):
        return json.dumps(self.to_dict())
Beispiel #3
0
class Spot(db.Document):
    name = db.StringField(required=True)
    user = db.ReferenceField(User)
    location = db.GeoPointField(required=True)
    wifi = db.IntField(min_value=0, max_value=5)
    power = db.BooleanField()
    category = db.StringField()
    comments = db.ListField(db.StringField())
    creation_date = db.DateTimeField(default=datetime.datetime.utcnow)

    meta = {'ordering': ['-creation_date']}

    def __str__(self):
        return self.name
Beispiel #4
0
class User(UserMixin, db.Document):
    email = db.EmailField(unique=True)
    password = db.StringField(required=True, min_length=32, max_length=120)
    creation_date = db.DateTimeField(default=datetime.datetime.utcnow)
    username = db.StringField(unique=True)
    about = db.StringField()
    slug = db.StringField(unique=True)
    locations = db.EmbeddedDocumentListField(UserLocation, default=[])
    allow_localization = db.BooleanField()
    current_location = db.GeoPointField()
    socials = db.DictField()

    def set_password(self, password):
        self.password = generate_password_hash(password)
        return self

    def check_password(self, password):
        return check_password_hash(self.password, password)

    @property
    def avatar(self):
        return '{}/{}'.format(app.config.get('AVATARS_URL'), self.id)

    def clean_username(self):
        self.username = Markup(self.username).striptags()

    def clean_about(self):
        self.about = (Markup(self.about.replace('<br>',
                                                '\^n^')).striptags().replace(
                                                    '\^n^', '\n'))

    def __str__(self):
        return str(self.username or 'Unknown')

    def save(self, *args, **kwargs):
        if self.slug is None:
            try:
                User.objects.get(username=self.username)
                # If the code above is reach on the block try, it means that
                # there is a duplicate
                self.username = uniquify_username(self.username)
            except User.DoesNotExist:
                pass
        self.slug = slugify(self.username)
        return super().save(*args, **kwargs)