def load_service(data): print(f'- Cleaning up data...\r', end='') slug = data['slug'] for k in [ 'meta_description_ar', 'meta_description_en', 'meta_description_es', 'meta_description_vi', 'meta_tags', 'meta_title_ar', 'meta_title_en', 'meta_title_es', 'meta_title_vi' ]: data.pop(k, None) print(f'- Loading service steps...\r', end='') service_steps = data.pop('service_steps') print('✅') data['additional_content'] = data['additional_content_en'] print('✅') print(f'- Loading user...\r', end='') data['owner'] = User.objects.get(id='1') print('✅') print(f'- Loading homepage...\r', end='') home = Page.objects.get(slug='home') print('✅') print(f'- Loading topic page...\r', end='') topic_slug = data.pop('topic') data['topic'] = Topic.objects.get(slug=topic_slug) print('✅') print(f'- Loading image...\r', end='') image_name = data.pop('image') image_regex = f'original_images/{image_name}' data['image'] = TranslatedImage.objects.get(file__startswith=image_regex) print('✅') print(f'- Loading contacts...\r', end='') contact = data.pop('contact') contact = Contact.objects.get(name=contact) print('✅') ddata = data.pop('dynamic_content') or [] dynamic_content = [] for d in ddata: content_type = d['type'] print(f'- Loading dynamic content {content_type}...\r', end='') if content_type == 'map': defaults = { k: v for k, v in d.items() if k.startswith('description') } defaults['location'] = Location.objects.get(name=d['location']) content_map, created = Map.objects.update_or_create( description_en=d['description_en'], defaults=defaults) dynamic_content.append(('map_block', content_map)) print(f'{"✅ Created" if created else "⭐ Updated"}') else: dynamic_content.append((f'{content_type}_block', None)) print('✅') data['dynamic_content'] = dynamic_content print(f'- Loading child page "{slug}"...\r', end='') created = False try: page = ServicePage.objects.get(slug=slug) for k, v in data.items(): setattr(page, k, v) for step in page.service_steps.all(): step.delete() except Exception as e: page = ServicePage(**data) home.add_child(instance=page) created = True page.title = data['title_en'] for i, service_step in enumerate(service_steps): print(service_step) service_step['page_id'] = page.id service_step['sort_order'] = i + 1 load_service_step(service_step) page.save_revision().publish() print(f'{"✅ Created" if created else "⭐ Updated"}') print(f'- Loading service contacts...\r', end='') _, created = ServicePageContact.objects.get_or_create( page=page, defaults={'contact': contact}) print(f'{"✅ Created" if created else "✔️ Fetched"}') yield page
def new_page_from_modal(request): user_perms = UserPagePermissionsProxy(request.user) if not user_perms.can_edit_pages(): raise PermissionDenied if request.method == 'POST': # Get the page data body = json.loads(request.body) print(body['type']) data = {} data['title'] = body['title'] data['owner'] = request.user # Create the page if body['type'] == 'service': page = ServicePage(**data) elif body['type'] == 'process': page = ProcessPage(**data) elif body['type'] == 'information': page = InformationPage(**data) elif body['type'] == 'topic': page = TopicPage(**data) elif body['type'] == 'topiccollection': if body['theme'] is not None: data['theme'] = Theme.objects.get(id=body['theme']) page = TopicCollectionPage(**data) elif body['type'] == 'department': page = DepartmentPage(**data) elif body['type'] == 'documents': page = OfficialDocumentPage(**data) elif body['type'] == 'guide': page = GuidePage(**data) elif body['type'] == 'form': page = FormContainer(**data) elif body['type'] == 'location': page = LocationPage(**data) # Add it as a child of home home = Page.objects.get(id=3) home.add_child(instance=page) # Save our draft page.save_revision() page.unpublish() # Not sure why it seems to go live by default # Respond with the id of the new page response = HttpResponse(json.dumps({'id': page.id}), content_type="application/json") return response
def resolve_page_type(self, info): return ServicePage.get_verbose_name().lower()