def main(): """@todo: Docstring for main. :returns: @todo """ print("Initializing DB...") db.drop_all(bind=[None]) db.create_all(bind=[None]) print("Getting latest alembic revision since we are generating this DB") args = shlex.split("alembic history") p = subprocess.Popen(args, stdout=subprocess.PIPE) output, error = p.communicate() data = output.split('\n') latest_alembic = None for row in data: if "(head)" in row: cols = row.split(" ") latest_alembic = cols[2].strip() if latest_alembic: print("Stamping with latest Alembic revision: %s" % latest_alembic) args = shlex.split("alembic stamp %s" % latest_alembic) subprocess.Popen(args, stdout=subprocess.PIPE) hashed_password = generate_password_hash('testy21') # Create a user to update and delete later. user = User(name="Test User", email='*****@*****.**', admin=True, openid='', password=hashed_password) user.insert() safe_commit()
def upload_file(payload, ufile): if ufile and allowed_file(ufile.filename): filename = secure_filename(ufile.filename) path = os.path.join(".", app.config['UPLOAD_FOLDER'], filename) if not os.path.isfile(path): ufile.save(path) try: # Create thumbnail from PIL import Image size = 128, 128 im = Image.open(path) width, height = im.size im.thumbnail(size, Image.ANTIALIAS) file, ext = os.path.splitext(path) ext = ext.lower() etype = ext.replace(".", "") if etype == 'jpg': etype = 'jpeg' im.save(file + "_thumbnail" + ext, etype) except (ImportError, IOError): width = 0 height = 0 afile = File(name=filename, user_id=payload.get('user_id'), path=path, size=os.path.getsize(path), width=width, height=height, mimetype=ufile.mimetype) afile.insert() safe_commit() return afile.id return None
def delete_user(): return_value = success('The user was deleted.') payload = get_payload(request) if not g.user or g.user.id != payload.get('id'): user = User.filter(User.id == payload.get('id')).first() if user: user.delete() safe_commit() else: return_value = failure('That user does not exist.') else: return_value = failure('You cannot delete the current user.') return jsonify(return_value)
def create_content(): return_value = success('The content was created.') payload = get_payload(request) editing = False if payload.get('id'): content = Content.get(payload.get('id')) editing = True return_value = success('The content was updated.') else: content = Content() content.type = payload.get('type').lower() content.title = payload.get('title') content.body = payload.get('body') content.user_id = payload.get('user_id') tags = [t.strip() for t in payload.get('tags', '').split(',') if t.strip()] for tag in tags: count = Tag.filter(Tag.name == tag).count() if not count: new_tag = Tag(name=tag) new_tag.insert() content.tags = ",".join(tags) content.parser = payload.get('parser', 'markdown') content.published = bool(payload.get('published', False)) if not editing: content.slug = make_slug(content.title) else: created_on = payload.get('created_on') if created_on: content.created_on = parser.parse(created_on) valid = content.validate() if valid['success'] or editing: content.insert() safe_commit() return_value['id'] = content.id else: return_value = valid return jsonify(return_value)
def update_user(): return_value = success('The user was updated.') payload = get_payload(request) user = User.get(payload.get('id')) if not user: return_value = failure('That user does not exist.') else: if payload.get('password'): hashed_password = generate_password_hash(payload.get('password')) if payload.get('email'): user.email = payload.get('email') if payload.get('name'): user.name = payload.get('name') user.password = hashed_password safe_commit() return_value['user'] = user.to_dict(camel_case=True) return jsonify(return_value)
def test_user_create(self): api_key = self.s.sign(self.api_key.name) ''' CREATE ''' post_data = { 'name': 'Testy McTesterson', 'email': '*****@*****.**', 'password': '******', } # Try to create the user with no API key rv = self.app.post('/user_create', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertFalse(data['success']) # Create the user. This should work fine. post_data['api_key'] = api_key rv = self.app.post('/user_create', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertTrue(data['success']) self.assertTrue(data['id']) self.assertIsNotNone(data['messages']) self.assertEquals(data['messages'][0], 'The user was created.') user_id = data['id'] # Make sure that we can grab the user from the DB. user = User.get(user_id) self.assertIsNotNone(user) self.assertEquals(user.name, 'Testy McTesterson') # Try to create the same user again. This should fail. rv = self.app.post('/user_create', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertFalse(data['success']) self.assertIsNotNone(data['messages']) self.assertEquals(data['messages'][0], 'That user exists already.') # Clean up! user.delete() safe_commit()
def create_user(): return_value = success('The user was created.') payload = get_payload(request) hashed_password = generate_password_hash(payload.get('password')) user = User() user.email = payload.get('email') user.name = payload.get('name') user.password = hashed_password valid = user.validate() if valid['success']: user.insert() safe_commit() return_value['id'] = user.id else: del(user) return_value = valid return jsonify(return_value)
def setUp(self): impression.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db' impression.app.config['TESTING'] = True self.app = impression.app.test_client() # Drop and create DB. impression.db.drop_all(bind=[None]) impression.db.create_all(bind=[None]) key = '{0:02X}'.format(randrange(36**50)) self.api_key = ApiKey(key=key, name='test-key') self.api_key.insert() self.s = TimestampSigner(key) safe_commit() hashed_password = generate_password_hash('password-123') # Create a user to update and delete later. self.user = User(name="Test User", email='*****@*****.**', admin=True, openid='', password=hashed_password) self.user.insert() safe_commit()
def test_content_create(self): api_key = self.s.sign(self.api_key.name) ''' CREATE ''' post_data = { 'title': 'This is a test page', 'body': 'Blah blah blah', 'type': 'post', 'user_id': self.user.id } # Try to create the content with no API key rv = self.app.post('/content_create', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertFalse(data['success']) # Create the content. This should work fine. post_data['api_key'] = api_key rv = self.app.post('/content_create', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertTrue(data['success']) self.assertTrue(data['id']) self.assertIsNotNone(data['messages']) self.assertEquals(data['messages'][0], 'The content was created.') content_id = data['id'] # Make sure that we can grab the content from the DB. content = Content.get(content_id) self.assertIsNotNone(content) self.assertEquals(content.title, post_data['title']) # Try to create the same content again. This should fail. rv = self.app.post('/content_create', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertFalse(data['success']) self.assertIsNotNone(data['messages']) self.assertEquals(data['messages'][0], 'That post or page exists already.') # Clean up! content.delete() safe_commit() # Create the content. This should work fine. post_data['api_key'] = api_key post_data['type'] = 'page' rv = self.app.post('/content_create', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertTrue(data['success']) self.assertTrue(data['id']) self.assertIsNotNone(data['messages']) self.assertEquals(data['messages'][0], 'The content was created.') content_id = data['id'] # Make sure that we can grab the content from the DB. content = Content.get(content_id) self.assertIsNotNone(content) self.assertEquals(content.title, post_data['title']) # Try to create the same content again. This should fail. rv = self.app.post('/content_create', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertFalse(data['success']) self.assertIsNotNone(data['messages']) self.assertEquals(data['messages'][0], 'That post or page exists already.') # Clean up! content.delete() safe_commit()
def test_content_retrieve(self): user_id = self.user.id ''' RETRIEVE ''' # Create some content using the model directly... content = Content(title="Test Content", published=True, type="post", body="blah blah blah", user_id=self.user.id) content.insert() content1 = content.to_dict() content2 = Content(title="Test Content 2", published=True, type="post", body="blah blah blah", user_id=self.user.id) content2.insert() content2 = content2.to_dict() content3 = Content(title="Test Content 3", published=True, type="post", body="blah blah blah", user_id=self.user.id) content3.insert() content3 = content3.to_dict() content4 = Content(title="Test Content 4", published=True, type="post", body="blah blah blah", user_id=self.user.id) content4.insert() content4 = content4.to_dict() safe_commit() post_data = { 'id': content.id } # retrieve the content. This should work fine. rv = self.app.post('/content_retrieve', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertTrue(data['success']) self.assertTrue(data['contents'][0]) self.assertIsNotNone(data['messages']) content = Content.get(data['contents'][0]['id']) self.assertEquals(content.title, data['contents'][0]['title']) self.assertEquals(content.body, data['contents'][0]['body']) self.assertEquals(user_id, data['contents'][0]['user_id']) post_data = { 'content_type': 'post', 'page_size': 3 } # retrieve the content. This should work fine. rv = self.app.post('/content_retrieve', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertTrue(data['success']) # There should be three posts. self.assertEquals(data['contents'][0]['title'], content4['title']) self.assertEquals(data['contents'][1]['title'], content3['title']) self.assertEquals(data['contents'][2]['title'], content2['title']) # And only three posts returned self.assertTrue(len(data['contents']) == 3) # Posts should be in the right order self.assertTrue(data['contents'][1]['published_on'] < data['contents'][0]['published_on']) self.assertIsNotNone(data['messages']) post_data = { 'content_type': 'post', 'current_page': 2, 'page_size': 3 } # retrieve the content. This should work fine. rv = self.app.post('/content_retrieve', data=post_data, follow_redirects=True) data = json.loads(rv.data) self.assertTrue(data['success']) # There should be one post. self.assertEquals(data['contents'][0]['title'], content1['title']) # And only one post returned self.assertTrue(len(data['contents']) == 1)