Re: mocking out a Model

2012-04-15 Thread HarpB
I would recommend that you don't use the patch decorator, but rather the 
patch function itself. Here is a wrapper function I used when patching 
classes and functions (assuming you are using TestCase class):
def patch(self, *args):
if len(args) == 2:
patcher = patch.object(args[0], args[1])
else:
patcher = patch(args[0])
instance = patcher.start()
self.addCleanup(patcher.stop)
return instance

The correct way to patch is  
@patch.object(apps.deps.house_factory, 'House')
or with the helper function
house_object = self.patch(apps.deps.house_factory, 'House')

Basically, the factory should be without quotes. Read for more info: 
http://www.voidspace.org.uk/python/mock/patch.html#where-to-patch


On Sunday, April 15, 2012 6:52:31 AM UTC-7, jonas wrote:
>
> >> @patch('apps.market.models.House')
> > 
> > Is create house in apps.market.models, or is it somewhere else? If
> > it's somewhere else, mock it relative to that file eg:
> > @patch('apps.some_file.House')
>
> def create_house can be found in apps/deps/house_factory.py
> Inside house_factory I ' from apps.market.models import House '
>
>
> I tried running the test like you said by patching it like:
> @patch('apps.deps.house_factory.House')
>
> But for some reason I'm getting a MemoryError by nose.
>
> I tried patching it with patch.object next,
> @patch.object('apps.deps.house_factory', 'House')
>
> Then I'm receiving a:
> AttributeError: apps.deps.house_factory does not have the attribute 'House'
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/uHDeXXl4XGsJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: basci question about queryset for retrieving top element

2012-04-16 Thread HarpB
Both patterns will produce same query. You can also use 
django-debug-toolbar, which gives ur debugsqlshell tool. So you can execute 
code from the commandline and can see what the query will be.

Django also has a code pattern for your case - for getting the first 
element, you can just use latest() 
https://docs.djangoproject.com/en/dev/ref/models/querysets/#latest
i.e. Model.objects.latest()

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/uUdOhhKJ2PQJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: basci question about queryset for retrieving top element

2012-04-17 Thread HarpB
Like this 
http://readthedocs.org/docs/django/en/latest/py-modindex.html?highlight=django 
or perhaps this http://djangoapi.quamquam.org/trunk/ ?

There is also a great book: http://www.djangobook.com/en/2.0/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/u7uK3YRLvy8J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: keep models simple ? where to put the business logic ?

2012-04-28 Thread HarpB
First of all, I think your function is too big if it is doing all of these 
things. Are you able to unit test the function? 

In my view, the logic for extract should not be in the model. I would 
create a class specifically for data extractions. I am assuming that the 
_store does not DB saves to the Feed model, so that logic should not be 
inside Feed. A model should only perform operations on its own model. If 
you want to do operations on 2 different models, then have a helper 
function to do so or put it in the manager of the model, not the model 
itself.

In your case, since the data is stored in 2 different storages, I would put 
the logic in a helper class inside helpers.py.
# helpers.py
class Feeder(object):
def store(data):
#  logix for storing to DB and any other storage.

@classmethod
def extract(cls, url, etag):
try:
#sub-logic here
return data, etag
except (.., ..) as ex:
# handle exceptions

@staticmethod
def extract_feed(feed)
data, etag = Feeder.extract(feed.url, feel.etag)
feed.update_etag(etag)

# models.py
class Feed():
def update_etag(self, etag):
if self.etag != etag:
self.etag = etag
self.save()

I strive for modular code and put business logic in its own classes. I unit 
test each code, so smaller the function, easier to test it and it is much 
more easier to use the code. In the above code, you could test it Feeder 
class without having a Feed model. As a side benefit, by having logic be 
independent of a specific model, you are able to much more easily use same 
piece of code for multiple purposes. i.e If you wanted to allow extracting 
for a specific url and etag from a VIEW or a specific feed from a view, you 
would be able to do both.

Overall, it comes down to what makes sense to you. Once you accept a 
certain way of coding, it is meant to PREVENT YOU from doing guess work as 
to where you placed a specific logic. So when a bug happens and need to 
find the code, you are not opening/closing files to find the logic, you 
know where you put the logic.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/r9aBzSOftAMJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: html5 + Django

2012-04-30 Thread HarpB
It is much better to use Apache for static files than Django. You can still 
run DJango for data validation, but all static content is typically served 
via Apache. In your  virtualhost, you should proxy the /static/ endpoint to 
the /static/ folder in Django app.

On Sunday, April 29, 2012 5:39:15 AM UTC-7, collectiveSQL wrote:
>
> Hi Everyone, 
>
> I'm working on a heavily animated web site using the html5 canvas tag. 
> Its mainly made of html, javascript and css static files and I'd like 
> to integrate the Google Identity Toolkit for an Oauth 2.0 account 
> chooser for signup and registration. 
>
> The first question is Django a good candidate for serving up mainly 
> static files and using a small Django app for authentication? 
>
> And secondly what performance impact would this have over straight 
> apache? 
>
> More info: 
>
> 1. Static web files such as html, javascript and css are stored on 
> Amazon AWS S3 
> 2. Data is loaded via oData using jsdata for animations 
> 3. Amazon AWS EC2 is used to scale apache web servers 
>
> Thanks in advance.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/p30N-x76AEgJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: How would I go about building an API converter?

2012-05-02 Thread HarpB
I am a big fan of TastyPie. But I don't understand your following 
requirement:

   - *Get/set (conversion) between different schemas* 
  - I.e.: No matter the input API, you can get it formatted in 
  whichever output API you request
   
Are you simply trying to create a unified interface for all of you existing 
APIs? If so, are they using Django models? TastyPie would only be useful 
for you if you have existing Django Models. You could still use it without 
having Django models, but then you would have to write lot more of the api 
code yourself. TastyPie is a full-featured, drop-in API system. It is meant 
to simply included into your existing code, your attach it to your Django 
model and it will serve the data associated to the models. It provides all 
of the RESTful options: GET, POST, DELETE and PUT. 


Django-Piston also makes use of django models, but it does not solely 
relies on it. It provides a very basic interface (in comparison to 
TastyPie) and you would need to write all of the RESTful request handles 
yourself.


Both TastyPie and Piston have authentication and authorization system.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/LOLDecKzQBEJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.