class Specialty(mongoengine.Document): id = fields.SequenceField(primary_key=True, sequence_name='specialty') name = fields.StringField(required=True, max_length=50) def to_dict(self): return { 'id': str(self.id), 'name': self.name, } @classmethod def get_all(cls): result = [] for specialty in cls.objects: result.append(specialty.to_dict()) return result @classmethod def find_by_id(cls, specialty_id): oid = bson.objectid.ObjectId(specialty_id) try: return cls.objects.get(id=oid) except errors.DoesNotExist: return None
class Appointment(mongoengine.Document): id = fields.SequenceField(primary_key=True, sequence_name='appointment') professional = fields.ReferenceField(Professional, required=True) patient = fields.ReferenceField(Patient, required=True) start_date = fields.StringField(required=True) end_date = fields.StringField(required=True) def to_dict(self, professional_info=False, patient_info=False): return { 'id': str(self.id), 'professional': self.professional.to_dict(), 'patient_id': str(self.patient.id), 'start_date': self.start_date, 'end_date': self.end_date, } @classmethod def find_by_professional(cls, professional): try: return cls.objects(professional=professional) except errors.DoesNotExist: return None @classmethod def find_by_patient(cls, patient): try: return cls.objects(patient=patient) except errors.DoesNotExist: return None
class User(mongoengine.Document): meta = {'allow_inheritance': True} id = fields.SequenceField(primary_key=True, sequence_name='user') username = fields.StringField(required=True, max_length=20) email = fields.StringField(required=True) fone = fields.StringField() first_name = fields.StringField(required=True, max_length=50) last_name = fields.StringField(required=True, max_length=50) birthdate = fields.StringField() image_url = fields.StringField() #look at the DateTimeField Documentation later gender = fields.StringField(required=True, max_length=6) def to_dict(self): return { 'id': str(self.id), 'username': self.username, 'email': self.email, 'fone': self.fone, 'first_name': self.first_name, 'last_name': self.last_name, 'birthdate': self.birthdate, 'gender': self.gender, 'image_url': self.image_url if self.image_url is not None else None, } def get_role(self): raise NotImplementedError() @classmethod def find_by_id(cls, user_id): oid = bson.objectid.ObjectId(user_id) try: return cls.objects.get(id=oid) except errors.DoesNotExist: return None @classmethod def find_by_username(cls, username): try: return cls.objects.get(username=username) except errors.DoesNotExist: return None @classmethod def find_by_email(cls, email): try: return cls.objects.get(email=email) except errors.DoesNotExist: return None
class TodoListModel(BaseDocument): meta = {"collection": "todo_list"} no = fields.SequenceField(collection_name="todo_list") title = fields.StringField(required=True) status = fields.DictField(required=True) category = fields.DictField(required=True) due_date = fields.StringField() due_time = fields.StringField() description = fields.StringField() star = fields.BooleanField(default=False) hash_tags = fields.ListField(fields.StringField()) @queryset_manager def withStar(doc_cls, queryset): return queryset.filter(star=True)
class AutoFieldModel(Document): auto_field = fields.SequenceField(primary_key=True)