import contentful import markdown import os import re class ContentfulService: def __init__(self, space_id, access_token): self.client = contentful.Client(space_id, access_token) def get_general_info(self): entries = self.client.entries({'content_type': 'general'}) if not entries: return None entry = entries[0] general_date = entry.fields().get('general_date') general_end_date = entry.fields().get('general_end_date') general_text = entry.fields().get('general_text') general_text_event = entry.fields().get('general_event_text') general_text_conference = entry.fields().get('general_conference_text') formatted_date = general_date.strftime('%#d %b %Y') if general_date else None formatted_end_date = general_end_date.strftime('%#d %b %Y') if general_end_date else None info = { 'startDate': formatted_date, 'endDate': formatted_end_date, 'text': markdown.markdown(general_text) if general_text else '', 'textConference': markdown.markdown(general_text_conference) if general_text_conference else '', 'textEvent':markdown.markdown(general_text_event) if general_text_event else '' } return info def get_location_info(self): entries = self.client.entries({'content_type': 'locations'}) location_list = [] for e in entries: name = getattr(e, 'name') directions = getattr(e, 'directions') url = getattr(e, 'url') image_asset = getattr(e, 'image') image_url = image_asset.url() if image_asset else '' content = { 'name': name, 'directions' : directions, 'url' : url, 'image' : image_url } location_list.append(content) return location_list def get_exhibition_list_info(self): entries = self.client.entries({'content_type': 'exhibitionList'}) exhibition_list = [] for e in entries: title = getattr(e, 'title') description = getattr(e, 'description') description = markdown.markdown(description) start_date = getattr(e, 'start_date') start_date = start_date.strftime('%#d %b %Y') if start_date else None end_date = getattr(e, 'end_date') end_date = end_date.strftime('%#d %b %Y') if end_date else None location = getattr(e, 'location') slug = title.lower() # Convert to lowercase slug = re.sub(r'[^a-z0-9\s-]', '', slug) # Remove non-alphanumeric characters slug = re.sub(r'[\s-]+', '-', slug).strip('-') # Replace spaces and repeated hyphens url = slug content = { 'title': title, 'location': location, 'description': description, 'start_date': start_date, 'end_date': end_date, 'url': url } exhibition_list.append(content) return exhibition_list def get_sponser_logos(self): entries = self.client.entries({'content_type': 'sponser'}) supp_img = [] prio_img = [] top_img = [] for e in entries: src = getattr(e, 'logo') idx = getattr(e, 'type') content = 'https:{0}'.format(src.url()) if idx == '1': top_img.append(content) elif idx == '2': prio_img.append(content) else: supp_img.append(content) return supp_img, prio_img, top_img