| Conditions | 27 |
| Total Lines | 112 |
| 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:
Complex classes like create_and_update_from_json_data() 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 | from django.db.models import Max |
||
| 131 | def create_and_update_from_json_data(d, user): |
||
| 132 | """ |
||
| 133 | Create or update page based on python dict d loaded from JSON data. |
||
| 134 | This applies all data except for redirect_to, which is done in a |
||
| 135 | second pass after all pages have been imported, |
||
| 136 | |||
| 137 | user is the User instance that will be used if the author can't |
||
| 138 | be found in the DB. |
||
| 139 | |||
| 140 | returns (page object, created, messages). |
||
| 141 | |||
| 142 | created is True if this was a new page or False if an existing page |
||
| 143 | was updated. |
||
| 144 | |||
| 145 | messages is a list of strings warnings/messages about this import |
||
| 146 | """ |
||
| 147 | page = None |
||
| 148 | parent = None |
||
| 149 | parent_required = True |
||
| 150 | created = False |
||
| 151 | messages = [] |
||
| 152 | |||
| 153 | page_languages = set(lang[0] for lang in settings.PAGE_LANGUAGES) |
||
| 154 | |||
| 155 | for lang, s in list(d['complete_slug'].items()): |
||
| 156 | if lang not in page_languages: |
||
| 157 | messages.append(_("Language '%s' not imported") % (lang,)) |
||
| 158 | continue |
||
| 159 | |||
| 160 | page = Page.objects.from_path(s, lang, exclude_drafts=False) |
||
| 161 | if page and page.get_complete_slug(lang) == s: |
||
| 162 | break |
||
| 163 | if parent_required and parent is None: |
||
| 164 | if '/' in s: |
||
| 165 | parent = Page.objects.from_path(s.rsplit('/', 1)[0], lang, |
||
| 166 | exclude_drafts=False) |
||
| 167 | else: |
||
| 168 | parent_required = False |
||
| 169 | else: |
||
| 170 | # can't find an existing match, need to create a new Page |
||
| 171 | page = Page(parent=parent) |
||
| 172 | created = True |
||
| 173 | |||
| 174 | user_model = get_user_model() |
||
| 175 | |||
| 176 | def custom_get_user_by_email(email): |
||
| 177 | """ |
||
| 178 | Simplified version |
||
| 179 | """ |
||
| 180 | return user_model.objects.get(email=email) |
||
| 181 | |||
| 182 | try: |
||
| 183 | page.author = custom_get_user_by_email(d['author_email']) |
||
| 184 | except (user_model.DoesNotExist, user_model.MultipleObjectsReturned): |
||
| 185 | page.author = user |
||
| 186 | messages.append(_("Original author '%s' not found") |
||
| 187 | % (d['author_email'],)) |
||
| 188 | |||
| 189 | page.creation_date = datetime.strptime(d['creation_date'], |
||
| 190 | ISODATE_FORMAT) |
||
| 191 | page.publication_date = datetime.strptime(d['publication_date'], |
||
| 192 | ISODATE_FORMAT) if d['publication_date'] else None |
||
| 193 | page.publication_end_date = datetime.strptime(d['publication_end_date'], |
||
| 194 | ISODATE_FORMAT) if d['publication_end_date'] else None |
||
| 195 | page.last_modification_date = datetime.strptime( |
||
| 196 | d['last_modification_date'], ISODATE_FORMAT) |
||
| 197 | page.status = { |
||
| 198 | 'published': Page.PUBLISHED, |
||
| 199 | 'hidden': Page.HIDDEN, |
||
| 200 | 'draft': Page.DRAFT, |
||
| 201 | }[d['status']] |
||
| 202 | page.template = d['template'] |
||
| 203 | page.redirect_to_url = d['redirect_to_url'] |
||
| 204 | |||
| 205 | page.save() |
||
| 206 | |||
| 207 | # Add tags |
||
| 208 | if settings.PAGE_TAGGING: |
||
| 209 | from taggit.models import Tag |
||
| 210 | tags = d.get('tags', []) |
||
| 211 | page.tags.clear() |
||
| 212 | if tags: |
||
| 213 | for tag in tags: |
||
| 214 | Tag.objects.get_or_create(name=tag) |
||
| 215 | page.tags.add(tag) |
||
| 216 | page.save() |
||
| 217 | |||
| 218 | if settings.PAGE_USE_SITE_ID: |
||
| 219 | if d['sites']: |
||
| 220 | for site in d['sites']: |
||
| 221 | try: |
||
| 222 | page.sites.add(Site.objects.get(domain=site)) |
||
| 223 | except Site.DoesNotExist: |
||
| 224 | messages.append(_("Could not add site '%s' to page") |
||
| 225 | % (site,)) |
||
| 226 | if not settings.PAGE_HIDE_SITES and not page.sites.count(): |
||
| 227 | # need at least one site |
||
| 228 | page.sites.add(Site.objects.get(pk=global_settings.SITE_ID)) |
||
| 229 | |||
| 230 | def create_content(lang, ctype, body): |
||
| 231 | Content.objects.create_content_if_changed(page, lang, ctype, body) |
||
| 232 | |||
| 233 | for lang in d['content_language_updated_order']: |
||
| 234 | if lang not in page_languages: |
||
| 235 | continue |
||
| 236 | create_content(lang, 'slug', |
||
| 237 | d['complete_slug'][lang].rsplit('/', 1)[-1]) |
||
| 238 | create_content(lang, 'title', d['title'][lang]) |
||
| 239 | for ctype, langs_bodies in list(d['content'].items()): |
||
| 240 | create_content(lang, ctype, langs_bodies[lang]) |
||
| 241 | |||
| 242 | return page, created, messages |
||
| 243 | |||
| 365 |