Custom searching using tastypie - tastypie

actually we have 2 models
class Event(Basetable):
title = models.CharField(max_length=150)
event_description = models.TextField()
image = ThumbnailerImageField(upload_to=get_eventlogo_path, resize_source=dict(size=(700, 0), crop='smart'),)
category = models.ManyToManyField("EventCategory")
venue = models.ForeignKey(Venue,null=True)
class Venue(models.Model):
venue = models.CharField(max_length=100)
address1 = models.CharField(max_length=200)
address2 = models.CharField(max_length=200,blank=True)
slug = models.CharField(max_length=150,blank=True,null=True)
country = models.ForeignKey(Countries,null=True,blank=True,on_delete=models.SET_NULL)
state = models.ForeignKey(States,null=True,on_delete=models.SET_NULL)
city = models.ForeignKey(City,null=True,on_delete=models.SET_NULL)
tastypie code
class EventResource(ModelResource):
venue = fields.ForeignKey(VenueResource, 'venue')
class Meta:
queryset = Event.objects.filter(status='P').order_by('-id').distinct()
resource_name = 'eventlist'
filtering = {
"slug": ('exact', 'startswith',),
"title": ALL,
}
my problem is i need to get event result using city which is in venue
ex www.example.com/api/v1/eventlist/?format=json&city="chicago" but it is not coming help me

You have to allow filtering by venue:
filtering = { 'venue': ALL_WITH_RELATIONS
}
This link will work.
www.example.com/api/v1/eventlist/?format=json&venue_city_name="chicago"

Related

Return Django ORM union result in Django-Graphene

I am trying to query two separate objects and return them as a single result set. I've tried using a Union and an Interface, but not sure what I'm doing wrong there.
My model:
class BaseActivity(models.Model):
class Meta:
abstract = True
name = models.CharField(db_column="activity_type_name", unique=True, max_length=250)
created_at = CreationDateTimeField()
modified_at = ModificationDateTimeField()
created_by = models.UUIDField()
modified_by = models.UUIDField()
deleted_at = models.DateTimeField(blank=True, null=True)
deleted_by = models.UUIDField(blank=True, null=True)
class Activity(BaseActivity):
class Meta:
db_table = "activity_type"
ordering = ("sort_order",)
id = models.UUIDField(
db_column="activity_type_id",
primary_key=True,
default=uuid.uuid4,
editable=False,
)
sort_order = models.IntegerField()
def __str__(self):
return self.name
class CustomActivity(BaseActivity):
class Meta:
db_table = "organization_custom_activity_type"
id = models.UUIDField(
db_column="organization_custom_activity_type",
primary_key=True,
default=uuid.uuid4,
editable=False,
)
farm = models.ForeignKey("farm.Farm", db_column="organization_id", on_delete=models.DO_NOTHING, related_name="custom_activity_farm")
My schema:
class FarmActivities(graphene.ObjectType):
id = graphene.String()
name = graphene.String()
class ActivityType(DjangoObjectType):
class Meta:
model = Activity
fields = ("id", "name", "requires_crop", "sort_order")
class CustomActivityType(DjangoObjectType):
class Meta:
model = CustomActivity
fields = ("id", "name", "farm")
And the query:
class Query(graphene.ObjectType):
get_farm_activities = graphene.Field(FarmActivities, farm=graphene.String(required=True))
def resolve_get_farm_activities(self, info, farm):
farm_activities = Activity.objects.values("id", "name").filter(
farmtypeactivityrel__farm_type__farm=farm, deleted_at=None
)
custom_activities = CustomActivity.objects.values("id", "name").filter(farm=farm, deleted_at=None)
return list(chain(farm_activities, custom_activities))
With this, I do get a list back from the query, but it's not going thru the resolver when I call getFarmActivities.
Literally the list returns:
ExecutionResult(data={'getFarmActivities': {'id': None, 'name': None}}, errors=None)
How to resolve graphene.Union Type?
That provided the hint I needed to get this working. I had to build a Union that would parse the model and not the schema type.
class FarmActivities(graphene.Union):
class Meta:
types = (ActivityType, CustomActivityType)
#classmethod
def resolve_type(cls, instance, info):
if isinstance(instance, Activity):
return ActivityType
if isinstance(instance, CustomActivity):
return CustomActivityType
return FarmActivities.resolve_type(instance, info)
Which then allowed me to run the query like so:
def resolve_get_farm_activities(self, info, farm):
farm_activities = Activity.objects.filter(
farmtypeactivityrel__farm_type__farm=farm
)
custom_activities = CustomActivity.objects.filter(farm=farm)
return list(chain(farm_activities, custom_activities))
And the API query:
query farmParam($farm: String!)
{
getFarmActivities(farm: $farm)
{
... on CustomActivityType
{
id
name
}
... on ActivityType
{
id
name
}
}
}

