def test_poet_model(self): """Does poet model work?""" poet = Poet.signup(username='******',password='******',email="*****@*****.**",image_url=None) db.session.add(poet) db.session.commit() self.assertEqual(poet.username, 'test') self.assertEqual(Poet.authenticate(username='******',password='******'),poet)
def poets(): username = str(request.data.get('username', '')) data = request.data if request.method == "POST": if username: poet = Poet(**data.to_dict()) poet.save() response = jsonify({ 'name': poet.name, 'username': poet.username, 'email': poet.email, }) response.status_code = 201 return response else: # GET poet_info = Poet.query.filter_by(username=username).first().lookup() response = jsonify(poet_info) response.status_code = 200 return response
def test_Quote_model(self): """Does the quote model work?""" poet = Poet.signup(username='******',password='******',email="*****@*****.**",image_url=None) db.session.add(poet) db.session.commit() quote = Quote(content='this is a test message',poet_id=poet.id) db.session.add(quote) db.session.commit() self.assertEqual(quote.poet,poet) self.assertEqual(quote.content,'this is a test message')
def setUp(self): """Create test client, add sample data.""" Poet.query.delete() Quote.query.delete() self.client = app.test_client() self.poet = Poet.signup(username="******", email="*****@*****.**", password="******", image_url=None) db.session.commit()
def test_signup(self): """Does the signup route work""" with self.client as c: resp = c.post("/signup", data={ "username": "******", "password": "******", "email": '*****@*****.**' }) self.assertEqual(resp.status_code, 302) self.assertTrue( Poet.authenticate(username='******', password='******') != False)
def test_comment_model(self): """Does the comment functionality work?""" poet = Poet.signup(username='******',password='******',email="*****@*****.**",image_url=None) db.session.add(poet) db.session.commit() quote = Quote(content='this is a test message',poet_id=poet.id) db.session.add(quote) db.session.commit() comment = Comment(content='test comment', poet_id=poet.id,quote_id=quote.id) db.session.add(comment) db.session.commit() self.assertEqual(comment.poet,poet) self.assertEqual(comment.content,'test comment') self.assertIn(comment, quote.comments)
def login(): if g.poet: flash("you are already logged in!") return redirect('/') form = LoginForm() if form.validate_on_submit(): username = form.username.data password = form.password.data poet = Poet.authenticate(username, password) if poet: do_login(poet) return redirect('/') else: form.username.errors.append('Invalid username or password') return render_template('/user/login.html', form=form)
def signup(): form = SignupForm() if form.validate_on_submit(): try: poet = Poet.signup(username=form.username.data, email=form.email.data, password=form.password.data, image_url=form.image_url.data or Poet.image_url.default.arg) db.session.commit() do_login(poet) return redirect('/') except IntegrityError: beans = IntegrityError raise return redirect('/beans') return render_template('/user/signup.html', form=form)
def Spider(self): while not self.poet_queue.empty(): url = self.poet_queue.get() req = requests.get(url, headers=get_header()) if req.status_code == 200: req.encoding = 'utf-8' html = etree.HTML(req.text) name = html.xpath( '/html/body/div[4]/div[2]/div[2]/div[1]/h2/a/text()' )[0] dynasty = html.xpath( '/html/body/div[4]/div[2]/div[2]/div[1]/div[2]/text()') if len(dynasty) == 0: dynasty = '未知' else: dynasty = dynasty[0] introduction = html.xpath( '/html/body/div[4]/div[2]/div[2]/div[1]/div[4]' )[0].xpath('string(.)').strip() with app.app_context(): poet = Poet(name=name, dynasty=dynasty, introduction=introduction) db.session.add(poet) db.session.commit() id = poet.id poem_num = html.xpath( '/html/body/div[4]/div[2]/div[2]/div[1]/div[3]/text()' )[0][:-1] poet_url_list = [] for i in range(1, int(int(poem_num) / 40) + 2): poet_id = re.sub("\D", "", url) poet_page_url = 'http://www.shicimingju.com/chaxun/zuozhe/{}_{}.html'.format( poet_id, i) req1 = requests.get(url=poet_page_url, headers=get_header()) if req1.status_code == 200: req1.encoding = 'utf-8' list_html = etree.HTML(req1.text) poet_url = list_html.xpath('//*/h3/a/@href') poet_url_list += poet_url poet_url_list = map(lambda x: self.base_url + x, poet_url_list) for url in poet_url_list: print(url) req2 = requests.get(url, headers=get_header()) if req2.status_code == 200: req2.encoding = 'utf-8' poet_html = etree.HTML(req2.text) title = poet_html.xpath( '//*[@class="shici-title"]/text()')[0] content = '\n'.join( poet_html.xpath( '//*[@class="shici-content"]/text()') ).strip() if not content: content = '\n'.join( poet_html.xpath( '//*[@class="para"]/text()')).strip() if len( poet_html.xpath( '//*[@class="shangxi-container"]') ) == 0: analysis = '' else: analysis = poet_html.xpath( '//*[@class="shangxi-container"]' )[0].xpath('string(.)').strip() with app.app_context(): poem = Poem(title=title, content=content, analysis=analysis, author=id) db.session.add(poem) db.session.commit()