Skip to content

Instantly share code, notes, and snippets.

@cheekybastard
Forked from maiksprenger/models.py
Last active December 12, 2015 01:59
Show Gist options
  • Save cheekybastard/4695227 to your computer and use it in GitHub Desktop.
Save cheekybastard/4695227 to your computer and use it in GitHub Desktop.
"""
https://groups.google.com/forum/#!topic/django-oscar/BMibGnLb6w0
I've replaced the Partner model. AbstractPartner already carries a ManyToMany field to users (which is not used as far as I can tell). My model instead got a OneToOne field to users and duck-typing to ensure compatibility with Oscar. https://gist.github.com/4690160
I create a Partner instance for every User via a post_save signal.
When a Product with StockRecord is created, Stockrecord.partner gets set to self.request.user.partner, and hence the connection is made.
The dirty work is overriding all the get_query_set functions in dashboard views to limit results to the logged in user. An example:
# views.py
class FilteredProductListView(ProductListView):
def get_queryset(self):
qs = super(FilteredProductListView, self).get_queryset()
partner = self.request.user.partner
return qs.filter(stockrecord__partner=partner)
So far, I haven't run into any issues with my solution.
"""
class Partner(models.Model):
"""
New partner class.
Note: 'name' and 'users' are dropped from Oscar's partner model.
"""
user = models.OneToOneField('auth.User')
def __unicode__(self):
return u'%s: %s' % (_('Fulfillment Partner'),
self.user)
@property
def name(self):
"""
Duck-typing to not break Oscar.
"""
return unicode(self)
@property
def users(self):
"""
Duck-typing to not break Oscar.
"""
return [self.user,]
class Meta(AbstractPartner.Meta):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment