def email_validate(statu, recieve_email=None): if recieve_email is None: if statu == 'activate': form = EmailValidateForm() if request.method == 'POST': if form.validate_on_submit(): return redirect('/validator/validation/%s' % form.email.data) return render_template('validate.html', title='Validate the email', form=form) else: e = Email() e.email = recieve_email if statu == 'validation': if not e.is_exist(): e.generate_password() email_msg = Message(recipients=[recieve_email], subject='OPEN ACCESS PUBLISH validation ') email_msg.body = 'CLICK HERE TO VALIDATE' email_msg.html = "<h1>Activation</h1><p><a href='http://jinmingyi.xin:8080/captcha/%s'>Click to activate</a></p>" % e.password sendEmail(email_msg) e.validate_time = datetime.datetime.now() db.session.add(e) db.session.commit() return "We've already send you an validation email" elif not e.is_validated(): return "<a href='/validator/resend/%s'>Didn't receive email?</a>" % recieve_email else: abort(404) elif statu == 'resend': if e.is_exist(): if not e.is_validated(): email_msg = Message( recipients=[recieve_email], subject='OPEN ACCESS PUBLISH validation ') email_msg.body = 'CLICK HERE TO VALIDATE' email_msg.html = "<h1>Activation</h1><p><a href='http://jinmingyi.xin:8080/captcha/%s'>Click to activate</a></p>" % e.password sendEmail(email_msg) return "We've already send you an validation email" abort(404) abort(404)
def detail(article_id): captcha = getCaptcha() form = CommentForm() article = Article.query.filter_by(id=article_id, is_hide='no').first() if article is not None: record = IpRecord() record.page = "detail" record.ip = request.remote_addr record.target_id = article_id db.session.add(record) db.session.commit() vis = int( IpRecord.query.filter_by(target_id=article_id, page="detail").group_by("ip").count()) article.visit = vis comments = Comment.query.filter_by(target=article.id).order_by( Comment.date.desc()).all() if request.method == 'POST': # post a comment if form.validate_on_submit() and check_session(): e = Email(email=form.email.data) if e.is_exist() and e.is_validated(): # generate a comment and add it comment = Comment(target=article.id, content=check_text(form.comment.data), email=form.email.data, id=1) t_num = int(Comment.query.count()) if t_num > 0: comment.id = Comment.query.order_by( Comment.id.desc()).first().id + 1 comment.date = datetime.datetime.now() db.session.add(comment) db.session.commit() # email actions email_msg = Message(recipients=[e.email], subject="Notification") email_msg.html = """<h1>Notication</h1><p>Your email has made a comment on <a href='http://jinmingyi.xin:8080/detail/%s'>website</a></p>""" % str( article_id) sendEmail(email_msg) return redirect('/detail/' + str(article_id)) return render_template('detail.html', form=form, title='Detail', article=article, comments=comments, captcha=captcha) abort(404)
def publish(): ''' If recieve a post, then regard as a publish form. A form will create an article class and do related work :return: ''' if not check_session(3): abort(404) form = UploadForm() captcha = getCaptcha() msg = "You should only upload a pdf file" if request.method == 'POST': if form.validate_on_submit(): e = Email(email=form.email.data) if e.is_exist() and e.is_validated(): # Every module below are independent # generate an article and add it article = form.to_Article() for s in CSsubject: if s == article.subject: article.subject = 'Computer Sciences' article.id = str(1) a_num = int(Article.query.count()) if a_num > 0: article.id = str( int( Article.query.order_by( Article.id.desc()).first().id) + 1) article.pdf = str(article.id) + '.pdf' filename = os.path.join(app.root_path, "static", "pdf", article.id + '.pdf') form.file.data.save(filename) db.session.add(article) # if a subject is not exist, then create it sub = Subject.query.filter_by(name=article.subject).first() if sub is None: sub = Subject() sub.name = article.subject sub.super_subject = 0 sub.depth = 0 db.session.add(sub) # generate a record and add it record = IpRecord() record.ip = request.remote_addr record.page = "publish" record.target_id = int(article.id) db.session.add(record) # after all work done, commit it db.session.commit() # send email email_msg = Message( recipients=[form.email.data], subject='[OPEN ACCESS PUBLISH]Publish notification') email_msg.body = 'CLICK HERE TO VALIDATE' email_msg.html = "<h1>Notification</h1><p>You have published an <a href='http://jinmingyi.xin:8080/detail/%s'>article</a></p>" % ( str(article.id)) sendEmail(email_msg) return redirect('/detail/' + str(article.id)) else: msg = "You must activate your email address before you publish" return render_template('publish.html', form=form, title='Publish', message=msg, captcha=captcha)