I've done only two small projects with pyramid, but from the beginning i've 
decided to go with traversal. because with traversal i don't have to decide 
about every urls of my site(which url must represent which resource and 
...).
So far my project structure is like this:
*Models:* i have a models folder which contains my models definitions(Duh!).
*Resources:* i have a resources folder which contains my root object. also 
for each model i have a resource file which has two classes: one for 
resource collection(i.e. *CustomersListResource*) and other one for single 
resource(i.e* CustomerResource*).
With pb help i figured out how to write my view/resource to minimize db 
queries. for example:
customer_rc.py:
...
class CustomersListResource(Resource):
    def __init__(self, name, parent):
        Resource.__init__(self, name, parent)

    def __getitem__(self, key):
        if key.is_digit():
           # check db if customer id exists:
           c_id = DBSession.query(models.Customer.id).filter(models.Customer
.id == int(key)).scalar()
           if c_id:
               return Customer(c_id, self, None)
        ...
        raise KeyError(key)

    def get_list(self):
        # Query and return all customers.
        customers = []
        for c in DBSession.query(models.Customer):
             customers.append(CustomerResource(c.id, self, c))
        return customers

class CustomerResource(Resource):
    def __init__(name, parent, model):
        Resource.__init__(self, name, parent)
        self.customer_model = model

    def __getitem__(self, key):
        if key == "orders":
            return OrdersListResource("orders", self)
        ...
    
    def load_model(self):
        self.customer_model = 
DBSession.query(models.Customer).get(self.__name__)

    def get_orders(self, year, month):
        ...

    def __json__(self, request):
        d = {}
        if self.customer_model is None:
            d["id"] = self.__name__
        else:
            # Fill up dict with all customer data needed to passed to 
client side.
        return d


And this is my view example:
@view_config(context=resources.CustomersListResource, renderer="json")
def customers_view(context, request):
    return {
        "context": context
    }


@view_config(context=resources.CustomersListResource, name="list", 
renderer="json")
def customers_list_view(context, request):
    return {
        "context": context.get_list()
    }

@view_config(context=resources.CustomerResource, renderer="json")
def customer_view(context, request):
    return {
        "context": context
    }

@view_config(context=resources.CustomerResource, name="get", 
renderer="json")
def customer_view(context, request):
    context.load_model()
    return {
        "context": context
    }

@view_config(context=resources.CustomerResource, name="orders", 
renderer="json")
def customer_view(context, request):
    year = request.params.get("year", None)
    month = request.params.get("month", None)
    return {
        "context": context,
        "orders": context.get_orders(year, month)
    }

I would like to know your opinions about this approach.
Thanks.



On Friday, August 28, 2015 at 2:31:51 PM UTC+4:30, pb wrote:
>
> Hi Mehdi,
>
> traversing your resource tree does not necessarily mean running any 
> queries. In
> your example, it wouldn't have to run any queries at all.
> Basically, it is there to bring you to the view and possibly gather some 
> information 
> on the way. For example (simplified and certainly not working):
>
> class TopContext(object):
>     def __init__(self, request):
>         self.request = request
>     def __getitem__(self, entity_name):
>         if "customers" == entity_name:
>             return CustomerContext(self.request)
>
> class CustomersContext(object):
>     def __init__(self, request):
>         self.request = request
>     def get_data(self):
>         # run the select and collect the data ...
>     def __getitem__(self, ent_id):
>         if ent_id.isdigit():
>             return OneCustomerContext(self.request)
>
> class OneCustomerContext(object):
>     def __init__(self, request):
>         self.request = request
>     def get_data(self):
>         # run the select and collect the data ...
>
> Now, when pyramid traverses the resource tree, it only uses constructors 
> and __getitem__,
> so if there are no queries in there, they are not run. And you usually do 
> not need them.
>
> If it was up to me, I would not define OrderContext. The traversal would 
> get me to 
> OneCustomerContext, and *order* would be the view name (parameter name of 
> view_config).
>
> --
> Petr
>
>
>
> On Fri, Aug 28, 2015 at 9:54 AM, Mehdi <[email protected] <javascript:>> 
> wrote:
>
>> Hi
>> Let's get straight to my question:
>> Consider this url: *http://mysite.com/customers/137/orders?year=2015&month=5 
>> <http://mysite.com/customers/137/orders?year=2015&month=5>*
>> Now in pyramid traversal mechanism, how many queries will be run against 
>> database?
>> I guess it would be three:
>> 1. */customers* :                             select * from customers
>> 2. */customers/137* :                         select * from customers 
>> where id = 137
>> 3. */customers/137/**orders?year=2015&month=5: *select * from orders 
>> where customer_id = 137 and year=:year and month=:month
>>
>> If so is there anyway to reduce the db queries down to one last query?
>> Thanks.
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "pylons-discuss" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> Visit this group at http://groups.google.com/group/pylons-discuss.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.

Reply via email to