def addPost(): url = 'editpost.html' user = getUserByEmail(login_session['email']) if user: # POST method add category if request.method == 'POST': # check if title and content exists title = request.form.get('title', None) content = request.form.get('post_content', None) picture = request.form['picture'] description = request.form['description'] if title and content: post = Post( title=title, user_id=user.id, picture=picture, description=description, post_content=content, date_added=datetime.datetime.now(), ) session.add(post) session.commit() keywords = request.form['keywords'].split(',') try: for k in keywords: if k: k = Keyword(post_id=post.id, word=k) session.add(k) session.commit() except Exception as e: print e flash(create_alert('Error in saving keywords', 'danger')) return redirect(url_for('showPost', post_id=post.id)) else: # title or content is empty if request.form['origin'] == 'html': # html edit status url = 'editpost_html.html' flash( create_alert('Title and Content must not be empty', 'danger')) return render_template(url, user_logged=user, post=None) # GET method shows add page if request.args.get('mode'): url = 'editpost_html.html' post = Post(title='', description='', post_content='', picture='') return render_template(url, post=post, edit=False, user_logged=user) else: return redirect(url_for('login'))
def create(): ''' Creates a new article ''' if request.method == 'POST': if request.form['title'] != '' and request.form['body'] != '' and request.form['picture'] != '.gitignore': # create the new article here :) if request.form['topic'] == '_newTopic': art = Post(topic = request.form['newTopic'],title=request.form['title'],picture='/static/' + request.form['picture'],body=request.form['body']) else: art = Post(topic = request.form['topic'],title=request.form['title'],picture='/static/' + request.form['picture'],body=request.form['body']) db.session.add(art) db.session.commit() flash('successfully created new article',category='info') else: flash('error, missing required portion, or using invalid Image',category='error') return render_template('addArticle.html')
def postmessage(): if redirect_is_not_logged(session) != True: return redirect_is_not_logged(session) new_post = {} if 'username' in session: if request.method == 'POST': message = request.form.get('message', None) title = request.form.get('title', None) redirection = request.form.get('redirection', None) if title: post = Post(title=title, message=message, user_id=session['id'], created=datetime.datetime.now()) db.session.add(post) db.session.commit() if redirection is not None: user = db.session.execute( 'SELECT user.* FROM user WHERE user.username = :username', { 'username': redirection }).first() print(redirection) if user is not None: return redirect(url_for('profile', username=user.username)) # new_post['title'] = title # new_post['message'] = message # return render_template('index.html', page_name="Home", new_post=new_post , session=session) return redirect(url_for('index'))
def post(self): if not self.user: self.redirect('/login') return postbutton = self.request.get("postbutton") if postbutton == "submit": subject = self.request.get('subject') content = self.request.get('content') if subject and content: p = Post(parent=blog_key(), subject=subject, content=content, postuser=self.user) p.put() self.redirect('/blog/%s' % str(p.key().id())) return else: error = "You must enter the subject and the content for the post." self.render("newpost.html", subject=subject, content=content, error=error) if postbutton == "cancel": self.redirect('/')
def fetch_x_days(sub, sub_id, last=None): param = {"before": f"t3_{base36.dumps(last)}"} if last is not None else {} i = 0 for s in reddit.subreddit(sub).new(limit=1000, params=param): author = getattr(s, "author", {}) try: p = Post(title=s.title, user_id=int(getattr(author, "id", "-1"), 36), username=getattr(author, "name", ""), link=s.url, num_comments=s.num_comments, score=s.score, r_id=int(s.id, 36), date=date.fromtimestamp( s.created_utc).strftime("%Y-%m-%d"), updated=int(time() * 1000), sub_id=sub_id) except Exception as e: print(f"oopsie\n{e}") continue session.add(p) if i % 100 == 0: session.commit()
def about(): form = Publish() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash('Post has been created!', 'success') return redirect(url_for('home')) return render_template('about.html', form=form, legend='New Post')
def createPost(): nTitle = request.forms.get('title') nBody = request.forms.get('body') nDateTime = datetime.datetime.now() author = User.get(User.firstname == 'alibi') post = Post(title=nTitle, body=nBody, published_date=nDateTime, user=author) post.save() return template('blog', posts=Post.select())
def query(self, id): try: item = db.session.query(Item).get(id) post = db.session.query(Post).get(item.post_id) if post is None: post = Post(**config.POST_TEMPLATE) return { "name": item.name, "post": post, "display": item.display } except: db.session.rollback() return None
def add(self, title, description, usage, complexity, code, note): try: post = Post(title=title, description=description, usage=usage, complexity=complexity, code=code, note=note, author=current_user.name) db.session.add(post) db.session.commit() return True except: db.session.rollback() return False
def newPosts(): if 'username' not in login_session: return redirect('/login') if 'username' in login_session: if request.method == 'POST': newpost = Post(name=request.form['name'], user_id=login_session['user_id'], post_type=request.form['post_type'], description=request.form['description'], image=request.form['image'], ratings=request.form['ratings']) session.add(newpost) flash("New Post Created Successfully") session.commit() return redirect(url_for('showPosts')) else: return render_template('newPost.html')
def apply(self, id, title, description, usage, complexity, code, note): try: post = Post(title=title, description=description, usage=usage, complexity=complexity, code=code, note=note, author=current_user.name) db.session.add(post) db.session.flush() item = db.session.query(Item).get(id) item.post_id = post.id db.session.commit() return True except: db.session.rollback() return False
def addNewPosts(subredditName, session): subreddit = reddit.subreddit(subredditName) newPosts = [] existingPosts = [pId for pId, in session.query(Post.postId)] for submission in subreddit.new(limit=100): if submission.id not in existingPosts: post = Post(postId=submission.id, title=submission.title, subreddit=submission.subreddit.display_name, created=int(submission.created_utc), author=submission.author.name) newPosts.append(post) session.add_all(newPosts) session.commit() logging.info(f"{subredditName}: added {len(newPosts)} new posts")
def addpost(): if request.method == 'GET': return render_template('addpost.html') else: post_title = request.form.get('title') post_continent = request.form.get('continent') post_country = request.form.get('country') post_city = request.form.get('city') if post_city != "": post_country += "," post_text = request.form.get('textish') post_pic_url = request.form.get('pic_url') thepost = Post(title=post_title, continent=post_continent, country=post_country, city=post_city, text=post_text, img_url=post_pic_url) session.add(thepost) session.commit() return redirect(url_for('feed'))
def new_post(self): post = Post() txt = input() self.__posts = self.__data.get_posts() # checks if posts.txt is empty (ternary doesn't work) if self.__posts == []: temp = str() else: temp = self.__posts[-1] post.set_crt(self.__cur_user.get_id()) # to avoid 'index out of range' error (ternary doesn't work) if isinstance(temp, str): id = 0 else: id = temp.get_id() post.set_id(id + 1) post.set_txt(txt) self.__data.push_post(post)
from database import db, User,Post,Comment,Anon,Target from werkzeug.security import generate_password_hash, check_password_hash from app import ap #from database import db db.init_app(ap) with ap.app_context(): db.create_all() #two example users b = User(name='test',password='******',email='[email protected]') a = User(name='ethan',password='******',email='a',permission=999) # add and save the users db.session.add(a) db.session.add(b) db.session.commit() #example of an article p1 = Post(topic='misc',title='Example Article',picture='/static/Pic.jpg',body='This is the body of the article, which accepts <i> HTML tags </i>') p2 = Post(topic='misc',title='Ex2',picture='/static/Pic.jpg',body='some random placeholder text here please') p3 = Post(topic='a new topic appears',title='Example Article',picture='/static/Pic.jpg',body='I yote a duck off a cliff... turns out they can fly, so everything was fine') db.session.add(p1) db.session.add(p2) db.session.add(p3) db.session.commit() #c = Comment(title='test',message='I love testing',poster='testman',article=1) #db.session.add(c) db.session.commit() quit()