class Event(DynamicDocument): """事件 gps_coordinate: {'type' : 'Point' , 'coordinates' : [x, y]} ip_coordinate: {'type' : 'Point' , 'coordinates' : [x, y]} address_coordinate: {'type' : 'Point' , 'coordinates' : [x, y]} """ meta = { "collection": "event", "indexes": [ "trx", "telephone", "product", "id_card", "ip", "device_id", "created", "event_type", "user_name" ], "ordering": ["-created"], "strict": False, "queryset_class": BaseQuerySet, } event_type = StringField(required=True, verbose_name="事件类型") trx = StringField(required=True, verbose_name="事务ID") product = StringField(required=True, verbose_name="产品编号") wx_openid = StringField(required=True, verbose_name="微信openid") wx_unionid = StringField(verbose_name="微信unionid") user_id = StringField(required=True, verbose_name="用户id") user_name = StringField(required=True, verbose_name="用户名称") device_id = StringField(required=True, verbose_name="设备指纹ID") device_info = DictField(verbose_name="设备信息") telephone = StringField(required=True, verbose_name="手机号") telephone_city = StringField(required=True, verbose_name="手机号归属城市") id_card = StringField(required=True, verbose_name="身份证号") id_card_city = StringField(required=True, verbose_name="身份证号归属城市") gps_coordinate = PointField(required=True, verbose_name="GPS纬度") gps_address = StringField(required=True, verbose_name="GPS地址") gps_city = StringField(required=True, verbose_name="GPS城市") ip = StringField(required=True, verbose_name="IP地址") ip_coordinate = PointField(required=True, verbose_name="IP坐标") ip_city = StringField(required=True, verbose_name="IP城市") address = StringField(required=True, verbose_name="地址") address_coordinate = PointField(required=True, verbose_name="联系地址坐标") address_city = StringField(required=True, verbose_name="联系地址城市") created = DateTimeField(default=datetime.utcnow, verbose_name="创建时间") decision = DictField(verbose_name="决策结果") features = DictField(verbose_name="特征结果")
class Exchange(BaseDocument): out_id = IntField() title = StringField() phone = StringField() address = StringField() geo = PointField() active = BooleanField(default=False) creation_date = DateTimeField() update_date = DateTimeField() def get_rating(self): return Request.objects(exchange=self).average('rating_value') def to_dict_impl(self, **kwargs): return { 'id': self.get_id(), 'out_id': self.out_id, 'title': self.title, 'phone': self.phone, 'geo': self.geo, 'address': self.address, 'active': self.active, 'creation_date': self.creation_date, 'update_date': self.update_date, 'rating': self.get_rating() } def save(self, *args, **kwargs): if not self.creation_date: self.creation_date = datetime.now(tz=timezone('UTC')) self.update_date = datetime.now(tz=timezone('UTC')) return super(Exchange, self).save(*args, **kwargs)
class School(Document): code = IntField(required=True, primary_key=True) name = StringField(required=True) street = StringField(required=True) suburb = StringField(required=True) postcode = IntField(required=True) phone = StringField(required=True) email = EmailField(required=True) website = URLField(required=True) logo = URLField(required=False) fax = StringField(required=True) enrolments = IntField(required=True) level = StringField(required=True) opportunity_classes = BooleanField(requred=True) selective = StringField(required=True) gender = StringField(required=True) location = PointField(required=True) preschool = BooleanField(required=True) late_opening = BooleanField(required=True) intensive_english_centre = BooleanField(required=True) healthy_canteen = BooleanField(required=True) indigenous_pct = FloatField(required=True) lbote_pct = FloatField(required=True) icsea = IntField(required=True) support_classes = ListField(required=False) attendance_rate = FloatField(required=False) selective_entry_score = IntField(required=False) opportunity_classes_entry_score = IntField(required=False) train_station_id = StringField(required=False) train_station = StringField(required=False) train_distance = IntField(required=False) train_duration = IntField(required=False) meta = {'collection': 'schools'} def __init__(self, code, name, street, suburb, postcode, phone, email, website, fax, enrolments, level, opportunity_classes, selective, gender, location, preschool, late_opening, intensive_english_centre, healthy_canteen, indigenous_pct, lbote_pct, icsea, *args, **kwargs): super().__init__(*args, **kwargs) self.code = code self.name = name self.street = street self.suburb = suburb self.postcode = postcode self.phone = phone self.email = email self.website = website self.fax = fax self.enrolments = enrolments self.level = level self.opportunity_classes = opportunity_classes self.selective = selective self.gender = gender self.location = location self.preschool = preschool self.late_opening = late_opening self.intensive_english_centre = intensive_english_centre self.healthy_canteen = healthy_canteen self.indigenous_pct = indigenous_pct self.lbote_pct = lbote_pct self.icsea = icsea
class Address(EmbeddedDocument): line1 = StringField(required=True, max_length=80) line2 = StringField(max_length=80) line3 = StringField(max_length=80) city = StringField(required=True, max_length=40) district = StringField(max_length=40) country = StringField(required=True, max_length=40) postcode = StringField(required=True, max_length=10) geolocation = PointField(required=True, auto_index=True) # geojson object extra = DictField() meta = { 'db_alias': 'main', 'collection': 'addresses', 'indexes': [ 'line1', 'line2', 'line3', 'postcode', 'district', 'city', 'country', 'geolocation', 'extra' ], } def __str__(self): return f'{self.geolocation}'
class TweetInformation(Document): tweet_id = StringField(required=True) text_full = StringField(required=True) coordinates = PointField(default=None, null=True) created_at = DateTimeField(required=True) screen_name = StringField(required=True) stores = ListField(StringField())
class Location(Document): location = PointField(required=True) created_date = DateTimeField(default=datetime.datetime.utcnow) created_by = ReferenceField(User) relation_desc = StringField(required=True) photo = ReferenceField(Photo) message = ReferenceField(Message)
class Normals(DynamicDocument): city = StringField(required=True) country = StringField(required=True) point = PointField(required=True) url = URLField(required=True) updated_at = DateTimeField(required=True, default=datetime.utcnow()) access_success = BooleanField(default=False) status_code = IntField(required=False)
class User(Document): name = StringField(required=True) mail = StringField(required=True) hash_password = StringField(required=True) credit = DecimalField(default=0.0) complete = BooleanField(default=False) current_location = PointField(required=False, default=[-103.392800, 20.675045])
class Restaurant(Document): name = StringField(required=True) location = PointField(required=True) city = StringField(required=True) img_url = StringField(required=False, default=random_img(urls)) category = StringField(required=True, default='Restaurante') delivery_cost = StringField(required=True, default='0') category_code = StringField(required=True) service_hours = StringField(required=True)
class Cinemainfo(Document): cid = IntField(required=True, unique=True) cityid = IntField() location = PointField() name = StringField() address = StringField() city = StringField() description = StringField() score = IntField() showinfo = StringField()
class Task(Document): name = StringField(required=True, max_length=30) requester_id = ObjectIdField(required=True) data_collection_id = ObjectIdField() questions = EmbeddedDocumentListField(Question) time_indication = FloatField(required=True) reward = FloatField(required=True) active = BooleanField(default=False) date_modified = DateTimeField(default=datetime.datetime.now) coordinates = PointField()
class ParkingSlot(Document): """ Parking slot used to train the models """ coordinates = PointField(required=True) district = StringField() price = FloatField(required=True) size = StringField() address = StringField() surface = FloatField()
class WorkerProperties(EmbeddedDocument): age = IntField() sex = StringField() address = StringField() # Country/GPS occupation = StringField() has_car = BooleanField() uses_public_transport = BooleanField() income = IntField() job_title = StringField() coordinates = PointField(default=[0, 0]) coordinates_updated = DateTimeField(default=datetime.datetime.min)
class Location(DynamicDocument): latlng = PointField() occured_at = DateTimeField() def to_dict(self): return {'id': str(self.id), 'latlng': self.latlng, 'occured_at': self.occured_at.isoformat()} @classmethod def post_save(cls, sender, document, **kwargs): tasks.run_scenarios.delay()
class Pdv(Document): trading_name = StringField(max_length=200, required=True) owner_name = StringField(max_length=100, required=True) document = StringField(max_length=20, required=True, unique=True) address = PointField(required=True) coverage_area = MultiPolygonField(required=True) meta = {"indexes": [[("coverage_area", "2dsphere")]]} def __str__(self): return "%s, %s" % (self.id, self.trading_name)
class Roof(db.Document): name = StringField(required=True, max_length=120) description = StringField() rating = FloatField(default=0.) location = PointField(required=True, auto_index=True) comments = EmbeddedDocumentListField(Comment, default=list) images = ListField(ReferenceField(RoofImage), default=list) def __init__(self, *args, **kwargs): if "lat" in kwargs.keys() and "lng" in kwargs.keys(): coords = [kwargs.pop("lat"), kwargs.pop("lng")] super().__init__(*args, **kwargs) self.location = coords else: super().__init__(*args, **kwargs) def update_rating(self): self.rating = reduce(lambda x, y: x + y.rating, self.comments, 0) / self.comments.count()
class Station(Document): id = IntField(primary_key=True) kind = StringField(db_field='k', max_length='1', min_length='1', required=True) name = StringField(db_field='n') location = PointField(db_field='l', auto_index=False) elevation = FloatField(db_field='e') depth = FloatField(db_field='d') creation_time = DateTimeField(db_field='c', required=True) client_id = StringField(db_field='i') state = StringField(db_field='s', min_length='1', max_length='1', required=True) signal_time = DateTimeField(db_field='t') def to_dict(self): '''Used for both JSON (for JavaScript in web app) and GraphQL.''' return { 'id': self.id, 'kind': self.kind, 'name': self.name, 'location': self.location, 'elevation': self.elevation, 'depth': self.depth, 'creation_time': self.creation_time.isoformat(), 'client_id': self.client_id, 'state': self.state, 'signal_time': self.signal_time.isoformat() if self.signal_time else None, }
class LoginData(EmbeddedDocument): created_datetime = DateTimeField(default=datetime.utcnow(), required=True) state = StringField() country = StringField() continent = StringField() accuracy_radius = IntField() geo_point = PointField() timezone = StringField() ip_address = StringField() def to_dict(self): return { 'created_datetime': str(self.created_datetime.isoformat()), 'state': self.state, 'country': self.country, 'continent': self.continent, 'accuracy_radius': self.accuracy_radius, 'geo_point': self.geo_point, 'timezone': self.timezone, 'ip_address': self.ip_address }
class GeoInfo(EmbeddedDocument): coords = PointField(required=True) accuracy = IntField(required=True, default=0) context = IntField(required=True, default=0) place_id = StringField(required=False) woe_id = LongField(required=False) region = StringField(required=False) city_part = StringField(required=False) @property def coordinates(self): return self.coords['coordinates'] @coordinates.setter def coordinates(self, coord_dict: dict): if 'lat' not in coord_dict or 'lng' not in coord_dict: raise ValueError( "error setting coordinates, lat or lng is not in dict") self.coords = [coord_dict['lng'], coord_dict['lat']] def set_coordinates(self, lat, lng): self.coords = [lng, lat]
class Location(Document): userId = EmbeddedDocumentField(User) location = ListField(PointField()) createdAt = DateTimeField(required=True) meta = {'collection': 'locations'}
class Area(Document): owner = ReferenceField('User', required=True) name = StringField(required=True) category = IntField(required=True, min_value=area_categories_map.minimum_key(), max_value=area_categories_map.maximum_key()) location = StringField(required=True) location_point = PointField() image = ImageField(size=(1920, 1080, False)) created_at = DateTimeField(default=datetime.now) updated_at = DateTimeField(default=datetime.now) def get_timestamp_common(self, field) -> int: return int(field.timestamp()) @property def created_at_timestamp(self): return self.get_timestamp_common(self.created_at) @property def updated_at_timestamp(self): return self.get_timestamp_common(self.updated_at) def put_b64_image(self, b64_string: str): byte_string = b64decode(b64_string) byte_array = bytearray(byte_string) with TemporaryFile() as f: f.write(byte_array) f.flush() f.seek(0) try: self.image.put(f) except GridFSError: self.image.replace(f) def get_b64_image_common(self, image: GridFSProxy): if not image: return None byte_array = image.read() if not byte_array: return None byte_string = bytes(byte_array) b64_string = b64encode(byte_string) return b64_string.decode('utf-8') def to_dict(self): # # HACK: location point is list when the object was created, but gets saved # as a dict with a coordinates key containing the list # if isinstance(self.location_point, list): location_points = self.location_point elif isinstance(self.location_point, dict): location_points = self.location_point['coordinates'] else: location_points = [] return { 'id': str(self.id), 'owner': self.owner.to_dict(), 'name': self.name, 'category': self.category, 'no_devices': 0, 'no_controllers': 0, 'location': self.location, 'location_point': location_points, 'created_at_timestamp': self.created_at_timestamp, 'updated_at_timestamp': self.updated_at_timestamp, 'image': self.get_b64_image_common(self.image), } def save(self, *args, **kwargs): if not self.created_at: self.created_at = datetime.now() self.updated_at = datetime.now() return super().save(*args, **kwargs)
class Node(Document): """ 站点: 带时刻人员表 """ name = StringField(min_length=1, max_length=64, unique=True, comment='站点名称') # 不允许重名, unique以后会自己index color = StringField(default="", max_length=64, comment="围栏显示颜色") loc = DictField(default=dict, comment='lat,lng,address...') point = PointField(comment='用于mongodb地理操作支持') time_table = ListField( default=list, comment='[{t=t, man={id, name, tel, m_type, client_id}},...]') create_time = DateTimeField(default=utc_now, comment='创建时间') update_time = DateTimeField(default=utc_now, comment='更新时间') meta = { 'db_alias': 'aeolus_connection', 'collection': 'node', 'ordering': [ '-update_time', ], 'indexes': [ '(point', # "2dsphere" 'time_table.t', 'time_table.man.id', 'time_table.man.tel', ] } def update(self, **kwargs): kwargs.update({'set__update_time': utc_now()}) return super(Node, self).update(**kwargs) def modify(self, **kwargs): kwargs.update({'set__update_time': utc_now()}) return super(Node, self).update(**kwargs) def save(self, **kwargs): self.update_time = utc_now() return super(Node, self).save(**kwargs) @staticmethod def _field_to_json(value): ret = value if isinstance(value, ObjectId): ret = safe_unicode(str(value)) elif isinstance(value, datetime): ret = utc_to_local(value) elif isinstance(value, EmbeddedDocument): if hasattr(value, "pack"): ret = value.pack() elif isinstance(value, Document): if hasattr(value, "pack"): ret = value.pack() else: ret = safe_unicode(value.pk) elif isinstance(value, list): ret = [Node._field_to_json(_) for _ in value] elif isinstance(value, dict): ret = {k: Node._field_to_json(value[k]) for k in value} return ret def pack(self, only=()): only = only if only else () expected = only if not only: # 有only只返回only; 没有only, 计算需要的字段: (_db_field_map | includes) - excludes expected = set([f for f in self]) packed = {} for field in expected: value = self[field] if hasattr(self, field) else None packed[field] = Node._field_to_json(value) return packed
class QueryNearbyDeliveries: """ Model Query Nearby Deliveries.""" radius = IntField(required=True) coordinates = PointField(required=True)
class Request(BaseDocument): TYPE_BUY_USD = 'buy_usd' TYPE_SAIL_USD = 'sale_usd' TYPE_BUY_EUR = 'buy_eur' TYPE_SAIL_EUR = 'sale_eur' TYPE_BUY_GBR = 'buy_gbr' TYPE_SAIL_GBR = 'sale_gbr' TYPE_BUY_CHF = 'buy_chf' TYPE_SAIL_CHF = 'sale_chf' TYPE = ( TYPE_BUY_USD, TYPE_SAIL_USD, TYPE_BUY_EUR, TYPE_SAIL_EUR, TYPE_BUY_GBR, TYPE_SAIL_GBR, TYPE_BUY_CHF, TYPE_SAIL_CHF ) number = SequenceField(unique=True) user = ReferenceField('User', reverse_delete_rule=mongoengine.NULLIFY, required=False) exchange = ReferenceField('Exchange', reverse_delete_rule=mongoengine.NULLIFY, required=False) step = IntField(default=0) type = StringField(choices=TYPE) course = FloatField(default=0) amount = IntField(default=0) geo = PointField() external_id = IntField() rating_request_send = BooleanField(default=False) fail_sended = BooleanField(default=False) creation_date = DateTimeField() update_date = DateTimeField() rating_value = IntField(default=None) rating_comment = StringField(default='') distance = FloatField(default=None) date_commission_withdrawal = DateTimeField(default=None) exchange_confirm = BooleanField(default=False) def to_dict_impl(self, **kwargs): result = { 'id': self.get_id(), 'number': self.number, 'external_id': self.external_id, 'step': self.step, 'type': self.type, 'amount': self.amount, 'geo': self.geo, 'course': self.course, 'creation_date': self.creation_date, 'update_date': self.update_date, 'rating_value': self.rating_value, 'rating_comment': self.rating_comment, 'distance': self.distance, 'exchange': self.exchange.to_dict() if self.exchange is not None else False, 'date_commission_withdrawal': self.date_commission_withdrawal if self.date_commission_withdrawal else None, 'exchange_confirm': True if self.exchange_confirm else False } try: result['user'] = self.user.to_dict() if self.user else {} except: result['user'] = {} return result def save(self, *args, **kwargs): if not self.creation_date: self.creation_date = datetime.now(tz=timezone('UTC')) self.update_date = datetime.now(tz=timezone('UTC')) return super(Request, self).save(*args, **kwargs)
class Agent(Document): point = PointField() speed = FloatField(default=0) heading = FloatField(default=0) destination_heading = FloatField(default=0) destination = PointField(required=False) stamp = DateTimeField(default=datetime.now) route = LineStringField() # route_flock = LineStringField() status = StringField(default="solo") meta = {'allow_inheritance': True} def to_dict(self): return {'agent_id': "%s" % self.id, 'point': self.point, 'status': self.status, 'in': True if self.status == 'flocking' else False, 'speed': self.speed, 'heading': float("%0.2f" % self.heading), 'destination_heading': float("%0.2f" % self.destination_heading), 'destination': self.destination, 'stamp': str(self.stamp)} def __str__(self): return "<A-%s [%s] %0.2fm @%sm/s>" % (str(self.id)[-3:], self.status, self.distance_to( self.destination), self.speed) def get_point_LatLon(self): return LatLon(Longitude(self.point['coordinates'][1]), Latitude(self.point['coordinates'][0])) def update(self, new_point, update_speed=False): """ updates time stamp uses @new_point to update: - point - heading - destination_heading - speed, if update_speed=True """ if 'coordinates' in self.point: a = LatLon(Longitude(self.point['coordinates'][1]), Latitude(self.point['coordinates'][0])) else: a = LatLon(Longitude(self.point[1]), Latitude(self.point[0])) if 'coordinates' in new_point: b = LatLon(Longitude(new_point['coordinates'][1]), Latitude(new_point['coordinates'][0])) else: b = LatLon(Longitude(new_point[1]), Latitude(new_point[0])) if 'coordinates' in self.destination: c = LatLon(Longitude(self.destination['coordinates'][1]), Latitude(self.destination['coordinates'][0])) else: c = LatLon(Longitude(self.destination[1]), Latitude(self.destination[0])) self.heading = a.heading_initial(b) self.destination_heading = b.heading_initial(c) if update_speed: tdelta = datetime.now() - self.stamp seconds = tdelta.total_seconds() distance = a.distance(b) / 1000.0 self.speed = distance / seconds self.stamp = datetime.now() self.point = new_point self.save() def heading_to(self, other_point): """ heading from my point to @other_point """ if 'coordinates' in self.point: a = LatLon(Longitude(self.point['coordinates'][1]), Latitude(self.point['coordinates'][0])) else: a = LatLon(Longitude(self.point[1]), Latitude(self.point[0])) b = LatLon(Longitude(other_point[1]), Latitude(other_point[0])) return a.heading_initial(b) def distance_to(self, other_point): """ distance from agent to another point, in metres """ if 'coordinates' in self.point: s = LatLon(Latitude(self.point['coordinates'][1]), Longitude(self.point['coordinates'][0])) else: s = LatLon(Latitude(self.point[1]), Longitude(self.point[0])) if 'coordinates' in other_point: t = LatLon(Latitude(other_point['coordinates'][1]), Longitude(other_point['coordinates'][0])) else: t = LatLon(Latitude(other_point[1]), Longitude(other_point[0])) return s.distance(t) * 1000.0 def got_there(self): """ return True if one step or less away """ if self.distance_to(self.destination) < self.speed: return True else: return False def step(self): """ move to next point in route """ self.reload() if self.route is None: self.update(self.destination) else: coordinates = self.route['coordinates'][:] if len(coordinates) == 2: p = coordinates[0] self.route = None else: p = coordinates.pop(0) self.route = coordinates self.save() self.update(p) def flock(self, radius): return Agent.objects(point__near=self.point, point__max_distance=radius, heading__gte=self.heading - 0.15, heading__lte=self.heading + 0.15)
class JunctionMeta(EmbeddedDocument): location = PointField(required=True) sales_id = IntField(min_value=0, required=True) use_default_vi4 = BooleanField(default=True, required=True) address_reference = StringField()