def json(location, rotate=False): """Create JSON dumps with all reviews. This command will create an archive for each license available on CB. Archives will be put into a specified directory (default is *dump*). """ create_path(location) current_app.json_encoder = DumpJSONEncoder print("Creating new archives...") for license in db_license.list_licenses(): safe_name = slugify(license["id"]) with tarfile.open(os.path.join(location, "critiquebrainz-%s-%s-json.tar.bz2" % (datetime.today().strftime('%Y%m%d'), safe_name)), "w:bz2") as tar: temp_dir = tempfile.mkdtemp() license_dir = os.path.join(temp_dir, safe_name) create_path(license_dir) # Finding entities that have reviews with current license entities = db_review.distinct_entities() for entity in entities: entity = str(entity) # Creating directory structure and dumping reviews dir_part = os.path.join(entity[0:1], entity[0:2]) reviews = db_review.list_reviews(entity_id=entity, license_id=license["id"], limit=None)[0] if reviews: rg_dir = '%s/%s' % (license_dir, dir_part) create_path(rg_dir) f = open('%s/%s.json' % (rg_dir, entity), 'w+') f.write(jsonify(reviews=[db_review.to_dict(r) for r in reviews]).data.decode("utf-8")) f.close() tar.add(license_dir, arcname='reviews') # Copying legal text tar.add(os.path.join("critiquebrainz", "data", "licenses", safe_name + ".txt"), arcname='COPYING') print(" + %s/critiquebrainz-%s-%s-json.tar.bz2" % (location, datetime.today().strftime('%Y%m%d'), safe_name)) shutil.rmtree(temp_dir) # Cleanup if rotate: print("Removing old sets of archives (except two latest)...") remove_old_archives(location, "critiquebrainz-[0-9]+-[-\w]+-json.tar.bz2", is_dir=False, sort_key=os.path.getmtime) print("Done!")
def test_distinct_entities(self): db_review.create( user_id=self.user.id, entity_id="e7aad618-fa86-3983-9e77-405e21796eca", entity_type="release_group", text="Awesome", is_draft=False, license_id=self.license["id"], ) db_review.create( user_id=self.user_2.id, entity_id="e7aad618-fa86-3983-9e77-405e21796eca", entity_type="release_group", rating=5, is_draft=False, license_id=self.license["id"], ) entities = db_review.distinct_entities() self.assertEqual(len(entities), 1)