Conditions | 6 |
Total Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Tests | 1 |
CRAP Score | 37.1 |
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:
1 | 1 | from plugin.managers.core.base import Manager, Update |
|
18 | 1 | def from_dict(self, p_account, changes): |
|
19 | # Resolve `account` |
||
20 | if inspect.isfunction(p_account): |
||
21 | p_account = p_account() |
||
22 | |||
23 | # Update `PlexAccount` |
||
24 | username = changes.pop('username', None) |
||
25 | |||
26 | if not super(UpdateAccount, self).from_dict(p_account, changes): |
||
27 | return False |
||
28 | |||
29 | # Update credentials |
||
30 | authorization = changes.get('authorization', {}) |
||
31 | |||
32 | # Update `PlexBasicCredential` |
||
33 | p_account.basic = PlexBasicCredentialManager.get.or_create( |
||
34 | PlexBasicCredential.account == p_account, |
||
35 | account=p_account |
||
36 | ) |
||
37 | |||
38 | # Update `TraktBasicCredential` (if there are changes) |
||
39 | if 'basic' in authorization: |
||
40 | PlexBasicCredentialManager.update.from_dict( |
||
41 | p_account.basic, |
||
42 | authorization['basic'], |
||
43 | save=False |
||
44 | ) |
||
45 | |||
46 | # Update `PlexAccount` username |
||
47 | try: |
||
48 | self.update_username(p_account, username) |
||
49 | except (apsw.ConstraintError, peewee.IntegrityError), ex: |
||
50 | log.debug('Plex account already exists - %s', ex, exc_info=True) |
||
51 | |||
52 | raise PlexAccountExistsException('Plex account already exists') |
||
53 | |||
54 | # Refresh `TraktAccount` |
||
55 | p_account.refresh( |
||
56 | force=True |
||
57 | ) |
||
58 | |||
59 | # Save credentials |
||
60 | if 'basic' in authorization: |
||
61 | p_account.basic.save() |
||
62 | |||
63 | # Refresh `Account` |
||
64 | p_account.account.refresh( |
||
65 | force=True |
||
66 | ) |
||
67 | |||
68 | log.info('Updated account authorization for %r', p_account) |
||
69 | return True |
||
70 | |||
106 |