class Hacker(db.Document): first_name = db.StringField() last_name = db.StringField() username = db.StringField(unique=True) password = db.StringField() email = db.EmailField(unique=True) phone_number = db.StringField(validation=validate_phone_number) profile = db.EmbeddedDocumentField(HackerProfile) resume = db.URLField() emergency_contact = db.EmbeddedDocumentField(HackerEmergencyContact) accepted_forms = db.BooleanField() active_participant = db.BooleanField() def to_json(self): return { "first_name": self.first_name, "last_name": self.last_name, "username": self.username, "password": self.password, "email": self.email, "phone_number": self.phone_number, "profile": self.profile, "resume": self.resume, "emergency_contact": self.emergency_contact, "accepted_forms": self.accepted_forms, "active_participant": self.active_participant }
class Ask(db.document): createdAt = db.DateTimeField(default=datetime.utcnow) updatedAt = db.DateTimeField(default=datetime.utcnow) question = db.EmbeddedDocumentField(Question) privacy = db.StringField() answers = db.ListField(db.EmbeddedDocumentField(Answers)) askedBy = db.RefrenceField(User)
class Resource(db.Document): rid = db.IntField(unique=True) title = db.StringField() link = db.StringField() tags = db.ListField(db.EmbeddedDocumentField('Tags')) description = db.StringField() def __unicode__(self): return str(self.rid) def get_dict(self): return { 'rid': self.rid, 'title': self.title, 'link': self.link, 'tags': self.tags, 'description': self.description } def __repr__(self): return 'rid ' + str(self.rid) def save(self, *args, **kwargs): if self.rid == None: try: self.rid = self.__class__.objects.order_by('-rid')[0].rid + 1 except IndexError: self.rid = Resource.objects.count() + 1 super(Resource, self).save(*args, **kwargs)
class OlympiadCategory(db.Document): created_at = db.DateTimeField(default=now(), required=True) name = db.StringField(max_length=255, required=True) abbreviation = db.StringField(max_length=10) url = db.URLField() events = db.ListField(db.EmbeddedDocumentField(Olympiad)) logo = None
class User(db.Document, UserMixin): ''' A class that represents users who will use this system, their subscriptions and is used to manage their sessions ''' name = db.StringField(required=True) email = db.EmailField(required=True, unique=True) password_hash = db.StringField(max_length=160) subscriptions = db.ListField(db.EmbeddedDocumentField(Subscription))
class Card(db.Document): title = db.StringField(required=True) content = db.ListField(db.EmbeddedDocumentField(Comment)) start_date = db.DateTimeField() end_date = db.DateTimeField() date_created = db.DateTimeField(default=datetime.utcnow) date_completed = db.DateTimeField() owner = db.ReferenceField(User) assigned = db.ReferenceField(User) # User.register_delete_rule(Board, 'owner', db.CASCADE) # User.register_delete_rule(Board, 'visible', db.CASCADE)
class User(db.Document): firstName = db.StringField(default=None) lastName = db.StringField(default=None) username = db.StringField(unique=True) email = db.EmailField(unique=True) password = db.StringField() phone = db.StringField(max_length=12) gender = db.StringField(max_length=10) branch = db.StringField(max_length=10) year = db.StringField(max_length=10) newUser = db.BooleanField(default=True) about = db.EmbeddedDocumentField(About, default=About) settings = db.EmbeddedDocumentField(Settings, default=Settings) # connections = db.ListField(db.ReferenceField(User)) image_file = db.ImageField() cover_img = db.StringField() createdAt = db.DateTimeField( default=datetime.utcnow) # utc to keep it universal updatedAt = db.DateTimeField(default=datetime.utcnow) def __repr__(self): return f"User('{self.username}','{self.email}')"
class Article(db.Document): '''A document that represents an article extracted from a feed''' source_url = db.URLField(verify_exists=True, unique=True) feed_id = db.ObjectIdField() features = db.EmbeddedDocumentField('Features') readers = db.ListField(db.EmbeddedDocumentField('Reader')) interested_users = db.ListField(db.ObjectIdField()) uninterested_users = db.ListField(db.ObjectIdField()) time_stamp = db.DateTimeField(default=datetime.datetime.now()) def get_words_in_article(self): ''' Arguments : URL Function: Gets the article only version of the URL using Instapaper. Extracts the text in the artcile and removes any non AlphaNumeric characters in the text Returns a list of words in the article present in the URL.''' #get article content from instapaper html_data = BeautifulSoup( urllib.urlopen("http://www.instapaper.com/m?%s" % urllib.urlencode({'u': self.source_url})).read()) html_data = html_data.find("body") content = html_data.findAll( text=True ) #setting text to True to extract only the text in the <body> word_list = [] for word in content[ 30:]: #Removing redundant content from Instapaper Mobilizer headers for w in word.split( " "): #splitting on spcae for multiword strings wd = (multiword_string_pattern.sub('', w.lower()) ) #substituing non alphanumeric characters with '' if len(wd) > 1: word_list.append( wd) #exclude strings of less than 2 characters filtered_words = [ w for w in word_list if not w in nltk.corpus.stopwords.words('english') ] return dict((word, True) for word in word_list) def get_words_in_title(self): "Get words in the title of an article" word_list = [] for w in self.features.title.split(" "): wd = (multiword_string_pattern.sub('', w.lower())) if len(wd) > 1: word_list.append(wd) filtered_words = [ w for w in word_list if w not in nltk.corpus.stopwords.words('english') ] return dict((word, True) for word in filtered_words) def get_score(self, classifier_object): "Use the trained classifier to find the interest for the new article" if classifier_object is None: return 0.5 classifier = pickle.loads(str(classifier_object)) if classifier.classify(self.get_words_in_title()) is True: return 1 else: return 0 def get_readers_from(self): ''' creates a list of reader objects for an article from a list of feed subscribers ''' subscribers = [] feed_subscribers = User.objects(subscriptions__feed_id=self.feed_id) for feed_subscriber in feed_subscribers: classifier_object = None for subscription in feed_subscriber.subscriptions: if subscription.feed_id == self.feed_id: classifier_object = subscription.classifier_object new_reader = Reader(\ user_id = feed_subscriber.id \ , score = self.get_score(classifier_object) ) #Set the scores for each user who has not yet read the article subscribers.append(new_reader) return subscribers def get_article_snippet(self, article, max_length=128): ''' Returns the article snippet to be show next to the article title. ''' #Make sure char_length a int parameter. if (type(max_length) is not int): max_length = 128 html_data = BeautifulSoup(article) #Join the words from the html content. words = ''.join(html_data.findAll(text=True)) if len(article) < max_length: return words + '...' else: return words[:max_length] + '...'