0

I need to deal with legacy django code.

Nearly all methods have request as first parameter. Not only views, that's ok there.

The request is passed along only to get request.user and to permission checks against the current active user.

I don't see a good solution to solve this mess.

There are tests, but I would like to refactor the code.

Removing request from the method definitions does not work, since the current active user needs to be accessed.

Update

request is accessed in:

  • views (OK for me)
  • forms (not OK for me)
  • utility methods (not OK for me)

models don't use request in this code (good for me).

guettli
  • 25,042
  • 81
  • 346
  • 663
  • In which layers is the request accessed? is it in views? models? forms? – petkostas Aug 13 '14 at 12:52
  • @petkostas thank you for our feedback. I updated the question – guettli Aug 13 '14 at 13:39
  • Request in forms is used if the form needs to deal with it. If you believe this needs refactoring, you could create a base class that assigns the request in an attribute and access it from there eg `self.request`, mind though that this requires request to be injected in the form so it doesn't save you much but allows you to use a common attribute to access the request information. Utility methods on the other hand, could call a centralized function / method to get the request and acquire the necessary details, this way future refactoring would require you to touch only the central function. – petkostas Aug 13 '14 at 13:47
  • If the logic required from request inside forms can be refactored to another layer (views?) you can also do that, but I can't really help you more since no details are provided of what kind of logic is performed inside your forms / utilities. – petkostas Aug 13 '14 at 13:49

0 Answers0