def delete(self, id): post = Post.get_by_id(id) if post is None: return render_view(url_for('PostsView:index'), status=False, redirect=True, message=_('POST_NOT_FOUND')) if not post.can_edit(): abort(401) try: title = post.title Post.delete(post.id) Feed.clear_feed_cache() ret = request.values.get('return') flash(_('POST_DELETE_SUCESS', title=title)) if ret: return render_view(ret, redirect=True) except Exception as e: flash(_('ERROR_POST_DELETE_FAILED', error=e), 'error') return render_view(url_for('PostsView:index'), redirect=True)
def feeds_edit(feed_id): form = AddFeedForm() if form.validate_on_submit(): Feed.edit_feed(feed_id, form.data["name"], form.data["url"]) flash("Feed has been changed", "success") return redirect(url_for("feeds_index")) feed_data = Feed.get_feed_by_id(feed_id) if not feed_data: return redirect(url_for("feeds_index")) feed_data = feed_data[0] return dict(form=form, feed_data=feed_data)
def feeds_download(feed_id): if feed_id is None or feed_id == "all": feeds = Feed.get_all_feeds() print(feeds) else: feeds = Feed.get_feed_by_id(feed_id) articles_list = [] for feed in feeds: articles = download_articles(feed.url, feed.id) articles_list += articles if feed_id != "all": return dict(articles_list=articles_list) else: return "".join(articles_list)
def update(): content = request.get_json(silent=False) all_feeds = content['all_feeds'] for feed in all_feeds: feed_name = feed['feed_name'] samples = feed['samples'] feed = Feed.query.filter_by(name=feed_name).first() if feed is None: feed = Feed(name=feed_name) db.session.add(feed) db.session.commit() for sample in samples: new_sample = Sample( feed_id=feed.id, value=sample['value'], time=datetime.fromtimestamp( sample['time']).replace(tzinfo=timezone.utc).astimezone( tz=local_tz).replace(tzinfo=None)) db.session.add(new_sample) db.session.commit() if len(Sample.query.all()) >= 9900: Thread(target=clean_history).start() return 'update success'
def opentaba_feed(): url = request.args.get('link','') relevantfeeds = relevant_feeds_urls() if url not in relevantfeeds: title = set_title_by_feed(url) title = title[1].split(" ") try: city = request.args.get('city','') title.insert(2," "+city+" ") except: city=" " name = " ".join(title) a_new_feed = Feed( user_id=session['user_id'], url = request.args.get('link',''), name=name, project='תב"ע פתוחה '+city, ) db.session.add(a_new_feed) db.session.commit() flash(u'ההזנה החדשה נוספה למאגר') return redirect(url_for('notifier.feeds_editor')) else: flash(u'את/ה כבר עוקבים אחרי מקור מידע זה') return redirect(url_for('notifier.feeds_editor'))
def post(self): form = PostForm() if form.validate_on_submit(): try: if not form.validate(): raise Exception(_('ERROR_INVALID_SUBMISSION')) remain = request.values.get('remain', False, bool) post = Post.create() form.populate_obj(post) post.user = current_user f = request.files.get('file') if f: picture = Picture.create() picture.save_file(f, current_user) post.cover_picture_id = picture.id if picture else 0 # init the score post.update_score(page_view=1) post.editor_version = 1 post.save() Feed.clear_feed_cache() if post.is_draft: message = _('POST_DRAFT_SAVE_SUCESS') else: message = _('POST_PUBLIC_SAVE_SUCESS') if remain: url = url_for('PostsView:put', id=post.id, remain='y') else: url = url_for('PostsView:get', id=post.id) return render_view(url, redirect=True, message=message) except Exception as e: flash(e.message, 'error') return render_view('admin/posts/edit.html', form=form)
def make_object(self, data): try: print data['url'] u = urlopen(data['url']) except: raise URLOpenError("Can't access Url") parser = FeedParser() if not parser.parse(u): abort(400) feed = Feed(**parser.feed) feed.user_id = g.current_user.id for item in parser.items: new_item = Item(**item) new_item.feed_title = feed.title feed.items.append(new_item) g.current_user.feeds.append(feed) db.session.add(g.current_user) db.session.commit() return feed
def add_feed(): form = AddFeedForm() if form.validate_on_submit(): feed = Feed(name=form.name.data, link=form.link.data, article_1=form.article_1.data, article_2=form.article_2.data) db.session.add(feed) db.session.commit() flash('Feed Created for {}'.format(form.name.data)) return redirect(url_for('index')) return render_template('add_feed.html', title='Add Feed', form=form)
def stamps_category(category, page=1, limit=100): lang = str(get_locale()) name = '%s-%s' % (Feed.CACHE_CATEGORY_PAGE, category.id) obj = Feed.get_feed_cache(name=name, page=page, limit=limit, lang=lang) if obj is None: posts, total = Feed.category(category, page=page, limit=limit) obj = render_template('main/stamp/partials/_category.html', category=category, posts=posts, page=page, limit=limit, total=total) Feed.set_feed_cache(name=Feed.CACHE_CATEGORY_PAGE, data=obj, page=page, limit=limit, lang=lang) return obj
def backup(): ''' Run backup tasks. ''' from config import basedir data = raw_input(u'Enter data identifier (e.g.: backup or 20160422): ') datadir = os.path.join(basedir, 'data', data) if not os.path.exists(datadir): os.makedirs(datadir) from app.models import User User.backup_entries(data=data, basedir=basedir) from app.models import UserCreation UserCreation.backup_entries(data=data, basedir=basedir) from app.models import Punch Punch.backup_entries(data=data, basedir=basedir) from app.models import Feed Feed.backup_entries(data=data, basedir=basedir)
def count_page_view(post_id): from app.models import Feed import datetime id = Post.decode_id(post_id) post = Post.get_by_id(id) try: key = u'counter_post_%s' % id count_time = float(session[key]) if key in session else 0 # Increase pageviews in 1 hour if Feed.epoch_seconds(datetime.datetime.now()) > count_time: Post.begin_transaction() post.update_score(page_view=1) post.save() Post.commit_transaction() session[key] = Feed.epoch_seconds(datetime.datetime.now() + datetime.timedelta(hours=8)) except Exception as e: Post.rollback_transaction() raise e return send_file('static/images/counter.gif', mimetype='image/gif')
def count_page_view(post_id): id = Post.decode_id(post_id) post = Post.get_by_id(id) try: key = u'counter_post_%s' % id count_time = float(session[key]) if key in session else 0 # Increase pageviews in 1 hour if Feed.epoch_seconds(datetime.datetime.now()) > count_time: Post.begin_transaction() post.update_score(page_view=1) post.save() Post.commit_transaction() session[key] = Feed.epoch_seconds(datetime.datetime.now() + datetime.timedelta(hours=8)) except Exception as e: Post.rollback_transaction() raise e return send_file(config.BASE_DIR + '/static/images/counter.gif', mimetype='image/gif')
def new_feed(): """ Add a new feed """ form = NewFeedForm() if form.validate_on_submit(): url = form.podcast_url.data if Feed.get_by_url(url) is not None: flash('This podcast already exists') else: flash('Adding podcast : ' + url) get_feed.delay(url, and_subscribe=current_user.id) return redirect(url_for('home')) return render_template('new_feed.html', title='New feed', form=form)
def save_feed_to_db(url, name, project, user_id, relevant_feeds): if url not in relevant_feeds: title = set_title_by_feed(url) try: title = title[1].split(" ") except: pass a_new_feed = Feed(url=url, name=name, user_id=user_id, project=project) db.session.add(a_new_feed) db.session.commit() flash(u'ההזנה החדשה נוספה למאגר') return redirect(url_for('notifier.feeds_editor')) else: flash(u'את/ה כבר עוקבים אחרי מקור מידע זה') return redirect(url_for('notifier.feeds_editor'))
def validate(self): if not Form.validate(self): return False feed = dboper.get_feed_by_rsslink(self.feedurl.data) #Search in the central repository if feed: if dboper.user_feed_added(g.user, feed): #Search the user feeds self.feedurl.errors.append('You are already registered to this feed') return False if not Feed.is_valid_rss(self.feedurl.data): self.feedurl.errors.append('This is not a valid rss feed') return False return True
def stamps_welcome(page=1, limit=Feed.FEED_DEFAULT_LIMIT): lang = str(get_locale()) obj = Feed.get_feed_cache(name=Feed.CACHE_WELCOME_PAGE, page=page, limit=limit, lang=lang) if obj is None: posts, total = Feed.posts(page=page, limit=Feed.FEED_DEFAULT_LIMIT) obj = render_template('main/stamp/partials/_index.html', posts=posts, page=page, limit=Feed.FEED_DEFAULT_LIMIT, total=total) Feed.set_feed_cache(name=Feed.CACHE_WELCOME_PAGE, data=obj, page=page, limit=limit, lang=lang) return obj
def stamps_ranking(page=1, limit=5): lang = str(get_locale()) obj = Feed.get_feed_cache(name=Feed.CACHE_RANKING_PAGE, page=page, limit=limit, lang=lang) if obj is None: posts, total = Feed.ranking(page=page, limit=limit) obj = render_template('main/stamp/partials/_ranking.html', posts=posts, page=page, limit=limit, total=total) Feed.set_feed_cache(name=Feed.CACHE_RANKING_PAGE, data=obj, page=page, limit=limit, lang=lang) return obj
def stamps_ranking(page=1, limit=5): key = 'stamps/ranking.v1.%s.%s' % (page, limit) obj = cache.get(key) if obj is None: posts, total = Feed.ranking(page=page, limit=limit) obj = render_template('main/stamp/partials/_ranking.html', posts=posts, page=page, limit=limit, total=total) cache.set(key, obj, 3600) return obj
def stamps_welcome(page=1, limit=20): key = 'stamps/welcome.v1.%s.%s' % (page, limit) obj = cache.get(key) if obj is None: posts, total = Feed.posts(page=page, limit=limit) obj = render_template('main/stamp/partials/_index.html', posts=posts, page=page, limit=limit, total=total) cache.set(key, obj, 3600) return obj
def update_score(self, page_view=0, vote=0, down_vote=0): from app.models import Feed scale = 10 if page_view > 0: self.page_views = self.page_views + page_view if vote > 0: self.votes = self.votes + vote if down_vote > 0: self.down_votes = self.down_votes + down_vote self.score = Feed.score(page_views=self.page_views, ups=self.votes, downs=self.down_votes, date=self.created_at)
def stamps_category(category, page=1, limit=20): key = 'stamps/category.v1.%s.%s.%s' % (category.id, page, limit) obj = cache.get(key) if obj is None: posts, total = Feed.category(category, page=page, limit=limit) obj = render_template('main/stamp/partials/_category.html', category=category, posts=posts, page=page, limit=limit, total=total) cache.set(key, obj, 3600) return obj
def dohandler(self, eventModel): # 评论评论不算做新鲜事 if eventModel.entityType == EntityType.COMMENT: return followerkey = 'follower:1:' + str(eventModel.actorId) feed = Feed(eventModel.eventType, eventModel.actorId, json.dumps(eventModel.dict)) db.session.add(feed) db.session.flush() db.session.commit() for userid in conn.zrange(followerkey, 0, sys.maxsize, desc=True, withscores=False, score_cast_func=float): userid = str(userid, encoding="utf-8") userid = int(userid) user = User.query.filter_by(id=userid).first() feedline = 'feedline:' + str(user.id) conn.zadd(feedline, {feed.id: time.time()})
def add(): """Page to enable a user to add a field to his/her profile""" if request.method == "POST": # check url field is populated if not request.form.get("feed_url"): flash("Field cannot be left blank", "error") return redirect(url_for("add")) # check url field is not more than 200 chars if len(request.form.get("feed_url")) > 200: flash( "Sorry, we cannot add urls longer than 200 characters in length", "error") return redirect(url_for("add")) # check url field is actually a legit rss. if not is_rss_page(request.form.get("feed_url")): flash("Sorry, this doesn't seem to be a rss page!", "error") return redirect(url_for("add")) # get feeds title url = request.form.get("feed_url") feed_name = get_rss_title(url) # see if feed already exists. f = Feed.query.filter_by(feed_url=url, user_id=session['user_id']).count() if f > 0: flash("You are already subscribed to this feed.", "error") return redirect(url_for("add")) f = Feed(feed_name, url, session["user_id"]) db.session.add(f) db.session.commit() flash("Rss feed added successfully", "success") return redirect(url_for("feeds")) return render_template("add.html")
def new_feed(): error = None form = AddFeedForm(request.form) try: project = get_project_by_feed_url(url) except: project ='' if request.method == 'POST': url = form.url.data name = " ".join(set_title_by_feed(url)) if project==u'תב"ע פתוחה': return redirect(url_for('notifier.opentaba_feed', link= url)) elif project==u'כיכר המדינה': return redirect(url_for('notifier.kikar_feed', link= url)) elif form.validate_on_submit(): a_new_feed = Feed( user_id=session['user_id'], name=name, url=url, project=project, ) db.session.add(a_new_feed) db.session.commit() flash(u'ההזנה החדשה נוספה למאגר') return redirect(url_for('notifier.feeds_editor')) elif request.method == 'GET': if project == u'תב"ע פתוחה': return redirect(url_for('notifier.opentaba_feed',link=request.args.get('link'))) elif project == u'כיכר המדינה': return redirect(url_for('notifier.kikar_feed',link=request.args.get('link'), type=request.args.get('type'))) user_email = session['user_email'] return redirect(url_for('notifier.feeds_editor'))
def get_feed(url, and_subscribe=None): """ - download a RSS url - make a Feed entry for it - populate corresponding Episodes kwargs and_subscribe: if not None, the specified userid gets subscribed to it. """ feedp = feedparser.parse(url) feed = Feed(url, feedp) db.session.add(feed) db.session.flush() for entry in feedp['entries']: title = entry['title'] enclosure = find_enclosure(entry) episode = Episode(feed.id, title, enclosure) db.session.add(episode) if and_subscribe is not None: userid = and_subscribe sub = Subscription(feed.id, userid) db.session.add(sub) db.session.commit()
def feeds_articles(feed_id): if feed_id is None: articles = Article.get_last_articles() else: articles = Article.get_articles_by_feed_id(feed_id) return dict(articles=articles, feeds=Feed.get_all_feeds())
def test_feed_edit_3(): with pytest.raises(Exception) as error_info: Feed.edit_feed(5, "abc", "http://localhost") assert error_info
def deploy(): ''' Run deployment tasks. ''' # migrate database to latest revision from flask_migrate import upgrade upgrade() # insert data from app.models import Color Color.insert_entries() from app.models import Permission Permission.insert_entries() from app.models import Role Role.insert_entries() from app.models import IDType IDType.insert_entries() from app.models import Gender Gender.insert_entries() from app.models import Relationship Relationship.insert_entries() from app.models import PurposeType PurposeType.insert_entries() from app.models import ReferrerType ReferrerType.insert_entries() from app.models import BookingState BookingState.insert_entries() from app.models import AssignmentScoreGrade AssignmentScoreGrade.insert_entries() from app.models import GREAWScore GREAWScore.insert_entries() from app.models import ScoreLabel ScoreLabel.insert_entries() from app.models import InvitationType InvitationType.insert_entries() from app.models import EducationType EducationType.insert_entries() from app.models import ScoreType ScoreType.insert_entries() from app.models import CourseType CourseType.insert_entries() from app.models import iPadCapacity iPadCapacity.insert_entries() from app.models import iPadState iPadState.insert_entries() from app.models import Room Room.insert_entries() from app.models import Lesson Lesson.insert_entries() from app.models import Section Section.insert_entries() from app.models import Assignment Assignment.insert_entries() from app.models import Test Test.insert_entries() from app.models import AnnouncementType AnnouncementType.insert_entries() from config import basedir data = raw_input(u'Enter data identifier (e.g.: initial or 20160422): ') datadir = os.path.join(basedir, 'data', data) if os.path.exists(datadir): from app.models import User User.insert_entries(data=data, basedir=basedir) from app.models import UserCreation UserCreation.insert_entries(data=data, basedir=basedir) from app.models import Punch Punch.insert_entries(data=data, basedir=basedir) from app.models import Tag Tag.insert_entries(data='initial', basedir=basedir) from app.models import Product Product.insert_entries(data='initial', basedir=basedir) from app.models import Course Course.insert_entries(data='initial', basedir=basedir) from app.models import Period Period.insert_entries(data='initial', basedir=basedir) from app.models import iPad iPad.insert_entries(data='initial', basedir=basedir) from app.models import iPadContent iPadContent.insert_entries(data='initial', basedir=basedir) from app.models import NotaBene NotaBene.insert_entries(data='initial', basedir=basedir) from app.models import Feed Feed.insert_entries(data=data, basedir=basedir) else: print u'---> Invalid data identifier: %s' % data
def deploy(): db.drop_all() db.create_all() users = [('*****@*****.**', u'知乎小管家', 'password'), ('*****@*****.**', u'Jack', 'password'), ('*****@*****.**', u'Jim', 'password'), ('*****@*****.**', u'麻花疼', 'password'), ('*****@*****.**', u'丁磊', 'password'), ('*****@*****.**', u'张家玮', 'password'), ('*****@*****.**', u'李开复', 'password'), ('*****@*****.**', u'张小北', 'password'), ('*****@*****.**', u'采铜', 'password'), ('*****@*****.**', u'张亮', 'password'), ('*****@*****.**', u'周晓农', 'password'), ('*****@*****.**', u'李楠', 'password'), ('*****@*****.**', u'马伯庸', 'password'), ('*****@*****.**', u'笑道人', 'password'), ('*****@*****.**', u'谢熊猫君', 'password')] for user in users: u = User(email=user[0], nickname=user[1], password=user[2]) u.username = create_username(u.nickname) db.session.add(u) db.session.commit() users = User.query.all() for user in users: other_users = users[:] other_users.remove(user) user2 = choice(other_users) user.follow_user(user2) db.session.commit() with open('zhihu_questions.pk', 'rb') as f: infos = pk.load(f) users = User.query.all() i = 0 while i < len(infos): q_html = infos[i]['detail'] title = infos[i]['title'] try: if i == 0: question = Question(user=users[0], title=title, content=html2text(q_html), content_html=q_html) db.session.add(question) db.session.commit a_html = infos[i]['answers'][0] answer = Answer(author=users[0], question=question, content=html2text(a_html), content_html=a_html) db.session.add(answer) db.session.commit() feed1 = Feed(user=users[0], action="ask_question", question=question) feed2 = Feed(user=users[0], action="answer_question", question=question, answer=answer) db.session.add_all([feed1, feed2]) db.session.commit() else: q_html = infos[i]['detail'] title = infos[i]['title'] prev_question = Question.query.order_by( Question.id.desc()).first() id_plus = randint(1, 4) question_id = prev_question.id + id_plus asker = choice(users) question = Question(id=question_id, user=asker, title=title, content=html2text(q_html), content_html=q_html) db.session.add(question) db.session.commit() feed1 = Feed(user=asker, action="ask_question", question=question) db.session.add(feed1) db.session.commit() answerers = users[:] j = 0 while j < len(infos[i]['answers']): answerer = choice(answerers) a_html = infos[i]['answers'][j] answer = Answer(author=answerer, question=question, content=html2text(a_html), content_html=a_html) db.session.add(answer) db.session.commit() feed2 = Feed(user=answerer, action="answer_question", question=question, answer=answer) db.session.add(feed2) db.session.commit() answerers.remove(answerer) j += 1 except Exception: continue i += 1 print(u'第%s个问题已收录' % i)
def feeds_delete(feed_id): if Feed.delete_feed(feed_id) is True: flash("Feed has been deleted", "success") return redirect(url_for("feeds_index"))
def add_feed (feed): if Feed.added(feed): return None db.session.add(feed) db.session.commit()
def feeds_index(): form = AddFeedForm() if AddFeedForm().validate_on_submit(): Feed.add_feed(form.data["name"], form.data["url"]) flash("Feed has been added", "success") return dict(feeds=Feed.get_all_feeds(), form=form)
def put(self, id): post = Post.get_by_id(id) if post is None or not post.can_edit() or post.is_hidden: return render_view(url_for('PostsView:index'), status=False, redirect=True, message=_('POST_NOT_FOUND')) form = PostForm(post=post) if form.is_submitted(): try: if not form.validate(): raise Exception(_('ERROR_INVALID_SUBMISSION')) cover_picture_id = request.values.get('cover_picture_id', 0, int) is_draft = request.values.get('status', 0, int) == Post.POST_DRAFT remain = request.values.get('remain', False, bool) if post.cover_picture and cover_picture_id == 0: # remove the picture, when user request its deletion post.cover_picture.remove() form.populate_obj(post) f = request.files.get('file') if f: if post.cover_picture: post.cover_picture.remove() picture = Picture.create() picture.save_file(f, current_user) post.cover_picture_id = picture.id if picture else 0 if is_draft: post.status = Post.POST_DRAFT else: if post.save_count == 1 or post.created_at is None: post.created_at = Post.current_date() post.save_count = 1 post.status = Post.POST_PUBLIC post.save_count += 1 post.editor_version = 1 post.save() Feed.clear_feed_cache() if post.is_draft: message = _('POST_DRAFT_SAVE_SUCESS') else: message = _('POST_PUBLIC_SAVE_SUCESS') if not remain: return render_view(url_for('PostsView:get', id=post.id), redirect=True, message=message) except Exception as e: flash(e.message, 'error') return render_view('admin/posts/edit.html', form=form, post=post)
try: user = User() user.id = 1 user.username = "******" user.password = ( "$2b$12$Sr35sAoWxKuq3hY8rf6yC.LjVHDd1ZF6jgsUZYGqvS1Mt3f7S4eSy" # user ) db.session.add(user) db.session.commit() print("User created user/user") except Exception as e: print(e) print("Problem in User creation") try: feed = Feed() feed.id = 1 feed.name = "HowToGeek" feed.url = "https://feeds.howtogeek.com/HowToGeek" feed.user_id = 0 db.session.add(feed) db.session.commit() print("Feed created") except Exception as e: print(e) print("Problem in Feed creation") try: feed = Feed() feed.id = 2 feed.name = "BBC-Tech"