def setUp(self): create_initial_content.INITIAL_ASSET_LOCATIONS = create_initial_content.INITIAL_ASSET_LOCATIONS__ORIGINAL.copy() for location in create_initial_content.INITIAL_ASSET_LOCATIONS__ORIGINAL: singleton_send(initialize_asset_storage, None, asset=AssetInfo.objects.get(location='base/' + location)) self.username = '******' self.password = '******' self.user = User.objects.create_user(username=self.username, email='*****@*****.**', password=self.password) self.account = UserAccount.objects.get(user=self.user)
def asset_clone(request, uuid): asset = AssetInfo.objects.get(uuid=uuid) ## Old method: ## # Use old owners, but make sure cloner is first in the list ## old_owners = asset.owners.split(',') ## new_owners = [request.account.uuid] + filter(lambda owner: owner != request.account.uuid, old_owners) new_owners = [request.account.uuid] clone_asset = AssetInfo.objects.create( location = asset.location, hash_value = asset.hash_value, type_x = asset.type_x, kb_size = asset.kb_size, comment = 'Clone of asset "%s"' % asset.comment, ) clone_asset.owners.add(request.account) clone_asset.save() # Clone dependencies for dep in asset.dependencies.all(): clone_asset.dependencies.add(dep) clone_asset.save() # Clone in the storage data = singleton_send(retrieve_asset, None, asset_uuid = asset.uuid) multiple_send(store_asset, None, asset = clone_asset, asset_file = File(data)) request.session['message'] = 'Asset successfully cloned.' return HttpResponseRedirect('/tracker/asset/view/%s/' % clone_asset.uuid)
def instance_unrequisition(request, uuid): instance = ServerInstance.objects.get(uuid=uuid) message = singleton_send(unrequisition_instance, request.account, instance=instance) request.session['message'] = message return HttpResponseRedirect('/tracker/account/')
def activity_requisition(request, uuid): activity = Activity.objects.get(uuid=uuid) message = singleton_send(requisition_instance, request.account, activity=activity) request.session['message'] = message return HttpResponseRedirect('/tracker/account/')
def asset_clone(request, uuid): asset = AssetInfo.objects.get(uuid=uuid) ## Old method: ## # Use old owners, but make sure cloner is first in the list ## old_owners = asset.owners.split(',') ## new_owners = [request.account.uuid] + filter(lambda owner: owner != request.account.uuid, old_owners) new_owners = [request.account.uuid] clone_asset = AssetInfo.objects.create( location=asset.location, hash_value=asset.hash_value, type_x=asset.type_x, kb_size=asset.kb_size, comment='Clone of asset "%s"' % asset.comment, ) clone_asset.owners.add(request.account) clone_asset.save() # Clone dependencies for dep in asset.dependencies.all(): clone_asset.dependencies.add(dep) clone_asset.save() # Clone in the storage data = singleton_send(retrieve_asset, None, asset_uuid=asset.uuid) multiple_send(store_asset, None, asset=clone_asset, asset_file=File(data)) request.session['message'] = 'Asset successfully cloned.' return HttpResponseRedirect('/tracker/asset/view/%s/' % clone_asset.uuid)
def need_initial_data(sender, **kwargs): from django.conf import settings if settings.DATABASE_NAME != ':memory:': # We are not testing, create the non-DB asset storage now need_more = singleton_send(initialize_asset_storage, None, asset=kwargs['instance']) if not need_more: post_save.disconnect(need_initial_data)
def download(request, uuid): asset = AssetInfo.objects.get(uuid=uuid) # TODO: Check permission to download this asset - add signal. Meanwhile allow all logged in users to read anything asset_file = singleton_send(retrieve_asset, None, asset_uuid = uuid) response = HttpResponse(FileWrapper(asset_file), content_type='application/octet-stream') response['Content-Disposition'] = 'attachment; filename=%s' % (uuid) # response['Content-Length'] = asset_file.tell() return response
def testInitialData(self): init_user = User.objects.get(email='*****@*****.**') self.assertEqual(init_user.is_active, False) # Do not allow this user to be logged in with init_asset = AssetInfo.objects.get(location='base/emptymap.tar.gz') init_asset_data = singleton_send(retrieve_asset, None, asset_uuid = init_asset.uuid) self.assertEquals(init_asset.kb_size, 1) # 1K, rounded up self.assertNotEquals(init_asset.hash_value, '') self.assertNotEquals(init_asset.hash_value, '?') self.assertEquals(len(init_asset.owners.all()), 1) init_account = UserAccount.objects.get(user=init_user) self.assertEquals(init_asset.owners.all()[0].uuid, init_account.uuid)
def download(request, uuid): asset = AssetInfo.objects.get(uuid=uuid) # TODO: Check permission to download this asset - add signal. Meanwhile allow all logged in users to read anything asset_file = singleton_send(retrieve_asset, None, asset_uuid=uuid) response = HttpResponse(FileWrapper(asset_file), content_type='application/octet-stream') response['Content-Disposition'] = 'attachment; filename=%s' % (uuid) # response['Content-Length'] = asset_file.tell() return response
def asset_getinfo(request): uuid = request.GET['asset_id'] base_asset = AssetInfo.objects.get(uuid=uuid) recurse = (request.GET.get('recurse') == '1') assets = {base_asset.uuid: base_asset} SEP = '$' # TODO: faster SQLs, and cache these - everyone playing the same map runs the same stuff if recurse: # Recurse all dependencies pool = [base_asset.uuid] while len(pool) > 0: temp_pool = pool pool = [] for asset_uuid in temp_pool: asset = assets[asset_uuid] for dep in asset.dependencies.all(): old_size = len(assets) assets[dep.uuid] = dep if len(assets) > old_size: pool.append(dep.uuid) asset_list = assets.values() # Rely on the order from now ret = [ ('asset_id', SEP.join([asset.uuid for asset in asset_list])), ('location', SEP.join([asset.location for asset in asset_list])), ('url', SEP.join([ (singleton_send(asset_download_redirect, None, uuid=asset.uuid) if asset.location[-1] != '/' else '') for asset in asset_list ]) ), # Directory locations do not have content, so no url for download ('hash', SEP.join([asset.hash_value for asset in asset_list])), ('type', SEP.join([asset.type_x for asset in asset_list])), ('dependencies', SEP.join([ ','.join([dep.uuid for dep in asset.dependencies.all()]) for asset in asset_list ])), ] return HttpResponse(urllib.urlencode(ret))
def asset_getinfo(request): uuid = request.GET['asset_id'] base_asset = AssetInfo.objects.get(uuid=uuid) recurse = (request.GET.get('recurse') == '1') assets = { base_asset.uuid: base_asset } SEP = '$' # TODO: faster SQLs, and cache these - everyone playing the same map runs the same stuff if recurse: # Recurse all dependencies pool = [base_asset.uuid] while len(pool) > 0: temp_pool = pool pool = [] for asset_uuid in temp_pool: asset = assets[asset_uuid] for dep in asset.dependencies.all(): old_size = len(assets) assets[dep.uuid] = dep if len(assets) > old_size: pool.append(dep.uuid) asset_list = assets.values() # Rely on the order from now ret = [ ('asset_id', SEP.join([asset.uuid for asset in asset_list])), ('location', SEP.join([asset.location for asset in asset_list])), ('url', SEP.join([ (singleton_send(asset_download_redirect, None, uuid=asset.uuid) if asset.location[-1] != '/' else '') for asset in asset_list ])), # Directory locations do not have content, so no url for download ('hash', SEP.join([asset.hash_value for asset in asset_list])), ('type', SEP.join([asset.type_x for asset in asset_list])), ('dependencies', SEP.join([ ','.join([dep.uuid for dep in asset.dependencies.all()]) for asset in asset_list ])), ] return HttpResponse(urllib.urlencode(ret))
def asset_download(request, uuid): return HttpResponseRedirect(singleton_send(asset_download_redirect, None, uuid=uuid))
def testGetAssetInfo(self): self.doLogin() asset_id = self.createAsset() asset = AssetInfo.objects.get(uuid=asset_id) self.assertRedirects(self.client.post('/tracker/asset/view/%s/' % asset_id, { 'location': '/some/loc/', # Directory asset - so no content or url 'owners': [owner.pk for owner in asset.owners.all()], 'dependencies': [], 'type_x': AssetInfo.TYPE.Both, 'comment': 'hrm', }), '/tracker/asset/view/%s/' % asset_id) asset = AssetInfo.objects.get(uuid=asset_id) # Get the asset info response = self.client.get('/asset/getinfo', { 'asset_id': asset_id, }) self.assertEquals(response.status_code, 200) parsed = cgi.parse_qs(response.content) self.assertEquals(parsed['location'][0], asset.location) self.assertTrue('url' not in parsed) # And now with a non-directory asset asset = AssetInfo.objects.get(uuid=asset_id) self.assertRedirects(self.client.post('/tracker/asset/view/%s/' % asset_id, { 'location': '/some/loc', 'owners': [owner.pk for owner in asset.owners.all()], 'dependencies': [], 'type_x': AssetInfo.TYPE.Both, 'comment': 'hrm', }), '/tracker/asset/view/%s/' % asset_id) asset = AssetInfo.objects.get(uuid=asset_id) # Get the asset info response = self.client.get('/asset/getinfo', { 'asset_id': asset_id, }) self.assertEquals(response.status_code, 200) parsed = cgi.parse_qs(response.content) self.assertEquals(parsed['location'][0], asset.location) self.assertEquals(parsed['url'][0], singleton_send(asset_download_redirect, None, uuid=asset_id)) self.assertTrue('hash' not in parsed) # Hash value of '' is simply 'not there' in the parsed self.assertTrue('dependencies' not in parsed) # No deps self.assertEquals(parsed['type'][0], asset.type_x) # Test with dep dep_asset_id = self.createAsset() asset.dependencies.add(AssetInfo.objects.get(uuid=dep_asset_id)) asset.save() response = self.client.get('/asset/getinfo', { 'asset_id': asset_id, }) self.assertEquals(response.status_code, 200) parsed = cgi.parse_qs(response.content) self.assertEquals(parsed['dependencies'][0], dep_asset_id) # Test with 2 deps - output as comma separated dep2_asset_id = self.createAsset() self.assertNotEquals(dep_asset_id, dep2_asset_id) dep2_asset_id = self.createAsset() asset.dependencies.add(AssetInfo.objects.get(uuid=dep2_asset_id)) asset.save() response = self.client.get('/asset/getinfo', { 'asset_id': asset_id, }) self.assertEquals(response.status_code, 200) parsed = cgi.parse_qs(response.content) deps = parsed['dependencies'][0].split(',') self.assertEquals(set((dep_asset_id, dep2_asset_id)), set(deps)) # Order doesn't matter # Test 'recurse' with 2 deps - should get info on them all response = self.client.get('/asset/getinfo', { 'asset_id': asset_id, 'recurse': '1', }) self.assertEquals(response.status_code, 200) parsed = cgi.parse_qs(response.content) self.assertEquals(set((asset_id, dep_asset_id, dep2_asset_id)), set(parsed['asset_id'][0].split('$'))) self.assertEquals(len(set(parsed['location'][0].split('$'))), 3) self.assertTrue(asset.location in parsed['location'][0].split('$')) # Test hash asset.hash_value = 'xYz' asset.save() response = self.client.get('/asset/getinfo', { 'asset_id': asset_id, }) self.assertEquals(response.status_code, 200) parsed = cgi.parse_qs(response.content) self.assertEquals(parsed['hash'][0], asset.hash_value) self.doLogout()
def asset_download(request, uuid): return HttpResponseRedirect( singleton_send(asset_download_redirect, None, uuid=uuid))
def tearDown(self): self.user.delete() singleton_send(destroy_asset_storage, None)