0

I would like a Pythonic way of accomplishing the following task. To call a method in Campaign Model that returns a URL with the campaign ID and the current login user ID i.e. /campaign_id/user_id

To achieve this I need the request.user scope, but I don't think its a good idea to have this directly in my models. I have been reading Python can use getters and setters, would this be useful? So my idea is to have something like this...

campaign = Campaign.object.get(id=1)
campaign.set_current_user(request.user)

referral_url = campaign.get_referral_url()

which would give me /campaign_id/user_id/

Is this a good way?

My Model so far:

class Campaign(models.Model):
   name = models.CharField(max_length=120)

   @property
   def current_user(self):
       return self._current_user

   @current_user.setter
   def current_user(self, user_object):
      if user_object:
          return user_object.id
      else:
          return None


   def _build_referral_url(self):
        """
        Builds the full referral URL for the user.
        :return:
        """
        return self.name + "/" + self.current_user


    def get_referral_url(self):
        """
        :return: Campaign referral URL.
        """
        return self._build_referral_url()
Prometheus
  • 32,405
  • 54
  • 166
  • 302
  • What is `set_current_user` doing? How is the user referenced from the Campaign model? If you have a foreign key, you should show it. – Daniel Roseman Jun 10 '14 at 10:03
  • Please give more context. Why would you want to place the user ID in the URL? – Kos Jun 10 '14 at 10:04
  • @Kos updated code in OP. When I load campaign in my view I want to be able to get a custom link off the model which is build using the current login user. the user is not linked to the model nor do I want to access the request scope in the model. This is the best way I can think of at the moment. – Prometheus Jun 10 '14 at 10:09
  • 1
    But you haven't explained why you would do that. You always know the current user (it's in `request.user`), so what's the point of putting the ID in the URL, especially as you say the user isn't linked to the campaign at all. Why do all this `current_user` stuff? – Daniel Roseman Jun 10 '14 at 10:11
  • @Daniel Roseman good point. I was thinking that I could use a model method and later on make the URL more complicated. So its more of a function I need thats builds a URL based on values passed. But maybe you are right and I'm overthinking this. – Prometheus Jun 10 '14 at 10:14
  • Since you tagged the question `Django` you probably want to know about the `reverse` function: https://docs.djangoproject.com/en/dev/ref/urlresolvers/#reverse There's also a convention in Django to name the method get_absolute_url, in your case that would probably take a user object as an argument. – antonagestam Jun 10 '14 at 10:17

1 Answers1

2

Instead of:

def get_referral_url(self):

simply define:

def get_referral_url_for(self, user):

This looks the most straightforward, and doesn't give a false impression that user in question would be somehow permanently connected to the Campaign.

Kos
  • 70,399
  • 25
  • 169
  • 233