def index(): commit_form = CommitForm() commit_form.course.choices = [(section.id, section.title) for section in current_user.sections] commit_form.assignment.choices = [] if commit_form.is_submitted(): commit_form = CommitForm(formdata=None) comment_form = CommentForm() if comment_form.is_submitted(): comment_form = CommentForm(formdata=None) page = request.args.get('page', 1, type=int) posts = current_user.followed_commits().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('index', page=posts.prev_num) \ if posts.has_prev else None return render_template('index.html', title='Home', posts=posts.items, commit_form=commit_form, comment_form=comment_form, next_url=next_url, prev_url=prev_url, )
def assignment(id): assignment = Assignment.query.filter_by(id=id).first_or_404() section = Section.query.filter_by(id=assignment.section_id).first_or_404() # commit form commit_form = CommitForm() set_options(commit_form, section, assignment) if commit_form.is_submitted(): commit_form = CommitForm(formdata=None) set_options(commit_form, section, assignment) post_form = PostForm() set_options(post_form, section, assignment) if post_form.is_submitted(): post_form = PostForm(formdata=None) set_options(post_form, section, assignment) comment_form = CommentForm() if comment_form.is_submitted(): comment_form = CommentForm(formdata=None) return render_template('_assignment.html', assignment=assignment, commit_form=commit_form, post_form=post_form, comment_form=comment_form)
def create_post(option, id): if option == 'commit_comment': form = CommentForm() if form.is_submitted(): if len(form.comment_post.data) >= 140 or len(form.comment_post.data) <= 5: flash('Your post must be between 5 and 140 characters!') return redirect(request.referrer) else: addition = CommitComment(commit_id=id, user_id=current_user.id, comment=form.comment_post.data) else: if option == 'commit': form = CommitForm() else: form = PostForm() assignment_id = form.assignment.data schoology_id = Assignment.query.filter_by(id=assignment_id).first().schoology_id if form.is_submitted() and option == 'commit': if len(form.post.data) <= 5: flash('Your post must be longer than 5 characters!') return redirect(request.referrer) elif len(form.post.data) >= 140: flash('Your post must be shorter than 140 characters!') return redirect(request.referrer) elif type(form.time_spent.data) != int: flash('Your time spent must be a number') return redirect(request.referrer) elif schoology_id is None: flash('You cannot post about a sample assignment') return redirect(request.referrer) else: addition = Commit(user_id=int(current_user.id), assignment_id=int(assignment_id), schoology_id=int(schoology_id), body=str(form.post.data), time_spent=int(form.time_spent.data)) if form.is_submitted() and option == 'post': if len(form.post.data) <= 12: flash('Your post must be longer than 12 characters!') return redirect(request.referrer) elif schoology_id is None: flash('You cannot post about a sample assignment') return redirect(request.referrer) else: addition = Post(user_id=int(current_user.id), assignment_id=int(assignment_id), schoology_id=int(schoology_id), body=form.post.data) db.session.add(addition) db.session.commit() flash('Congratulations for posting!') return redirect(request.referrer)
def commit(request): today = datetime.date.today() weekday = get_week_day(datetime.datetime.now()) if request.method == 'POST': form = CommitForm(request.POST) if form.is_valid(): urlinfor = form.save(commit=False) urlinfor.save() print(urlinfor) messages.success(request, "提交成功! 审核期1个工作日。") return HttpResponseRedirect(reverse('commit')) else: form = CommitForm() return render(request, 'commit.html', { 'form': form, "today": today, "weekday": weekday })
def post(self, request, *args, **kwargs): was_limited = getattr(request, 'limited', False) if was_limited: messages.warning(self.request, "操作太频繁了,请1分钟后再试") return render(request, 'app/commit.html', {'form': CommitForm()}) return super().post(request, *args, **kwargs)