Conditions | 11 |
Total Lines | 83 |
Lines | 0 |
Ratio | 0 % |
Tests | 1 |
CRAP Score | 121.6384 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like UpdateAccount.from_dict() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | 1 | from plugin.managers.core.base import Manager, Update |
|
19 | 1 | def from_dict(self, t_account, changes): |
|
20 | # Resolve `account` |
||
21 | if inspect.isfunction(t_account): |
||
22 | t_account = t_account() |
||
23 | |||
24 | # Update `TraktAccount` |
||
25 | if not super(UpdateAccount, self).from_dict(t_account, changes): |
||
26 | return False |
||
27 | |||
28 | # Update credentials |
||
29 | authorization = changes.get('authorization', {}) |
||
30 | |||
31 | if 'username' in changes: |
||
32 | # Provide username change in basic authorization update |
||
33 | if 'basic' not in authorization: |
||
34 | authorization['basic'] = {} |
||
35 | |||
36 | authorization['basic']['username'] = changes['username'] |
||
37 | |||
38 | # Retrieve `TraktBasicCredential` |
||
39 | t_account.basic = TraktBasicCredentialManager.get.or_create( |
||
40 | TraktBasicCredential.account == t_account, |
||
41 | account=t_account |
||
42 | ) |
||
43 | |||
44 | # Update `TraktBasicCredential` (if there are changes) |
||
45 | if 'basic' in authorization: |
||
46 | TraktBasicCredentialManager.update.from_dict( |
||
47 | t_account.basic, |
||
48 | authorization['basic'], |
||
49 | save=False |
||
50 | ) |
||
51 | |||
52 | # Retrieve `TraktOAuthCredential` |
||
53 | t_account.oauth = TraktOAuthCredentialManager.get.or_create( |
||
54 | TraktOAuthCredential.account == t_account, |
||
55 | account=t_account |
||
56 | ) |
||
57 | |||
58 | # Update `TraktOAuthCredential` (if there are changes) |
||
59 | if 'oauth' in authorization: |
||
60 | TraktOAuthCredentialManager.update.from_dict( |
||
61 | t_account.oauth, |
||
62 | authorization['oauth'], |
||
63 | save=False |
||
64 | ) |
||
65 | |||
66 | # Validate the account authorization |
||
67 | with t_account.authorization().http(retry=True): |
||
68 | settings = Trakt['users/settings'].get() |
||
69 | |||
70 | if not settings: |
||
71 | log.warn('Unable to retrieve account details for authorization') |
||
72 | return None |
||
73 | |||
74 | # Update `TraktAccount` username |
||
75 | try: |
||
76 | self.update_username(t_account, settings) |
||
77 | except (apsw.ConstraintError, peewee.IntegrityError), ex: |
||
78 | log.debug('Trakt account already exists - %s', ex, exc_info=True) |
||
79 | |||
80 | raise TraktAccountExistsException('Trakt account already exists') |
||
81 | |||
82 | # Refresh `TraktAccount` |
||
83 | t_account.refresh( |
||
84 | force=True, |
||
85 | settings=settings |
||
86 | ) |
||
87 | |||
88 | # Save credentials |
||
89 | if 'basic' in authorization: |
||
90 | t_account.basic.save() |
||
91 | |||
92 | if 'oauth' in authorization: |
||
93 | t_account.oauth.save() |
||
94 | |||
95 | # Refresh `Account` |
||
96 | t_account.account.refresh( |
||
97 | force=True |
||
98 | ) |
||
99 | |||
100 | log.info('Updated account authorization for %r', t_account) |
||
101 | return True |
||
102 | |||
207 |