def add_link(self, name, callback_url): try: link = Links(name=name, callback_url=callback_url, pub_date=get_datetime()) self.session.add(link) self.session.commit() return True except Exception as e: return False
def change_description(self, id, content): try: self.session.query(Description).filter(Description.id == id).update( {"content": content, "pub_date": get_datetime()}) self.session.commit() return True except Exception as e: return False
def add_category(self, name, basename): try: category = Category(name=name, basename=basename, pub_date=get_datetime()) self.session.add(category) self.session.commit() return True except Exception as e: return False
def change_link(self, id, link_name, link_url): try: self.session.query(Links).filter(Links.id == id).update( {"name": link_name, "callback_url": link_url, "pub_date": get_datetime()}) self.session.commit() return True except Exception as e: return False
def change_category(self, id, category_name, category_basename): try: self.session.query(Category).filter(Category.id == id).update( {"name": category_name, "basename": category_basename, "pub_date": get_datetime()}) self.session.commit() return True except Exception as e: return False
def change_tag(self, id, tag_name): print(tag_name) try: self.session.query(Tag).filter(Tag.id == id).update( {"name": tag_name, "pub_date": get_datetime()}) self.session.commit() return True except Exception as e: return False
def add_article(self, title, description, category, tags, content, username): try: username = turn_bytes_to_str(username) now_time = get_datetime() category_obj = self.session.query(Category).filter(Category.name == category).first() user_obj = self.session.query(User).filter(User.username == username).first() article = Article( title=title, description=description, content=content, user_id=user_obj.id, category_id=category_obj.id, pub_date=now_time, last_modify_date=now_time, ) # 标签名重复不添加,不重复添加 tag_list = [] for tmp in tags: tag_obj = self.session.query(Tag).filter(Tag.name == tmp).first() if tag_obj is not None: # 标签重复,不添加,获取标签对象 tag = tag_obj tag_list.append(tag) # print('标签重复,不添加') else: # 标签为新标签,先创建新标签 tag = Tag() tag.name = tmp tag.pub_date = now_time tag_list.append(tag) self.session.add(tag) self.session.commit() article.tags = tag_list self.session.add(article) self.session.commit() return article except Exception as e: return False
def update_article(self, id, title, description, category, tags, content): try: # username = turn_bytes_to_str(username) now_time = get_datetime() category_obj = self.session.query(Category).filter(Category.name == category).first() # user_obj = self.session.query(User).filter(User.username == username).first() self.session.query(Article).filter(Article.id == id).update({ "title": title, "description": description, "content": content, "category_id": category_obj.id, "last_modify_date": now_time, }) self.session.commit() tag_list = [] for tmp in tags: tag_obj = self.session.query(Tag).filter(Tag.name == tmp).first() if tag_obj is not None: # 标签重复,不添加,获取标签对象 tag = tag_obj tag_list.append(tag) # print('标签重复,不添加') else: # 标签为新标签,先创建新标签 # print(tmp,"即将被添加") tag = Tag() tag.name = tmp tag_list.append(tag) self.session.add(tag) self.session.commit() article = self.session.query(Article).filter(Article.id == id).first() article.tags = tag_list self.session.add(article) self.session.commit() return article except Exception as e: return False
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from modules.common import get_datetime url = "https://www.oschina.net/news" headers = { 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36', 'referer': 'https://www.oschina.net/' } engine = create_engine(SQLALCHEMY_DATABASE_URI) Session = sessionmaker(bind=engine, autoflush=False, autocommit=False) session = Session() r = requests.get(url, headers=headers) soup = BeautifulSoup(r.content.decode(), 'html.parser') for item in soup.select('div[class="item clear"]'): try: title, img_src, article_src, desc = item.div.a.get_text(), item.div.a['href'], item.img['src'], \ item.find_all("div", class_="sc text text-gradient summary")[0].get_text() except IndexError as e: title, img_src, article_src, desc = item.div.a.get_text(), item.div.a['href'], item.img['src'], \ item.find_all("div", class_="summary")[0].get_text() except Exception as e: pass cur = engine.execute( "insert into rotates (title, description, img_src, article_src, pub_date) values (%s, %s, %s, %s, %s)", [(title, desc, img_src, article_src, get_datetime())])
def regist(self, username, password, email, role='ordinary'): """ 注册用户,从首页注册的都是普通用户,后台注册可以指定是什么用户 :return: """ # self.session.add(User(username=username, password=password, email=email)) # self.session.add(Role(name='ordinary')) # self.session.commit() try: role_list = [] user = User(username=username, password=password, email=email, create_date=get_datetime()) role_obj = self.session.query(Role).filter(Role.name == role).first() print(role_obj.id) if role_obj is not None: role = role_obj role_list.append(role) else: role = Role(name='ordinary') self.session.add(role) self.session.commit() user.roles = role_list self.session.add(user) self.session.commit() return True except IntegrityError as e: return False