what's best approach permalink database with zf2 and doctrine2? - doctrine-orm

i'm building a web site with zf2 and doctrine. How can i save permalink to the my database? What's the best approach way? Should i save permalink in my entities?
//Product Doctirine Entity
public function setProdName($prodName){
$this->prodName = $prodName;
//is this right way?
$this->setSeoLink = urlhelper->url($prodName);
}

I suggest you to create your urls using hash, like
/this-is-the-product-title-xyz123.html
where xyz123 is the hash of your product.
Doing this you'll have these advantage
1) you can change your product title anytime, your product can always be reached even the search engine or links in other website has the old one, for instance both
/this-is-my-product-xyz123.html
and
/this-is-my-wonderful-product-xyz123.html
will work.
2) you are not showing the real id to people.
To accomplish this I used the HashId module, creating a factory service that return an already configurated object, then you can encrypt/descript using this code:
$hashids = $this->getServiceLocator()->get('my.service.hashid');
// decrypt having hash
$id = $hashids->decrypt($hash);
// encrypt having id
$hash = $hashids->encrypt($id);
The .html suffix is just as example, you can create your url as you wish.

Related

Django creating model instances - Validation before creation? In manager class? What is manager class for?

Say I have a class "Book" and I want to hit an API to verify the book exists before creating my model.
Do I create my "BookManager" class, override create, hit the api, and throw an exception if not valid or create if valid?
Then in Book I'd write objects = BookManager()
And create a book with.
new_book = Book.objects.create(name)?
Basically, this feels like a good way to organize my code, but I'm not sure if this is intended use for the Manager class as opposed to only modifying the queryset.
Additionally, does anyone have a good reference on how to structure your django rest framework app? Folder structure etc
I will start with very basics. I am assuming you api calls are simple get requests for now which you can achieve with python http package.
(I am assuming the api is a third party api for now)
you can define a simple view let say view name be : bookM
Next you have defined you model with lets say a primary key, book_name, other_attrs, date
now when you hit you api within this view you can get the response from get request
requests.get(url = URL, params = PARAMS)
With this if you find the response sent back with some text or null you can act on model as below :
book= BookSave(
name = "book1",
)
book.save()
If this is not the case you can save error message in python variable and display while rendering html
You can use this view as api and do ajax call from web page, as well in this case you can just return back messages

How do you perform a filtered database lookup in views?

I'm working with a database in Django. I'm familiar with the Django database API but am wondering how to interact with the data base inside views.py. Here's the relevant model:
class SlotFilling(models.Model):
originator = models.CharField(max_length=20)
empty_slot = models.BooleanField(default=False)
I'm trying to write an If statment in my program to check if empty_slot is True for a given originator. I'm thinking I might be able to use filter() to accomplish this. Any experience with the most efficient way to implement this?
If you are looking to query for originator all empty slots you can do something as following
SlotFilling.objects.filter(empty_slot=True, originator='someoriginator')
#comment code
Assuming originator is unique
originator_slot = SlotFlling.objects.get(originator='originator')
slot_value = originator_slot.empty_slot
You can use filter instead if originator is not unique, which would list you back all rows for particular originator
originator_slots = SlotFlling.objects.filter(originator='originator')
for originator_slot in originator_slots:
print originator_slot.empty_slot
Also please check out retrieving objects in DB API documentation as saying that you are familiar with it would be big overstatement :)

How to get Zomato restaurant ID using restaurant link?

I want to get details of a restaurant in Zomato. I have it's link as the input (https://www.zomato.com/mumbai/fantasy-the-cake-shop-kalyan?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1). By browsing the documentation of Zomato APIs, I didn't found a way to get it.
I tried searching for the restaurant using search API but it returns many results.
Any help will be appreciated
It's a two-step process:
Find out restaurant's city id, in your case, Mumbai's city id through the /cities API. It's a simple query search.
Use the city id from the above API call in the /search API, like, https://developers.zomato.com/api/v2.1/search?entity_type=city&entity_id=3&q=fantasy%20the%20cake%20shop%20kalyan
This would give all the basic information about a restaurant.
View the page's source and search for window.RES_ID
I had the same issue as you described. This Zomato's API approach is at least odd. It's almost immposible to GET any information about restaurant if you don't know res_id in advance and that's not possible to parse since Zomato will deny access.
This worked for me:
Obtain user-key from Zomato API Credentials (https://developers.zomato.com/api)
Search restaurant url via API (https://developers.zomato.com/api/v2.1/search?entity_id=84&entity_type=city&q=RESTAURANT_URL&category=7%2C9). The more specific you will be, the better results you'll get (This url is specified by city to Prague (ID = 84) and categories Daily menus (ID = 7) and Lunch (ID = 9). If there is possibility do specify city, category or cuisine, it helps, but should't be necessary. Don't forget to define GET method in headers.
Loop or filter through json results and search for the wanted url. You might need to use method valueOf() to search for the same url. Be careful, you might need to add "?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1" at the end of your wanted url so it has the same format. Check that through Zomato API Documentation page.
for (i in body.restaurants) {
var url_wanted = restaurant_url + '?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1'
var url_in_json = body.restaurants[i].restaurant.url;
if (url_wanted.valueOf() == url_in_json.valueOf()) {
var restaurant_id = body.restaurants[i].restaurant.id;
}
console.log('Voala! We have res_id:' + restaurant_id);
}
There you have it. It could be easier though.
Hope it helps!
once you have the url of the rseraunt's page you can simply look for a javascript object attribute named "window.RES_ID" and further use it in the api call.

SOLR Java APi - Adding multiple condition

I am using Solr API to index the records and for search functionality. I am using the following code to search through
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("q", "country_id:("+id+")");
I would like to add one more parameter like state_id and I would like to do logical AND/OR operations and depending on the result the records should be retrieved. I searched through Google, but could not find a way to combine the conditions. Is it possible through the SOLR api? Or am I doing something wrong?
You can make Your Query as Following.....
String qr="cstm_text:"+"devotional";
SolrQuery qry = new SolrQuery(qr);
qry.setIncludeScore(true);
qry.setShowDebugInfo(true);
qry.setRows(1);
qry.setFacet(true);
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("qt", "/spell");
params.set("spellcheck", "on");
params.set(MoreLikeThisParams.MLT,true);
qry.add(params);

Django query: field is substring

In Django I have my Site model that contains the field "base_url" that's the base url of the site. I've an object like this:
foo = Site(base_url="http://foo_base_url.com/")
foo.save()
I receive an url and I want to obtain the site object having this url. I would like to perform a query in django like this:
Site.objects.get(base_url__is_substring="http://foo_base_url.com/something_non_base_url")
How can I perform this query?
Thanx
edit:
It doesn't exist a pattern for base_url, my foo site can be:
foo = Site(base_url="http://foo.com/base/url/")
What you want is not provided by the Django ORM but you can use the where param described under the reference for QuerySet:
url = "http://foo_base_url.com/something_non_base_url"
Site.objects.extra(where=["%s LIKE CONCAT('%%',field,'%%')"], params=[url]).get()
Keep in mind that there is no standard method of concatenation across DMBS so if you migrate, you'll have to migrate this code.
The only portable method would be to filter it using Python:
sites = [site for site in Site.objects.all() if site.base_url in url]
although this is of course not ideal for huge data sets.