def _import(self, folder): json_folder = folder + JSONPATTERN print("Nuking the DB:", end='') Record.objects.all().delete() Tag.objects.all().delete() print("Done") print("Importing: %s" % json_folder) file_counter = 0 for filename in iglob(json_folder): with open(filename) as f: file_counter += 1 print("#%s" % file_counter) details = load_json(f) record = Record( record_number=details['record number'] if 'record number' in details else None, area=details['area'] if 'area' in details else None, date_raw=details['date'] if 'date' in details else None, date=self._date_cleanup(details['date']) if 'date' in details else None, street=details['street'] if 'street' in details else None, number=details['number'] if 'number' in details else None, image_url=details['image_url'] if 'image_url' in details else None, description=details['description'] if 'description' in details else None, caption=details['caption'] if 'caption' in details else None) record.save() if record.date is None: print( "Error with date in %s: %s" % (details['record number'], details['date'] if 'date' in details else "NO DATE")) if details['tags'] is not None: for tag_title in details['tags']: tag_title = self._tag_cleanup(tag_title) try: tag = Tag.objects.get(title=tag_title) except Tag.DoesNotExist: tag = Tag(title=tag_title) tag.save() record.tags.add(tag) record.save()
def setUp(self): super(BaseTestCase, self).setUp() self.app = create_app(config.Testing) self.app_context = self.app.app_context() self.client = self.app.test_client() self.app_context.push() self.db = db self.db.drop_all() self.db.create_all() self.user = dict(username="******", password="******", first_name="Test", last_name="User", _admin=True) self.document = dict(title="This is a Test Title", body="Body Body Body, likeasomebody") self.tag = {"title": "TAGGY"} self.default_user = User.create(self.user) self.default_document = Document.create(self.document) self.default_document.user = self.default_user self.tag = Tag.create(self.tag) self.tag.user = self.default_user self.default_document.tags.append(self.tag) self.db.session.commit() self.redis_store = RedisStore(store=FakeStrictRedis, name='test') token = jwt.create_token_for_user(self.default_user) self.headers = [('Content-Type', 'application/json'), ('Authorization', 'Bearer %s' % token)]
def setUp(self): super(BaseTestCase, self).setUp() self.app = create_app(config.Testing) self.app_context = self.app.app_context() self.client = self.app.test_client() self.app_context.push() self.db = db self.db.drop_all() self.db.create_all() self.user = dict( username="******", password="******", first_name="Test", last_name="User", _admin=True ) self.document = dict( title="This is a Test Title", body="Body Body Body, likeasomebody" ) self.tag = {"title": "TAGGY"} self.default_user = User.create(self.user) self.default_document = Document.create(self.document) self.default_document.user = self.default_user self.tag = Tag.create(self.tag) self.tag.user = self.default_user self.default_document.tags.append(self.tag) self.db.session.commit() self.redis_store = RedisStore(store=FakeStrictRedis, name='test') token = jwt.create_token_for_user(self.default_user) self.headers = [ ('Content-Type', 'application/json'), ('Authorization', 'Bearer %s' % token) ]
def create_tag(): user = api.helpers.get_user() data = tag_schema.load(request.get_json()).data data["user"] = user tag = Tag.create(data) xhr = MakeResponse(201, tag.to_dict()) return xhr.response
def test_delete_tag_no_access(self): tag = Tag.create(dict(title="TAGGY")) res = self.client.delete('/tags/%s' % tag.id, headers=self.headers) self.assertStatus(res, 401) self.assertIsNotNone(Tag.query.get(tag.id))
def test_get_tag_no_access(self): tag = Tag.create(dict(title="TAGGY")) res = self.client.get("/tags/%s" % tag.id, headers=self.headers) self.assertStatus(res, 401)