Swift Codable: How do I make multiple objects from embedded data

Say I have JSON like this:
["conferences": <__NSArrayI 0x60c00002f720>(
{
alias = Conference1;
divisions = (
{
alias = "Division 1";
id = "b95cd27d-d631-4fe1-bc05-0ae47fc0b14b";
name = "Division 1";
teams = (
{
alias = OXC;
id = "768c92aa-75ff-4a43-bcc0-f2798c2e1724";
market = East;
name = Rams;
references = (
{
id = Rams;
origin = gsis;
}
);
{
alias = AXC;
id = "768c92aa-75ff-4a43-bcc0-f2798c2e1724";
market = East;
name = Platypus;
references = (
{
id = Platypus;
origin = gsis;
}
);
},
{
alias = "Division 2";
id = "b95cd27d-d631-4fe1-bc05-0ae47fc0b14b";
name = "Division 2";
teams = (
{
alias = NXC;
id = "768c92aa-75ff-4a43-bcc0-f2798c2e1724";
market = West;
name = Ants;
references = (
{
id = Ants;
origin = gsis;
}
);
{
alias = QXC;
id = "768c92aa-75ff-4a43-bcc0-f2798c2e1724";
market = West;
name = Bulls;
references = (
{
id = Bulls;
origin = gsis;
}
);
},
}]
I'm at a lost as to how to create an object that contains the team data but also the Conference and Division alias.
I started down this path but this seems like it'll just basically dump all the json onto the object. I don't want that overhead obviously.
struct Team:Codable {
var arrConference:[Conference]
private enum CodingKeys: String, CodingKey {
case arrConference = "conferences"
}
struct Conference:Codable {
var conferenceName:String
var conferenceID:String
private enum CodingKeys: String, CodingKey {
case conferenceName = "alias"
case conferenceID = "id"
}
}
}
I guess I am stuck thinking in an old way, but how do you enumerate through the data with a codable?

What steps to get rid of Collection was modified; enumeration operation may not execute. Error?

Our programming involves some Mock testing using In-Memory Data. Therefore, we implemented the following code that would first create In-Memory Data of Customer objects
// Let us create some in-memory data
// Create a list of Customer
List<Customer> listOfCustomers = new List<BlahBlahExample.Domain.Objects.Customer>()
{ new Customer { CustomerID = "1 ",Orders = new HashSet<Order>(), CustomerDemographics = new HashSet<CustomerDemographic>(), CompanyName = "Chicago Bulls", ContactName = "Michael Jordan", ContactTitle = "top basket ball player", Address = "332 testing lane", City = "Chicago", Region = "Illinois", PostalCode = "484894", Country = "USA", Phone = "3293993", Fax = "39393" },
new Customer { CustomerID = "2 ",Orders = new HashSet<Order>(),CustomerDemographics = new HashSet<CustomerDemographic>() , CompanyName = "Miami Heat", ContactName = "Lebron James", ContactTitle = "second best basket ball player", Address = "90 test street", City = "Miami", Region = "Florida", PostalCode = "4869394", Country = "USA", Phone = "3293213", Fax = "33393" },
new Customer { CustomerID = "3 ",Orders = new HashSet<Order>(),CustomerDemographics = new HashSet<CustomerDemographic>() , CompanyName = "Oklahoma City Thunder", ContactName = "Kevin Durant", ContactTitle = "current top basket ball player", Address = "35 test row", City = "Oklahoma City", Region = "Oklahoma", PostalCode = "480290", Country = "USA", Phone = "304923", Fax = "33325" }
};
// Convert the list to an IQueryable list
IQueryable<Customer> queryableListOfCustomerInMemoryData = listOfCustomers.AsQueryable();
// Let us create a Mocked DbSet object.
Mock<DbSet<BlahBlahExample.Domain.Objects.Customer>> mockDbSet = new Mock<DbSet<BlahBlahExample.Domain.Objects.Customer>>();
// Force DbSet to return the IQueryable members
// of our converted list object as its
// data source
mockDbSet.As<IQueryable<BlahBlahExample.Domain.Objects.Customer>>().Setup(m => m.Provider).Returns(queryableListOfCustomerInMemoryData.Provider);
mockDbSet.As<IQueryable<BlahBlahExample.Domain.Objects.Customer>>().Setup(m => m.Expression).Returns(queryableListOfCustomerInMemoryData.Expression);
mockDbSet.As<IQueryable<BlahBlahExample.Domain.Objects.Customer>>().Setup(m => m.ElementType).Returns(queryableListOfCustomerInMemoryData.ElementType);
mockDbSet.As<IQueryable<BlahBlahExample.Domain.Objects.Customer>>().Setup(m => m.GetEnumerator()).Returns(queryableListOfCustomerInMemoryData.GetEnumerator());
mockDbSet.Setup(m => m.Add(It.IsAny<Customer>())).Callback<Customer>(listOfCustomers.Add);
Mock<BlahBlahAuditMappingProvider> jsAudtMppngPrvdr = new Mock<BlahBlahAuditMappingProvider>();
Mock<BlahBlahDataContext> fctry = new Mock<BlahBlahDataContext>(jsAudtMppngPrvdr.Object);
Mock<BlahBlahDataContext> qryCtxt = new Mock<BlahBlahDataContext>();
Mock<BlahBlahAuditContext> audtCtxt = new Mock<BlahBlahAuditContext>();
Mock<BlahBlahDataContext> mockedReptryCtxt = new Mock<BlahBlahDataContext>();
mockedReptryCtxt.Setup(q => q.Customers).Returns(mockDbSet.Object);
mockedReptryCtxt.Setup(q => q.Set<Customer>()).Returns(mockDbSet.Object);
mockedReptryCtxt.CallBase = true;
DbSet<Customer> inMemoryDbSetCustomer = mockedReptryCtxt.Object.Set<Customer>();
In the next excerpt of code( which is our "Code Under Test"), I add a new Customer to the existing In-Memory Data, and then Invoke SaveChanges on the Mocked Object.
Customer returnCust = (Customer)(mockedReptryCtxt.Object.Set<Customer>().Add(new Customer { CustomerID = "4 ", Orders = new HashSet<Order>(), CustomerDemographics = new HashSet<CustomerDemographic>(), CompanyName = "Kolkota Knights", ContactName = "Sachin Tendulkar", ContactTitle = "current top cricket player", Address = "35 test row", City = "Kolkota", Region = "West Bengal", PostalCode = "3454534", Country = "India", Phone = "304923", Fax = "33325" }));
mockedReptryCtxt.Object.SaveChanges();
Later on in the code, I have the following excerpt of code where _context.Set() will return the In-Memory Data DBSet that we created previously
var query = _context.Set<TEntity>().AsQueryable();
if (typeof(TEntity).Name.Contains("Audit"))
{
return query;
}
if (includes != null && includes.Any())
{
foreach (var include in includes)
{
query = query.Include(include);
}
}
List<TEntity> resultsAsList = query.ToList(); // Error Thrown When using ToList()
var results = resultsAsList.AsQueryable();
When we invoke ToList(), it Throws the following Error:
System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=Collection was modified; enumeration operation may not execute.
Source=mscorlib
StackTrace:
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at System.Collections.Generic.List`1.Enumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at BlahBlah.Framework.EntityFramework.EntityFrameworkRepository`1.ConcreteQuery(List`1 includes) in d:\EMIS\BlahBlah Framework\BlahBlahFrameworkLightweight\BlahBlah.Framework.EntityFramework\EntityFrameworkRepository.c s:line 51
at Castle.Proxies.EntityFrameworkRepository`1Proxy.ConcreteQuery_callback(List`1 includes)
at Castle.Proxies.Invocations.EntityFrameworkRepository`1_ConcreteQuery.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Moq.Proxy.CastleProxyFactory.CallContext.InvokeBase()
at Moq.InvokeBase.HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx)
at Moq.Interceptor.Intercept(ICallContext invocation)
at Moq.Proxy.CastleProxyFactory.Interceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.EntityFrameworkRepository`1Proxy.ConcreteQuery(List`1 includes)
at BlahBlah.Framework.Core.Repository.BaseRepository`1.Query(List`1 includes) in d:\EMIS\BlahBlah Framework\BlahBlahFrameworkLightweight\BlahBlah.Framework.Core\Repository\BaseRepository.cs:line 149
at Castle.Proxies.EntityFrameworkRepository`1Proxy.Query_callback(List`1 includes)
at Castle.Proxies.Invocations.IRepository`1_Query.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Moq.Proxy.CastleProxyFactory.CallContext.InvokeBase()
at Moq.InvokeBase.HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx)
at Moq.Interceptor.Intercept(ICallContext invocation)
at Moq.Proxy.CastleProxyFactory.Interceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.EntityFrameworkRepository`1Proxy.Query(List`1 includes)
at BlahBlah.Test.Unit.CntrlrsTests.CustomerControllerTest.Test_Creation_Of_Customer_Using_Constructor_Of _Customer_Controller_That_Expects_Arguments() in d:\EMIS\BlahBlah Framework\BlahBlahFrameworkLightweight\BlahBlah.Test.Unit\CntrlrsTests\CustomerControllerTest.cs:line 278
InnerException:
What steps do we need to take in order to stop the said error from being thrown( preferably without Changing too much of our Code Under Test)?
I had this problem as well but not iterating over the collection isn't really an option for me. After some thought, I did figure out a solution. The issue is that the mock sets up the various IQueryable properties off of a fixed IQueryable object from the original list. That causes any modification of that list to invalidate the corresponding IQueryable. The solution is to get a new IQueryable on each access using a lambda with Moq.
Here's the helper function I created to make mocking out DBSets easier, using the described technique.
public static Mock<DbSet<T>> MockDbSet<T>(List<T> list) where T : class
{
var mockSet = new Mock<DbSet<T>>();
mockSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(() => list.AsQueryable().Provider);
mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(() => list.AsQueryable().Expression);
mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(() => list.AsQueryable().ElementType);
mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(() => list.GetEnumerator());
mockSet.Setup(m => m.Add(It.IsAny<T>())).Callback((T x) => list.Add(x));
mockSet.Setup(m => m.AddRange(It.IsAny<IEnumerable<T>>())).Callback((IEnumerable<T> x) => list.AddRange(x));
mockSet.Setup(m => m.Remove(It.IsAny<T>())).Callback((T x) => list.Remove(x));
mockSet.Setup(m => m.RemoveRange(It.IsAny<IEnumerable<T>>())).Callback((IEnumerable<T> x) => list.RemoveAll(x.Contains));
return mockSet;
}
Edit: Added AddRange, Remove, RemoveRange since why not...
Edit 2: Correction for RemoveRange
I found a really Clumsy Solution:
List<TEntity> tempList = new List<TEntity>();
for (int i = query.Count() - 1; i >= 0; i--)
{
tempList.Add(query.ElementAt(i));
}
List<TEntity> resultsAsList = tempList.ToList();
var results = resultsAsList.AsQueryable();
In the aforementioned code, it is important to use a for loop with an index to go through the DBSet instance. Furthermore, in the loop, you add each element to a List. ( Basically, it's important to Avoid using the Iterator)

Why is the DBSet Add failing, and also destroying the existing Customer entries in the DBSet?

Our programming involves some Mock testing using In Memory Data.
// Let us create some in-memory data
// Create a list of Customer
List<Customer> listOfCustomers = new List<BlahProjectBlahExample.Domain.Objects.Customer>()
{ new Customer { CustomerID = "1 ", CompanyName = "Chicago Bulls", ContactName = "Michael Jordan", ContactTitle = "top basket ball player", Address = "332 testing lane", City = "Chicago", Region = "Illinois", PostalCode = "484894", Country = "USA", Phone = "3293993", Fax = "39393" },
new Customer { CustomerID = "2 ", CompanyName = "Miami Heat", ContactName = "Lebron James", ContactTitle = "second best basket ball player", Address = "90 test street", City = "Miami", Region = "Florida", PostalCode = "4869394", Country = "USA", Phone = "3293213", Fax = "33393" },
new Customer { CustomerID = "3 ", CompanyName = "Oklahoma City Thunder", ContactName = "Kevin Durant", ContactTitle = "current top basket ball player", Address = "35 test row", City = "Oklahoma City", Region = "Oklahoma", PostalCode = "480290", Country = "USA", Phone = "304923", Fax = "33325" }
};
// Convert the list to an IQueryable list
IQueryable<Customer> queryableListOfCustomerInMemoryData = listOfCustomers.AsQueryable();
// Let us create a Mocked DbSet object.
Mock<DbSet<BlahProjectBlahExample.Domain.Objects.Customer>> mockDbSet = new Mock<DbSet<BlahProjectBlahExample.Domain.Objects.Customer>>();
// Force DbSet to return the IQueryable members
// of our converted list object as its
// data source
mockDbSet.As<IQueryable<BlahProjectBlahExample.Domain.Objects.Customer>>().Setup(m => m.Provider).Returns(queryableListOfCustomerInMemoryData.Provider);
mockDbSet.As<IQueryable<BlahProjectBlahExample.Domain.Objects.Customer>>().Setup(m => m.Expression).Returns(queryableListOfCustomerInMemoryData.Expression);
mockDbSet.As<IQueryable<BlahProjectBlahExample.Domain.Objects.Customer>>().Setup(m => m.ElementType).Returns(queryableListOfCustomerInMemoryData.ElementType);
mockDbSet.As<IQueryable<BlahProjectBlahExample.Domain.Objects.Customer>>().Setup(m => m.GetEnumerator()).Returns(queryableListOfCustomerInMemoryData.GetEnumerator());
Mock<BlahProjectBlahDataContext> mockedReptryCtxt = new Mock<BlahProjectBlahDataContext>();
mockedReptryCtxt.Setup(q => q.Customers).Returns(mockDbSet.Object);
mockedReptryCtxt.Setup(q => q.Set<Customer>()).Returns(mockDbSet.Object);
mockedReptryCtxt.CallBase = true;
DbSet<Customer> inMemoryDbSetCustomer = mockedReptryCtxt.Object.Set<Customer>();
In the following code, I used a loop to see the contents of the inMemoryDbSetCustomer, and it contained the expected data.
Customer something;
foreach (var entry in inMemoryDbSetCustomer)
{
something = entry as Customer;
}
Sadly, when I try to add a new customer,
1) the inMemoryDbSetCustomer fails to add the customer
2) the inMemoryDbSetCustomer.Add(someCust) returns NULL
3) the inMemoryDbSetCustomer seems to have lost all it's other Customer entries.
Customer someCust = new Customer { CustomerID = "4 ", CompanyName = "Kolkota Knights", ContactName = "Sachin Tendulkar", ContactTitle = "current top cricket player", Address = "35 test row", City = "Kolkota", Region = "West Bengal", PostalCode = "3454534", Country = "India", Phone = "304923", Fax = "33325" };
try
{
Customer returnCust = (Customer)(inMemoryDbSetCustomer.Add(someCust));
}
catch(Exception ex ){
}
Why is the DBSet Add failing, and also destroying the existing Customer entries in the DBSet?
Update With Answer
Thanks to #Werlang suggestion. The following code addition helped:
mockDbSet.Setup(m => m.Add(It.IsAny<Customer>()))
.Callback<Customer>((Customer c) => { listOfCustomers.Add(c); })
.Returns((Customer c) => c);
Also, if you mocking DBSets with Moq Framework like I am then the following stackoverflow posting will be helpful because you might face a similar problem:
What steps to get rid of Collection was modified; enumeration operation may not execute. Error?
You've mocked the queryable to return a fixed set of data, when accessed via IQueryable. But Add's are maintained in a separated list on DbContext internal in the form of DbEntry's.
I suggest you to mock the Add() method too, addind the new element to mocked array (listOfCustomers).

Tasty pie search

i have two models
class Business(Basetable):
name = models.CharField(max_length=120)
slug = models.SlugField(max_length=150)
logo=models.OneToOneField("BusinessLogo",null=True,on_delete=models.SET_NULL)
class Address(models.Model):
business = models.ForeignKey("Business", related_name='biz_address')
address1 = models.CharField(max_length=100,null=True)
address2 = models.CharField(max_length=100,null=True)
state = models.ForeignKey(States,null=True,on_delete=models.SET_NULL)
city = models.ForeignKey(City,null=True,on_delete=models.SET_NULL)
view.py
class BusinessResource(ModelResource):
class Meta:
queryset = Business.objects.filter(status='P').order_by('-id')
resource_name = 'business'
filtering = {
'is_black_business': ALL,
'city': ALL_WITH_RELATIONS,
}
def dehydrate(self,bundle):
if buss_address.city:bundle.data['city'] = buss_address.city.name
else:bundle.data['city'] = ''
// i am getting city here
problem is i need to filter the business using city .. how can i do that ? any guess
class BusinessResource(ModelResource):
addresses = fields.ToManyField(AddressResource, 'biz_address')
class Meta:
filtering = {
'addresses': ALL_WITH_RELATIONS,
}
class AddressResource(ModelResource):
city = fields.ToOneField(CityResource, 'city')
class Meta:
filtering = {
'city': ALL_WITH_RELATIONS,
}
Second way is to write custom filter by overriding build_filters and apply_filters methods.