class PlayList(mongoengine.Document): # Playlist fields _id = mongoengine.UUIDField(binary=False, primary_key=True) name = mongoengine.StringField() summary = mongoengine.StringField() videos = mongoengine.ListField(mongoengine.UUIDField(binary=False)) channel = mongoengine.UUIDField(binary=False) tags = mongoengine.ListField(mongoengine.StringField(max_length=50)) def save(self, *args, **kwargs): # Update id value with unique id self._id = uuid.uuid4() # Provide default role self.role = 'user' # Encode current password current_password = str.encode(self.password) # Hash password hashed = bcrypt.hashpw(current_password, bcrypt.gensalt(10)) # Update password self.password = hashed.decode("utf-8") # Proceed save super(PlayList, self).save(*args, **kwargs)
class Seed(BaseJinxObject, mongoengine.Document): """ Seed base class schema for MongoDB engine """ _name = 'seed' meta = {'collection': 'seed'} INTERFACE_STRING = "seed" # Required fields pod_stalk_uuid = mongoengine.UUIDField(required=True, dispName="Pod Stalk UUID") seed_stalk_uuid = mongoengine.UUIDField(required=True, dispName="Seed Stalk UUID")
class Organization(mongo.Document): id = mongo.UUIDField(required=False) name = mongo.StringField(max_length=255, required=True, unique=True) address = mongo.StringField(max_length=255, required=False) city = mongo.StringField(max_length=255, required=False) state = mongo.StringField(max_length=255, required=False) zip = mongo.StringField(max_length=50, required=False) point = mongo.PointField(required=False) phone = mongo.StringField(max_length=20, required=False) website = mongo.StringField(required=False) description = mongo.StringField(required=False) privacy = mongo.StringField(required=True, default='') last_updated = mongo.DateTimeField(required=True, default=datetime.datetime.now()) created_by = mongo.StringField(required=False) def save(self, *args, **kwargs): self.last_updated = datetime.datetime.now() address = (self.address + ", " + self.zip, self.city, self.state) geocoded_addr = verify_address(address) if geocoded_addr != 'Error': self.point = geocoded_addr super(Organization, self).save(*args, **kwargs) def location_count(self): return Location.objects.filter(org=str(self.id)).count() def location_set(self): return Location.objects.filter(org=str(self.id))
class Taxonomy(mongo.Document): id = mongo.UUIDField(required=False) name = mongo.StringField(required=True, default='', unique=True) privacy = mongo.StringField(required=True, default='') description = mongo.StringField(required=True, default='') inherit = mongo.ReferenceField('self', required=False) model = mongo.StringField(required=False, default='') compare = mongo.StringField(required=False, default='') model_id = mongo.StringField(required=False) custom_field_form = mongo.ReferenceField("FormSchema", required=False) created = mongo.StringField(required=True, default=str(datetime.now())) updated = mongo.StringField(required=True, default=str(datetime.now())) created_by = mongo.StringField(required=False) @property def group(self): return "Taxonomy" @property def number_using(self): count = [ l for l in loc.models.LocationType.objects() if self in l.taxonomies ] return len(count)
class DataImport(mongo.Document): id = mongo.UUIDField(required=False) upload_type = mongo.StringField(required=True) upload_format = mongo.StringField(required=True) duplicate_content = mongo.StringField(required=True) description = mongo.StringField(required=False) content_status = mongo.StringField(required=False) content_author = mongo.StringField(required=False) upload_file = mongo.StringField(required=False) upload_url = mongo.URLField(required=False) upload_freq = mongo.IntField(required=False) last_updated = mongo.DateTimeField(required=True) fields = mongo.ListField(required=False) name = mongo.StringField(required=True) meta = {'allow_inheritance': True} @property def datatype(self): dt = { "0": "csv", "1": "json", "2": "shapefile", "3": "geojson", } return dt.get(self.upload_format, "unknown") @property def group(self): if self.upload_type == "0": return "Imported from File" else: return "Imported from Live Feed"
class Playlists(me.Document): record_id = me.UUIDField(default=uuid.uuid4(), unique=True) playlist_id = me.StringField(required=True) user = me.StringField(required=True) api = me.StringField(required=True) is_active = me.BooleanField(default=True) created_at = me.DateTimeField(default=datetime.utcnow) @staticmethod def add_playlist(playlist_id: str, username: str, api: str) -> str: is_exist = Playlists.objects(playlist_id=playlist_id).count() if not is_exist: client = get_interface(api) if client: playlist_resolve = client.playlist_is_exists(playlist_id) if playlist_resolve: new_playlist = Playlists(playlist_id=playlist_id, user=username, api=api) new_playlist.save() return "Success, playlist has been added" else: return "Error, playlist not found" else: return "Error, playlist already exists, playlist must be public" @staticmethod def remove_playlist(playlist_id: str, username: str, api: str) -> str: playlist = Playlists.objects(playlist_id=playlist_id, user=username, api=api).first() if playlist: playlist.delete() return "Success, playlist has been removed" return "Error, playlist not found"
class Token(me.Document): """ Represents a token issued by SNI. """ class TokenType(str, Enum): """ Enumeration containing the various token types. """ dyn = "dyn" # Dynamic app token per = "per" # Permanent app token use = "use" # User token callback = me.URLField(default=None, null=True) """Callback URL of the application. When a new user token is issued, the corresponding application is notified at this URL.""" comments = me.StringField(default=str) """Comments""" created_on = me.DateTimeField(default=utils.now, required=True) """Timestamp of the creation of this document""" expires_on = me.DateTimeField(default=None, null=True) """Self explanatory""" owner = me.ReferenceField(User, required=True, reverse_delete_rule=me.CASCADE) """Reference to the owner of this token""" parent = me.ReferenceField("self", default=None, null=True, reverse_delete_rule=me.CASCADE) """Optional reference to the token that has been used to create this one""" token_type = me.StringField(choices=TokenType, required=True) """Token type, see :class:`sni.uac.models.Token.TokenType`""" uuid = me.UUIDField(binary=False, unique=True) """ UUID of this token Todo: Use the document's ``_id`` field instead. """ meta = { "indexes": [ { "fields": ["expires_on"], "expireAfterSeconds": 0, }, ], } def __repr__(self) -> str: return f"<Token: {repr(self.owner)} {self.token_type}>"
class Device(gj.EmbeddedDocument): uuid = db.UUIDField(required=True, default=lambda: str(uuid.uuid4()), binary=False) online = db.BooleanField(default=False) controller_type = db.StringField() created_at = db.DateTimeField(default=datetime.datetime.utcnow) last_update = db.DateTimeField(default=datetime.datetime.utcnow)
class Article(Document): """Test schema.""" user = db.ReferenceField(User) addition = db.EmbeddedDocumentField(ArticleMetaData) title = db.StringField() date = db.DateTimeField() body = db.BinaryField() uuid = db.UUIDField()
class User(mongo.Document): name = mongo.StringField(max_length=255) username = mongo.StringField(max_length=64) npm = mongo.StringField(max_length=20) batch = mongo.StringField(max_length=5) major = mongo.ReferenceField("Major") update_schedule_at = mongo.DateTimeField(default=None) completion_id = mongo.UUIDField(default=None) last_update_course_request_at = mongo.DateTimeField(default=None)
class Activty(mongoengine.Document): # Fields _id = mongoengine.UUIDField(binary=False, primary_key=True) user = mongoengine.UUIDField(binary=False) action = mongoengine.StringField(max_length=50) car = mongoengine.EmbeddedDocumentField(ActivitySubject) created_at = mongoengine.DateTimeField(default=datetime.datetime.now) meta = {'indexes': [{'fields': ['user']}]} # Override save method def save(self, *args, **kwargs): # Update id value with unique id self._id = uuid.uuid4() # Proceed save super(Activty, self).save(*args, **kwargs)
class User(mongoengine.Document): # Fields _id = mongoengine.UUIDField(binary=False, primary_key=True) name = mongoengine.StringField() surname = mongoengine.StringField() age = mongoengine.IntField() email = mongoengine.EmailField() password = mongoengine.StringField() username = mongoengine.StringField() avatar = mongoengine.EmbeddedDocumentField(Avatar) role = mongoengine.StringField() created_at = mongoengine.DateTimeField(default=datetime.datetime.now) channels = mongoengine.ListField( mongoengine.UUIDField(binary=False, primary_key=True)) following = mongoengine.ListField( mongoengine.UUIDField(binary=False, primary_key=True)) meta = {'indexes': [{'fields': ['email'], 'unique': True}]} # Fields needed for JWT def get_auth_fields(self): return {'role': self.role, '_id': str(self._id)} # Override save method def save(self, *args, **kwargs): # Update id value with unique id self._id = uuid.uuid4() # Provide default role self.role = 'user' # Encode current password current_password = str.encode(self.password) # Hash password hashed = bcrypt.hashpw(current_password, bcrypt.gensalt(10)) # Update password self.password = hashed.decode("utf-8") # Proceed save super(User, self).save(*args, **kwargs)
class FormData(mongo.Document): id = mongo.UUIDField(required=False) location = mongo.ReferenceField("Location", required=False) user = mongo.ReferenceField("User", required=False) form_schema = mongo.DynamicField(required=False) data = mongo.DynamicField(required=False) created = mongo.DateTimeField(default=datetime.datetime.today().date(), required=False) last_updated = mongo.DateTimeField( default=datetime.datetime.today().date(), required=False)
class Categories(mongo.Document): id = mongo.UUIDField(required=False) name = mongo.StringField(required=True, default='', unique=True) privacy = mongo.StringField(required=True, default='') description = mongo.StringField(required=True, default='') taxonomies = mongo.StringField(required=False, default='') custom_field_form = mongo.ReferenceField("FormSchema", required=False) created = mongo.StringField(required=True, default=str(datetime.now())) created_by = mongo.StringField(required=False) updated = mongo.StringField(required=True, default=str(datetime.now())) tagged = mongo.ListField(mongo.GenericReferenceField(), required=False) meta = {'indexes': [{'fields': ('name', ), 'unique': True}]} def add_object(self, obj): self.tagged.append(obj) self.save() @property def group(self): return "Categories" @property def permission_type(self): return "category" @property def number_using(self): return len(self.tagged) @classmethod def objects_with(cls, name): try: return Categories.objects.get(name=name).tagged except Categories.DoesNotExist: return [] @classmethod def for_object(cls, obj): return Categories.objects(tagged__in=[obj]) @classmethod def for_object_id(cls, objid, cls_name): #kinda an ugly hack, but global should have most everything loaded #if your getting a Keyerror, try importing the cls cls = globals()[cls_name] obj = cls.objects.get(id=objid) return Categories.for_object(obj) @classmethod def remove_obj(cls, obj): for cat in Categories.for_object(obj): Categories.objects(id=str(cat.id)).update_one(pull__tagged=obj)
class Channel(mongoengine.Document): # Channel fields _id = mongoengine.UUIDField(binary=False, primary_key=True) cover = mongoengine.EmbeddedDocumentField(Photo) name = mongoengine.StringField(max_length=60) status = mongoengine.StringField(max_length=60, default="active") summary = mongoengine.StringField(max_length=180) tags = mongoengine.ListField(mongoengine.StringField(max_length=50)) followers = mongoengine.ListField(mongoengine.UUIDField(binary=False)) admins = mongoengine.ListField(mongoengine.UUIDField(binary=False)) created_at = mongoengine.DateTimeField(default=datetime.datetime.now) def save(self, *args, **kwargs): # Update id value with unique id self._id = uuid.uuid4() # Proceed save super(Channel, self).save(*args, **kwargs)
class Comment(mongoengine.Document): # Fields _id = mongoengine.UUIDField(binary=False, primary_key=True) comment = mongoengine.StringField() car = mongoengine.UUIDField(binary=False) video = mongoengine.UUIDField(binary=False) author = mongoengine.EmbeddedDocumentField(CommentAuthor) upvotes = mongoengine.ListField(mongoengine.UUIDField(binary=False)) downvotes = mongoengine.ListField(mongoengine.UUIDField(binary=False)) created_at = mongoengine.DateTimeField(default=datetime.datetime.now) meta = {'indexes': [{'fields': ['video']}]} # Override save method def save(self, *args, **kwargs): # Update id value with unique id self._id = uuid.uuid4() # Proceed save super(Comment, self).save(*args, **kwargs)
class Car(mongoengine.Document): # Fields _id = mongoengine.UUIDField(binary=False, primary_key=True) title = mongoengine.StringField() summary = mongoengine.StringField() features = mongoengine.ListField( mongoengine.EmbeddedDocumentField(CarFeature)) model = mongoengine.StringField() tags = mongoengine.ListField(mongoengine.StringField()) photos = mongoengine.ListField(mongoengine.EmbeddedDocumentField(Photo)) user = mongoengine.UUIDField(binary=False) created_at = mongoengine.DateTimeField(default=datetime.datetime.now) meta = {'indexes': [{'fields': ['user']}]} # Return car data def get_details(self): data = { "_id": self.model, "title": self.title, "model": self.model, "tags": self.tags, "photos": self.photos, "created_at": self.created_at } return data # Override save method def save(self, *args, **kwargs): # Update id value with unique id self._id = uuid.uuid4() # Proceed save super(Car, self).save(*args, **kwargs)
class Video(mongoengine.Document): # Video filds _id = mongoengine.UUIDField(binary=False, primary_key=True) name = mongoengine.StringField(max_length=60) path = mongoengine.StringField() filename = mongoengine.StringField() summary = mongoengine.StringField(max_length=190) tags = mongoengine.ListField(mongoengine.StringField(max_length=50)) views = mongoengine.IntField(default=0) thumbnail = mongoengine.EmbeddedDocumentField(Photo) channel = mongoengine.UUIDField(binary=False) upvotes = mongoengine.ListField(mongoengine.UUIDField(binary=False)) downvotes = mongoengine.ListField(mongoengine.UUIDField(binary=False)) created_at = mongoengine.DateTimeField(default=datetime.datetime.now) status = mongoengine.StringField(max_length=60, default="active") def save(self, *args, **kwargs): # Update id value with unique id self._id = uuid.uuid4() # Proceed save super(Video, self).save(*args, **kwargs)
class Image(db.EmbeddedDocument): img_id = db.UUIDField(db_field="audio", unqiue=True, default=uuid.uuid4()) filename = db.StringField(required=True) date_upload = db.DateTimeField(default=datetime.utcnow) payload = db.ImageField(required=True, thumbnail_size=(100, 100, False)) def json(self): user_dict = { "image_id": self.img_id, "filename": self.filename, "date_uploaded": self.date_upload, "payload": self.payload, } return json.dumps(user_dict) meta = {"indexes": [], "ordering": ["date_uploaded"]}
class SharedSongs(me.Document): record_id = me.UUIDField(default=uuid.uuid4(), required=False) title = me.StringField(required=False) rank = me.IntField(required=False) song_id = me.StringField(required=True) artist = me.StringField(required=False) cover = me.StringField(required=False) album = me.StringField(required=True) playlist_id = me.StringField(required=False) added_to_playlist = me.StringField(required=True) added_by = me.StringField(required=False) created_at = me.DateTimeField(default=datetime.utcnow) api = me.StringField(required=False) link = me.StringField(required=False, default="") is_shared = me.BooleanField(default=False) @staticmethod def fetch_playlist() -> Optional[List[Share]]: get_playlists: List[Playlists] = Playlists.objects(is_active=True) songs: List[Share] = [] for playlist in get_playlists: interface = get_interface(playlist.api) if interface: client = interface client.fetch(playlist_id=playlist.playlist_id) latest: Share = client.get_latest if latest: latest.added_by = playlist.user is_exists = SharedSongs.objects(song_id=latest.song_id, api=latest.api).count() if not is_exists: create = SharedSongs(**asdict(latest)) create.is_shared = True create.save() songs.append(latest) else: raise Exception("Playlist not exist") return songs @staticmethod def get_songs_to_play() -> List[str]: share: List[Share] = SharedSongs.objects().order_by( settings.SHARE_DATE_SORTED)[:settings.QUEUE_LIMIT] songs = [] for song in share: songs.append(f"{song.artist} {song.title}") return songs
class Audio(db.EmbeddedDocument): audio_id = db.UUIDField(db_field="audio", unqiue=True, default=uuid.uuid4()) filename = db.StringField(required=True) date_upload = db.DateTimeField(default=datetime.utcnow) payload = db.FileField(required=True) def json(self): user_dict = { "audio_id": self.audio_id, "filename": self.filename, "date_uploaded": self.date_upload, "payload": self.payload, } return json.dumps(user_dict) meta = {"indexes": [], "ordering": ["date_uploaded"]}
class Leaf(BaseJinxObject, mongoengine.Document): """ Leaf base class schema for MongoDB engine """ _name = 'leaf' meta = {'collection': 'leaf'} INTERFACE_STRING = "leaf" # Required fields stalk_uuid = mongoengine.UUIDField(required=True, dispName="Stalk UUID", visible=False) format = mongoengine.StringField(required=True, dispName="Format") # Optional fields resolution = mongoengine.ListField(dispName="Resolution") framerange = mongoengine.ListField(dispName="Frame Range") thumbnail = mongoengine.StringField(dispName="Thumbnail", icon=icon_paths.ICON_IMAGE_SML) def parent(self): super(Leaf, self).parent() db = mongorm.getHandler() filt = mongorm.getFilter() filt.search(db['stalk'], uuid=self.stalk_uuid) stalk = db['stalk'].one(filt) return stalk def siblings(self, includeSelf=True): db = mongorm.getHandler() filt = mongorm.getFilter() filt.search(db['leaf'], stalk_uuid=self.stalk_uuid) siblings = db['leaf'].all(filt) if not siblings.size() < 2: if includeSelf: return siblings else: siblings.remove_object(self) return siblings else: return None
class FormSchema(mongo.Document): id = mongo.UUIDField(required=False) field_groups = mongo.DynamicField(required=False) created = mongo.DateTimeField(default=datetime.datetime.today().date(), required=False) last_updated = mongo.DateTimeField( default=datetime.datetime.today().date(), required=False) @property def permission_type(self): return "forms" def convert_to_json(self): return { 'id': str(self.id), 'field_groups': self.field_groups, 'created': self.created, 'last_updated': self.last_updated }
class School(gj.Document): id = db.UUIDField(primary_key=True, default=uuid.uuid4) code_inep = db.StringField(required=True, max_length=10, unique=True) address = db.EmbeddedDocumentField(Address) nameSchool = db.StringField(required=True, unique=True, max_length=200) admDependency = db.StringField(required=True, max_length=100) locate = db.StringField(required=True, max_length=100) create_at = db.DateTimeField(default=datetime.now) update_at = db.DateTimeField(default=datetime.now) meta = { 'indexes': [ { 'fields': ['+nameSchool'] }, { 'fields': ['#locate'] }] } def __str__(self): return self.nameSchool
class Order(mongoengine.Document): # Fields _id = mongoengine.UUIDField(binary=False, primary_key=True) user = mongoengine.EmbeddedDocumentField(OrderAuthor) subject = mongoengine.EmbeddedDocumentField(OrderSubject) total = mongoengine.IntField() start = mongoengine.StringField() end = mongoengine.StringField() created_at = mongoengine.DateTimeField(default=datetime.datetime.now) meta = {'indexes': [{'fields': ['user.author']}]} # Override save method def save(self, *args, **kwargs): # Update id value with unique id self._id = uuid.uuid4() # Proceed save super(Order, self).save(*args, **kwargs)
class Credential(db.EmbeddedDocument): cred_id = db.UUIDField(db_field="cred_id", unqiue=True, default=uuid.uuid4()) cred_name = db.StringField(required=True) username = db.StringField(required=True) password = db.StringField(required=True) website = db.StringField(required=True) date_uploaded = db.DateTimeField(default=datetime.utcnow) def json(self): user_dict = { "cred_id": self.cred_id, "cred_name": self.cred_name, "username": self.username, "password": self.password, "website": self.website, "date_upload": self.date_uploaded } return json.dumps(user_dict) meta = {"indexes": [], "ordering": ["date_uploaded"]}
class Customer(mongoengine.Document): uuid = mongoengine.UUIDField(binary=False) name = mongoengine.StringField() email = mongoengine.EmailField(unique=True) products = mongoengine.ListField( mongoengine.EmbeddedDocumentField(Product)) def api_json(self): return json.loads(self.to_json()) def check_product(self, uuid): return uuid in [str(i.uuid) for i in self.products] @classmethod def get_customer(self, key): try: uuid.UUID(key) params = {'uuid': key} except ValueError: params = {'email': key} return Customer.objects(**params).first()
class CrashReportToken(me.EmbeddedDocument): """ Represents a token in a crash report. See :class:`sni.uac.models.Token` """ created_on = me.DateTimeField() """See :class:`sni.uac.models.Token`""" expires_on = me.DateTimeField() """See :class:`sni.uac.models.Token`""" owner = me.ReferenceField(User) """See :class:`sni.uac.models.Token`""" token_type = me.StringField() """See :class:`sni.uac.models.Token`""" uuid = me.UUIDField() """See :class:`sni.uac.models.Token`""" def to_dict(self) -> dict: """ Returns a dict representation """ return { "created_on": str(self.created_on), "expires_on": str(self.expires_on), "owner": { "authorized_to_login": self.owner.authorized_to_login, "character_id": self.owner.character_id, "character_name": self.owner.character_name, "clearance_level": self.owner.clearance_level, "created_on": str(self.owner.created_on), "updated_on": str(self.owner.updated_on), }, "token_type": self.token_type, "uuid": str(self.uuid), }
class Job(me.DynamicDocument): jobid = me.UUIDField(binary=False, required=True) name = me.StringField(max_length=128, default="Unknown") failed = me.BooleanField(default=False) reason = me.StringField(max_length=512) version = me.StringField(max_length=10, default=baleen.get_version) started = me.DateTimeField(default=datetime.now, required=True) finished = me.DateTimeField(default=None) updated = me.DateTimeField(default=datetime.now, required=True) errors = me.MapField(field=me.IntField()) counts = me.MapField(field=me.IntField()) totals = me.MapField(field=me.IntField()) @classmethod def pre_save(cls, sender, document, **kwargs): document.updated = datetime.now() meta = { 'collection': 'jobs', } def duration(self, humanize=False): """ Returns the timedelta of the duration. """ finished = self.finished or datetime.now() delta = finished - self.started if humanize: return humanizedelta(days=delta.days, seconds=delta.seconds, microseconds=delta.microseconds) return delta def __unicode__(self): return "{} Job {}".format(self.name, self.jobid)
class DataVisualization(mongo.Document): id = mongo.UUIDField(required=False) group_name = mongo.StringField(required=True, unique=True) selected_external_ds = mongo.ListField( mongo.EmbeddedDocumentField(DataSet), required=False) selected_internal_ds = mongo.ListField( mongo.EmbeddedDocumentField(DataSet), required=False) widgets = mongo.ListField(mongo.EmbeddedDocumentField(Widget)) created = mongo.DateTimeField(required=False, default=datetime.now()) updated = mongo.DateTimeField(required=False, default=datetime.now()) short_url = mongo.StringField(required=False) hidden = mongo.BooleanField(default=False) @property def num_datasets(self): return len(self.selected_external_ds) + len(self.selected_internal_ds) @property def visualization_types(self): return list(set([w.viz_type for w in self.widgets])) @property def visualizations(self): return len(self.widgets)