def til_status(): status = request.form.get('status') ids = request.form.getlist('ids[]') if any(status in code for code in Til.STATUSES) and len(ids) > 0: Til.objects(code__in=ids).update(set__status=status) return {'status': True} else: return {'status': False}
def twitter_sync(): posts = twitter.fetch_posts() new_posts_count = 0 for post in posts: post['source'] = Til.SOURCE_TWITTER til_post = Til(**post) try: til_post.save() new_posts_count += 1 except NotUniqueError: pass print(f'New tweets: {new_posts_count}')
def reddit_sync(): for subreddit in app.config['REDDIT_SUBREDDITS']: posts = reddit.fetch_posts(subreddit) new_posts_count = 0 for post in posts: post['source'] = Til.SOURCE_REDDIT til_post = Til(**post) try: til_post.save() new_posts_count += 1 except NotUniqueError: pass print(f'{subreddit}: {new_posts_count}')
def til(request): if request.method == 'POST': form = TilForm(request.POST) if form.is_valid(): til = Til(user_id=request.user.id, user_name=request.user.username, user_email=request.user.email, **form.cleaned_data) til.save() return redirect('til:til') else: form = TilForm() return render( request, 'til/til.html', { 'form': form, 'now': date.today(), 'til_list': Til.objects.filter(user_id=request.user.id) })
def toggle_visible(): try: post = Til.objects(code=request.form.get('id')).get() post.visible = not post.visible if post.visible: post.learned = datetime.utcnow() post.save() return {'status': True, 'visible': post.visible} except Til.DoesNotExist: return {'status': False}
def create_til(): form = forms.TilForm() if request.method == 'POST': if form.validate_on_submit(): til_post = Til(code=str(uuid.uuid4()), source=Til.SOURCE_ME, author='', content=html.escape(form.title.data), extended=html.escape(form.content.data), url='/', status=Til.STATUS_CURRENT) til_post.save() flash('Your post has been created', 'success') return redirect(url_for('current')) else: return render_template('til.html', form=form, action='create_til', id=None)
def post(self): json_data = request.get_json(force=True) til = Til() til.language = json_data.get('language') til.title = json_data.get('title') til.text = json_data.get('text') til.save() return til.to_dict()
def date_tils(date): posts = [] try: date = datetime.strptime( date, '%Y-%m-%d') if date else datetime.today().date() except ValueError: date = None if date: next_date = date + timedelta(days=1) posts = Til.objects(visible=True, learned__gte=date, learned__lt=next_date).order_by('-learned') return render_template('date.xml', posts=posts), 200, { 'Content-Type': 'application/xml' }
def update_til(id): til_post = Til.objects(code=id).get() form = forms.TilForm() if request.method == 'POST': if form.validate_on_submit(): til_post.content = form.title.data til_post.extended = form.content.data til_post.save() flash('Post was updated', 'success') return redirect(url_for('current')) else: form.title.data = til_post.content form.content.data = til_post.extended return render_template('til.html', form=form, action='update_til', id=id)
def icebox(): posts = Til.objects(status=Til.STATUS_ICEBOX).order_by('-created') return render_template('icebox.html', posts=posts)
def backlog(): posts = Til.objects(status=Til.STATUS_BACKLOG).order_by('-created') return render_template('backlog.html', posts=posts)
def current(): posts = Til.objects(status=Til.STATUS_CURRENT).order_by('-created') return render_template('current.html', posts=posts)
def feed_tils(): posts = Til.objects(visible=True).order_by('-learned') return render_template('feed.xml', posts=posts), 200, { 'Content-Type': 'application/xml' }
def home(): posts = Til.objects(visible=True).order_by('-learned') return render_template('home.html', posts=posts)
def remove_til(id): til_post = Til.objects(code=id).get() til_post.delete() flash('Your post has been deleted', 'success') return redirect(url_for('current'))