def update(post: Post, post_dict: dict): if 'date' in post_dict.keys(): post.published_at = datetime.strptime(post_dict['date'], "%Y-%m-%d") if 'html_url' in post_dict.keys(): post.html_url = post_dict['html_url'] if 'category' in post_dict.keys(): try: category = Category.objects.get(name=post_dict['category']) except Category.DoesNotExist: category = Category(name=post_dict['category']) category.save() post.category = category if 'preface' in post_dict.keys(): post.preface = post_dict['preface'] if 'thumbnail' in post_dict.keys(): post.thumbnail = post_dict['thumbnail'] post.body = post_dict['body'] return post
c1 = Committee("Health Council") c2 = Committee("Dining Council") c3 = Committee("Education Council") print "Adding new Category" categories = [Category("Healthcare"),Category("Education"),Category("Tuition"),Category("Dining")] #categories[0].committee = c1 c1.categories.append(categories[0]) #categories[1].committee = c3 c2.categories.append(categories[3]) c3.categories.append(categories[1]) c3.categories.append(categories[2]) #categories[2].committee = c3 #categories[3].committee = c2 print 'Adding new post' post1 = Post('Terrible Dining', 'There is food poisoning epidemic because of our cafeteria food') post1.category = categories[3] post1.approved = True post1.status = 'success' post2 = Post('Need better health center', 'School needs better health services') post2.category = categories[0] post2.approved = True post2.status = 'success' post3 = Post('Tuition is too high', 'NYU needs to lower tuition') post3.category = categories[2] post3.approved = True post3.status = 'success' post4 = Post('Classes are too hard', 'Professors need to make classes easier') post4.category = categories[1] post5 = Post('Tuition is too cheap', 'Tuition should be higher') post5.category = categories[2]
def make_post(bsObj): # Iterate over the articles on the current page for each in bsObj: # get title from app import db from app.models import Post title = each.find('div', {'class': 'content'}).h4.get_text() # get summary posted = Post.query.filter_by(title=title).first() site = 'http://120.24.245.101/info/view/container/index/' detail_url = site + each.find('h4').a.attrs['href'] # load the detail page, make page soup detail_page = load_url(detail_url) detail_soup = make_detail_soup(detail_page) if posted: # update its category posted.category = detail_soup.find('span', {'id':'local'}).get_text() db.session.add(posted) db.session.commit() else: summary = each.p.get_text() # get keywords temp = [] for a in each.find('div', {'class': 'keywords'}): temp.append(a.get_text()) # keywords should be returned as a string delimited by , # i.e. 'pollution, ep' keywords = ','.join(temp) # get external links for imgs # add the upload feature for imgs in the future try: href = each.find('img').attrs['src'] except: href = '' # how to deal with articles that did not upload imgs # get timestamp timestamp = detail_soup.find('span', {'class':'article_time'}).get_text() # 2015-07-08 14:10:40 # convert it to a datetime object, 14:10 # get body_html, preserve the tags body_html = detail_soup.find('div', {'class':'article_detail'}) body_html = str(body_html) # get source source = detail_soup.find('span', {'class':'article_source'}).get_text() category = detail_soup.find('span', {'id':'local'}).get_text() #update model, database # (self, title, summary, source, timestamp, keywords, img_href, body_html) post = Post(title, summary, source, timestamp, keywords, href, body_html) post.category = category db.session.add(post) db.session.commit()