def test_get_to_many_uris_with_special_othersidename(self): data = api.get_resource('agent', self.agent.id) self.assertEqual(data['collectors'], api.uri_for_model('collector') + '?agent=%d' % self.agent.id) self.assertEqual(data['orgmembers'], api.uri_for_model('agent') + '?organization=%d' % self.agent.id)
def test_get_to_many_uris_with_special_othersidename(self): data = api.get_resource('agent', self.agent.id) self.assertEqual( data['collectors'], api.uri_for_model('collector') + '?agent=%d' % self.agent.id) self.assertEqual( data['orgmembers'], api.uri_for_model('agent') + '?organization=%d' % self.agent.id)
def test_get_to_many_uris_with_special_othersidename(self): data = api.get_resource('agent', self.agent.id) # This one is actually a regular othersidename self.assertEqual( data['collectors'], api.uri_for_model('collector') + '?agent=%d' % self.agent.id) # This one is the special otherside name ("organization" instead of "agent") self.assertEqual( data['orgmembers'], api.uri_for_model('agent') + '?organization=%d' % self.agent.id)
def test_get_recordset_info(self): ids = [co.id for co in self.collectionobjects] for id in ids: self.recordset.recordsetitems.create(recordid=id) for i, co in enumerate(self.collectionobjects): info = api.get_recordset_info(co, self.recordset.id) self.assertEqual(info['recordsetid'], self.recordset.id) self.assertEqual(info['total_count'], len(self.collectionobjects)) self.assertEqual(info['index'], i) self.assertEqual(info['previous'], None if i == 0 else \ api.uri_for_model('collectionobject', self.collectionobjects[i-1].id)) self.assertEqual(info['next'], None if i == len(self.collectionobjects) - 1 else \ api.uri_for_model('collectionobject', self.collectionobjects[i+1].id))
def test_create_object_with_inlines(self): data = { 'collection': api.uri_for_model('collection', self.collection.id), 'catalognumber': 'foobar', 'determinations': [{ 'iscurrent': False, 'number1': 1 }, { 'iscurrent': False, 'number1': 2 }], 'collectionobjectattribute': { 'text1': 'some text' } } obj = api.create_obj(self.collection, self.agent, 'collectionobject', data) co = models.Collectionobject.objects.get(id=obj.id) self.assertEqual( set(co.determinations.values_list('number1', flat=True)), set((1, 2))) self.assertEqual(co.collectionobjectattribute.text1, 'some text')
def test_post_bad_resource(self): with self.assertRaises(api.RecordSetException) as cm: obj = api.post_resource(self.collection, self.agent, 'Agent', {'agenttype': 0, 'lastname': 'MonkeyWrench', 'division': api.uri_for_model('division', self.division.id)}, recordsetid=self.recordset.id) self.assertEqual(models.Agent.objects.filter(lastname='MonkeyWrench').count(), 0)
def test_create_object(self): obj = api.create_obj(self.collection, self.agent, 'collectionobject', { 'collection': api.uri_for_model('collection', self.collection.id), 'catalognumber': 'foobar'}) obj = models.Collectionobject.objects.get(id=obj.id) self.assertTrue(obj.id is not None) self.assertEqual(obj.collection, self.collection) self.assertEqual(obj.catalognumber, 'foobar') self.assertEqual(obj.createdbyagent, self.agent)
def test_post_resource_to_bad_recordset(self): max_id = models.Recordset.objects.aggregate(Max('id'))['id__max'] with self.assertRaises(api.RecordSetException) as cm: obj = api.post_resource(self.collection, self.agent, 'Agent', {'agenttype': 0, 'lastname': 'Pitts', 'division': api.uri_for_model('division', self.division.id)}, recordsetid=max_id + 100) self.assertEqual(models.Agent.objects.filter(lastname='Pitts').count(), 0)
def test_delete_object(self): obj = api.create_obj( self.collection, self.agent, 'collectionobject', { 'collection': api.uri_for_model('collection', self.collection.id), 'catalognumber': 'foobar' }) api.delete_obj(self.agent, 'collectionobject', obj.id, obj.version) self.assertEqual( models.Collectionobject.objects.filter(id=obj.id).count(), 0)
def test_delete_object(self): obj = api.create_obj(self.collection, self.agent, 'collectionobject', { 'collection': api.uri_for_model('collection', self.collection.id), 'catalognumber': 'foobar'}) data = api.get_resource('collectionobject', obj.id) obj.version += 1 obj.save() with self.assertRaises(api.StaleObjectException) as cm: api.delete_obj(self.agent, 'collectionobject', data['id'], data['version']) self.assertEqual(models.Collectionobject.objects.filter(id=obj.id).count(), 1)
def test_post_resource(self): obj = api.post_resource( self.collection, self.agent, 'collectionobject', { 'collection': api.uri_for_model('collection', self.collection.id), 'catalognumber': 'foobar' }, recordsetid=self.recordset.id) self.assertEqual( self.recordset.recordsetitems.filter(recordid=obj.id).count(), 1)
def test_create_object_with_inlined_existing_resource(self): coa = models.Collectionobjectattribute.objects.create( collectionmemberid=self.collection.id) coa_data = api.get_resource('collectionobjectattribute', coa.id) co_data = { 'collection': api.uri_for_model('collection', self.collection.id), 'collectionobjectattribute': coa_data, 'catalognumber': 'foobar'} obj = api.create_obj(self.collection, self.agent, 'collectionobject', co_data) co = models.Collectionobject.objects.get(id=obj.id) self.assertEqual(co.collectionobjectattribute, coa)
def test_create_object(self): obj = api.create_obj( self.collection, self.agent, 'collectionobject', { 'collection': api.uri_for_model('collection', self.collection.id), 'catalognumber': 'foobar' }) obj = models.Collectionobject.objects.get(id=obj.id) self.assertTrue(obj.id is not None) self.assertEqual(obj.collection, self.collection) self.assertEqual(obj.catalognumber, 'foobar') self.assertEqual(obj.createdbyagent, self.agent)
def test_post_bad_resource(self): with self.assertRaises(api.RecordSetException) as cm: obj = api.post_resource( self.collection, self.agent, 'Agent', { 'agenttype': 0, 'lastname': 'MonkeyWrench', 'division': api.uri_for_model('division', self.division.id) }, recordsetid=self.recordset.id) self.assertEqual( models.Agent.objects.filter(lastname='MonkeyWrench').count(), 0)
def test_post_resource_to_bad_recordset(self): max_id = models.Recordset.objects.aggregate(Max('id'))['id__max'] with self.assertRaises(api.RecordSetException) as cm: obj = api.post_resource( self.collection, self.agent, 'Agent', { 'agenttype': 0, 'lastname': 'Pitts', 'division': api.uri_for_model('division', self.division.id) }, recordsetid=max_id + 100) self.assertEqual( models.Agent.objects.filter(lastname='Pitts').count(), 0)
def test_create_object_with_inlined_existing_resource(self): coa = models.Collectionobjectattribute.objects.create( collectionmemberid=self.collection.id) coa_data = api.get_resource('collectionobjectattribute', coa.id) co_data = { 'collection': api.uri_for_model('collection', self.collection.id), 'collectionobjectattribute': coa_data, 'catalognumber': 'foobar' } obj = api.create_obj(self.collection, self.agent, 'collectionobject', co_data) co = models.Collectionobject.objects.get(id=obj.id) self.assertEqual(co.collectionobjectattribute, coa)
def test_delete_object(self): obj = api.create_obj( self.collection, self.agent, 'collectionobject', { 'collection': api.uri_for_model('collection', self.collection.id), 'catalognumber': 'foobar' }) data = api.get_resource('collectionobject', obj.id) obj.version += 1 obj.save() with self.assertRaises(api.StaleObjectException) as cm: api.delete_resource(self.agent, 'collectionobject', data['id'], data['version']) self.assertEqual( models.Collectionobject.objects.filter(id=obj.id).count(), 1)
def test_create_object_with_inlines(self): data = { 'collection': api.uri_for_model('collection', self.collection.id), 'catalognumber': 'foobar', 'determinations': [{ 'iscurrent': False, 'number1': 1 }, { 'iscurrent': False, 'number1': 2 }], 'collectionobjectattribute': { 'text1': 'some text'}} obj = api.create_obj(self.collection, self.agent, 'collectionobject', data) co = models.Collectionobject.objects.get(id=obj.id) self.assertEqual(set(co.determinations.values_list('number1', flat=True)), set((1, 2))) self.assertEqual(co.collectionobjectattribute.text1, 'some text')
def make_recordset(request): try: recordset_info = json.load(request) except ValueError as e: return HttpResponseBadRequest(e) spquery = recordset_info['fromquery'] tableid = spquery['contexttableid'] with models.session_context() as session: recordset = models.RecordSet() recordset.timestampCreated = datetime.now() recordset.version = 0 recordset.collectionMemberId = request.specify_collection.id recordset.dbTableId = tableid recordset.name = recordset_info['name'] if 'remarks' in recordset_info: recordset.remarks = recordset_info['remarks'] recordset.type = 0 recordset.createdByAgentID = request.specify_user_agent.id recordset.SpecifyUserID = request.specify_user.id session.add(recordset) session.flush() new_rs_id = recordset.recordSetId model = models.models_by_tableid[tableid] id_field = getattr(model, model._id) field_specs = [ QueryField.from_spqueryfield(EphemeralField.from_json(data)) for data in sorted(spquery['fields'], key=lambda field: field['position']) ] query, __ = build_query(session, request.specify_collection, request.specify_user, tableid, field_specs) query = query.with_entities(id_field, literal(new_rs_id)).distinct() RSI = models.RecordSetItem ins = insert(RSI).from_select((RSI.recordId, RSI.RecordSetID), query) session.execute(ins) return HttpResponseRedirect(uri_for_model('recordset', new_rs_id))
def make_recordset(request): if settings.RO_MODE or request.specify_user.usertype not in ('Manager', 'FullAccess'): return HttpResponseForbidden() try: recordset_info = json.load(request) except ValueError as e: return HttpResponseBadRequest(e) spquery = recordset_info['fromquery'] tableid = spquery['contexttableid'] with models.session_context() as session: recordset = models.RecordSet() recordset.timestampCreated = datetime.now() recordset.version = 0 recordset.collectionMemberId = request.specify_collection.id recordset.dbTableId = tableid recordset.name = recordset_info['name'] if 'remarks' in recordset_info: recordset.remarks = recordset_info['remarks'] recordset.type = 0 recordset.createdByAgentID = request.specify_user_agent.id recordset.SpecifyUserID = request.specify_user.id session.add(recordset) session.flush() new_rs_id = recordset.recordSetId model = models.models_by_tableid[tableid] id_field = getattr(model, model._id) field_specs = [QueryField.from_spqueryfield(EphemeralField.from_json(data)) for data in sorted(spquery['fields'], key=lambda field: field['position'])] query, __ = build_query(session, request.specify_collection, request.specify_user, tableid, field_specs) query = query.with_entities(id_field, literal(new_rs_id)).distinct() RSI = models.RecordSetItem ins = insert(RSI).from_select((RSI.recordId, RSI.RecordSetID), query) session.execute(ins) return HttpResponseRedirect(uri_for_model('recordset', new_rs_id))
def test_post_resource(self): obj = api.post_resource(self.collection, self.agent, 'collectionobject', { 'collection': api.uri_for_model('collection', self.collection.id), 'catalognumber': 'foobar'}, recordsetid=self.recordset.id) self.assertEqual(self.recordset.recordsetitems.filter(recordid=obj.id).count(), 1)
def test_get_to_many_uris_with_regular_othersidename(self): data = api.get_resource('agent', self.agent.id) self.assertEqual(data['agentattachments'], api.uri_for_model('agentattachment') + '?agent=%d' % self.agent.id)
def test_delete_object(self): obj = api.create_obj(self.collection, self.agent, 'collectionobject', { 'collection': api.uri_for_model('collection', self.collection.id), 'catalognumber': 'foobar'}) api.delete_obj(self.agent, 'collectionobject', obj.id, obj.version) self.assertEqual(models.Collectionobject.objects.filter(id=obj.id).count(), 0)
def test_get_to_many_uris_with_regular_othersidename(self): data = api.get_resource('agent', self.agent.id) self.assertEqual( data['agentattachments'], api.uri_for_model('agentattachment') + '?agent=%d' % self.agent.id)
def test_get_to_many_uris_with_regular_othersidename(self): data = api.get_resource('collectingevent', self.collectingevent.id) self.assertEqual( data['collectionobjects'], api.uri_for_model('collectionobject') + '?collectingevent=%d' % self.collectingevent.id)