| Conditions | 1 |
| Total Lines | 132 |
| Code Lines | 103 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 | # -*- coding: utf-8 -*- |
||
| 17 | def insert(self): |
||
| 18 | admin = self._session.query(models.User) \ |
||
| 19 | .filter(models.User.email == '[email protected]') \ |
||
| 20 | .one() |
||
| 21 | bob = self._session.query(models.User) \ |
||
| 22 | .filter(models.User.email == '[email protected]') \ |
||
| 23 | .one() |
||
| 24 | admin_workspace_api = WorkspaceApi( |
||
| 25 | current_user=admin, |
||
| 26 | session=self._session, |
||
| 27 | ) |
||
| 28 | bob_workspace_api = WorkspaceApi( |
||
| 29 | current_user=bob, |
||
| 30 | session=self._session, |
||
| 31 | ) |
||
| 32 | content_api = ContentApi( |
||
| 33 | current_user=admin, |
||
| 34 | session=self._session, |
||
| 35 | ) |
||
| 36 | role_api = RoleApi( |
||
| 37 | current_user=admin, |
||
| 38 | session=self._session, |
||
| 39 | ) |
||
| 40 | |||
| 41 | # Workspaces |
||
| 42 | w1 = admin_workspace_api.create_workspace('w1', save_now=True) |
||
| 43 | w2 = bob_workspace_api.create_workspace('w2', save_now=True) |
||
| 44 | w3 = admin_workspace_api.create_workspace('w3', save_now=True) |
||
| 45 | |||
| 46 | # Workspaces roles |
||
| 47 | role_api.create_one( |
||
| 48 | user=bob, |
||
| 49 | workspace=w1, |
||
| 50 | role_level=UserRoleInWorkspace.CONTENT_MANAGER, |
||
| 51 | with_notif=False, |
||
| 52 | ) |
||
| 53 | |||
| 54 | # Folders |
||
| 55 | w1f1 = content_api.create( |
||
| 56 | content_type=ContentType.Folder, |
||
| 57 | workspace=w1, |
||
| 58 | label='w1f1', |
||
| 59 | do_save=True, |
||
| 60 | ) |
||
| 61 | w1f2 = content_api.create( |
||
| 62 | content_type=ContentType.Folder, |
||
| 63 | workspace=w1, |
||
| 64 | label='w1f2', |
||
| 65 | do_save=True, |
||
| 66 | ) |
||
| 67 | |||
| 68 | w2f1 = content_api.create( |
||
| 69 | content_type=ContentType.Folder, |
||
| 70 | workspace=w2, |
||
| 71 | label='w2f1', |
||
| 72 | do_save=True, |
||
| 73 | ) |
||
| 74 | w2f2 = content_api.create( |
||
| 75 | content_type=ContentType.Folder, |
||
| 76 | workspace=w2, |
||
| 77 | label='w2f2', |
||
| 78 | do_save=True, |
||
| 79 | ) |
||
| 80 | |||
| 81 | w3f1 = content_api.create( |
||
| 82 | content_type=ContentType.Folder, |
||
| 83 | workspace=w3, |
||
| 84 | label='w3f3', |
||
| 85 | do_save=True, |
||
| 86 | ) |
||
| 87 | |||
| 88 | # Pages, threads, .. |
||
| 89 | w1f1p1 = content_api.create( |
||
| 90 | content_type=ContentType.Page, |
||
| 91 | workspace=w1, |
||
| 92 | parent=w1f1, |
||
| 93 | label='w1f1p1', |
||
| 94 | do_save=True, |
||
| 95 | ) |
||
| 96 | w1f1t1 = content_api.create( |
||
| 97 | content_type=ContentType.Thread, |
||
| 98 | workspace=w1, |
||
| 99 | parent=w1f1, |
||
| 100 | label='w1f1t1', |
||
| 101 | do_save=False, |
||
| 102 | ) |
||
| 103 | w1f1t1.description = 'w1f1t1 description' |
||
| 104 | self._session.add(w1f1t1) |
||
| 105 | w1f1d1_txt = content_api.create( |
||
| 106 | content_type=ContentType.File, |
||
| 107 | workspace=w1, |
||
| 108 | parent=w1f1, |
||
| 109 | label='w1f1d1', |
||
| 110 | do_save=False, |
||
| 111 | ) |
||
| 112 | w1f1d1_txt.file_extension = '.txt' |
||
| 113 | w1f1d1_txt.depot_file = FileIntent( |
||
| 114 | b'w1f1d1 content', |
||
| 115 | 'w1f1d1.txt', |
||
| 116 | 'text/plain', |
||
| 117 | ) |
||
| 118 | self._session.add(w1f1d1_txt) |
||
| 119 | w1f1d2_html = content_api.create( |
||
| 120 | content_type=ContentType.File, |
||
| 121 | workspace=w1, |
||
| 122 | parent=w1f1, |
||
| 123 | label='w1f1d2', |
||
| 124 | do_save=False, |
||
| 125 | ) |
||
| 126 | w1f1d2_html.file_extension = '.html' |
||
| 127 | w1f1d2_html.depot_file = FileIntent( |
||
| 128 | b'<p>w1f1d2 content</p>', |
||
| 129 | 'w1f1d2.html', |
||
| 130 | 'text/html', |
||
| 131 | ) |
||
| 132 | self._session.add(w1f1d2_html) |
||
| 133 | w1f1f1 = content_api.create( |
||
| 134 | content_type=ContentType.Folder, |
||
| 135 | workspace=w1, |
||
| 136 | label='w1f1f1', |
||
| 137 | parent=w1f1, |
||
| 138 | do_save=True, |
||
| 139 | ) |
||
| 140 | |||
| 141 | w2f1p1 = content_api.create( |
||
| 142 | content_type=ContentType.Page, |
||
| 143 | workspace=w2, |
||
| 144 | parent=w2f1, |
||
| 145 | label='w2f1p1', |
||
| 146 | do_save=True, |
||
| 147 | ) |
||
| 148 | self._session.flush() |
||
| 149 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.