def _save__process_rows_and_content(request, version, posted_rows): """Processes rows and content data. Iterates over the submitted data and saves it in the database. """ # Delete all existing rows version.rows.all().delete() # Loop through submitted rows for row in posted_rows: # Create row instance row_obj = Row(version=version, order=row['order']) row_obj.save() # Create column instances for column in row['columns']: column_obj = Column( row=row_obj, span=column['span'], offset=column['offset'], order=column['order'], ) column_obj.save() for content in column['contents']: content_obj = Content( column=column_obj, content=content['content'], type=content['type'], order=content['order'] ) content_obj.save()
def create_template(template, version): if template == '1': contents = [ {'type': 'widget', 'content': json.dumps({'widget': 'ad', 'format': 'fullwidth_single'})}, ] row = Row(version=version, order=0) row.save() column = Column(row=row, span=12, offset=0, order=0) column.save() for i in range(len(contents)): content = Content(column=column, content=contents[i]['content'], type=contents[i]['type'], order=i) content.save() elif template == '2': contents = [ {'type': 'html', 'content': """<h1>Fengende overskrift</h1>"""}, {'type': 'image', 'content': json.dumps({'src': settings.EDITOR_PLACEHOLDER_IMAGE, 'alt': "placeholder"})}, {'type': 'html', 'content': """<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ut gravida lorem. Ut turpis felis, pulvinar a semper sed, adipiscing id dolor. Pellentesque auctor nisi id magna consequat sagittis. Curabitur dapibus enim sit amet elit pharetra tincidunt feugiat nisl imperdiet. Ut convallis libero in urna ultrices accumsan. Donec sed odio eros. Donec viverra mi quis quam pulvinar at malesuada arcu rhoncus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In rutrum accumsan ultricies. Mauris vitae nisi at sem facilisis semper ac in est.</p>"""}, {'type': 'html', 'content': """<h2>Mindre overskrift</h2><p>Vivamus fermentum semper porta. Nunc diam velit, adipiscing ut tristique vitae, sagittis vel odio. Maecenas convallis ullamcorper ultricies. Curabitur ornare, ligula semper consectetur sagittis, nisi diam iaculis velit, id fringilla sem nunc vel mi. Nam dictum, odio nec pretium volutpat, arcu ante placerat erat, non tristique elit urna et turpis. Quisque mi metus, ornare sit amet fermentum et, tincidunt et orci. Fusce eget orci a orci congue vestibulum. Ut dolor diam, elementum et vestibulum eu, porttitor vel elit. Curabitur venenatis pulvinar tellus gravida ornare. Sed et erat faucibus nunc euismod ultricies ut id justo. Nullam cursus suscipit nisi, et ultrices justo sodales nec. Fusce venenatis facilisis lectus ac semper. Aliquam at massa ipsum. Quisque bibendum purus convallis nulla ultrices ultricies. Nullam aliquam, mi eu aliquam tincidunt, purus velit laoreet tortor, viverra pretium nisi quam vitae mi. Fusce vel volutpat elit. Nam sagittis nisi dui.</p>"""}, {'type': 'image', 'content': json.dumps({'src': settings.EDITOR_PLACEHOLDER_IMAGE, 'alt': "placeholder"})}, {'type': 'widget', 'content': json.dumps({'widget': 'ad', 'format': 'fullwidth_single'})}, ] row = Row(version=version, order=0) row.save() column = Column(row=row, span=12, offset=0, order=0) column.save() for i in range(len(contents)): content = Content(column=column, content=contents[i]['content'], type=contents[i]['type'], order=i) content.save() elif template == '3': contents_upper = [ {'type': 'html', 'content': """<h1>Fengende overskrift</h1>"""}, {'type': 'image', 'content': json.dumps({'src': settings.EDITOR_PLACEHOLDER_IMAGE, 'alt': "placeholder"})}, ] contents_lower_left = [ {'type': 'html', 'content': """<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ut gravida lorem. Ut turpis felis, pulvinar a semper sed, adipiscing id dolor. Pellentesque auctor nisi id magna consequat sagittis. Curabitur dapibus enim sit amet elit pharetra tincidunt feugiat nisl imperdiet. Ut convallis libero in urna ultrices accumsan. Donec sed odio eros. Donec viverra mi quis quam pulvinar at malesuada arcu rhoncus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In rutrum accumsan ultricies. Mauris vitae nisi at sem facilisis semper ac in est.</p>"""}, {'type': 'html', 'content': """<h2>Mindre overskrift</h2><p>Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In euismod ultrices facilisis. Vestibulum porta sapien adipiscing augue congue id pretium lectus molestie. Proin quis dictum nisl. Morbi id quam sapien, sed vestibulum sem. Duis elementum rutrum mauris sed convallis. Proin vestibulum magna mi. Aenean tristique hendrerit magna, ac facilisis nulla hendrerit ut. Sed non tortor sodales quam auctor elementum. Donec hendrerit nunc eget elit pharetra pulvinar. Suspendisse id tempus tortor. Aenean luctus, elit commodo laoreet commodo, justo nisi consequat massa, sed vulputate quam urna quis eros. Donec vel.</p>"""}, ] contents_lower_right = [ {'type': 'widget', 'content': json.dumps({'widget': 'ad', 'format': 'sidebar'})}, {'type': 'html', 'content': """<p>Suspendisse lectus leo, consectetur in tempor sit amet, placerat quis neque. Etiam luctus porttitor lorem, sed suscipit est rutrum non. Curabitur lobortis nisl a enim congue semper. Aenean commodo ultrices imperdiet. Vestibulum ut justo vel sapien venenatis tincidunt. Phasellus eget dolor sit amet ipsum dapibus condimentum vitae quis lectus.</p>"""}, {'type': 'image', 'content': json.dumps({'src': settings.EDITOR_PLACEHOLDER_IMAGE, 'alt': "placeholder"})}, ] contents_bottom = [ {'type': 'widget', 'content': json.dumps({'widget': 'ad', 'format': 'fullwidth_single'})}, ] row = Row(version=version, order=0) row.save() column = Column(row=row, span=12, offset=0, order=0) column.save() for i in range(len(contents_upper)): content = Content(column=column, content=contents_upper[i]['content'], type=contents_upper[i]['type'], order=i) content.save() row = Row(version=version, order=1) row.save() column = Column(row=row, span=9, offset=0, order=0) column.save() for i in range(len(contents_lower_left)): content = Content(column=column, content=contents_lower_left[i]['content'], type=contents_lower_left[i]['type'], order=i) content.save() column = Column(row=row, span=3, offset=0, order=1) column.save() for i in range(len(contents_lower_right)): content = Content(column=column, content=contents_lower_right[i]['content'], type=contents_lower_right[i]['type'], order=i) content.save() row = Row(version=version, order=2) row.save() column = Column(row=row, span=12, offset=0, order=0) column.save() for i in range(len(contents_bottom)): content = Content(column=column, content=contents_bottom[i]['content'], type=contents_bottom[i]['type'], order=i) content.save() elif template == '4': contents_upper = [ {'type': 'html', 'content': """<h1>Fengende overskrift</h1>"""}, {'type': 'image', 'content': json.dumps({'src': settings.EDITOR_PLACEHOLDER_IMAGE, 'alt': "placeholder"})}, ] contents_middle_left = [ {'type': 'html', 'content': """<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>"""}, ] contents_middle_center = [ {'type': 'html', 'content': """<p>Vivamus fermentum semper porta. Nunc diam velit, adipiscing ut tristique vitae, sagittis vel odio. Maecenas convallis ullamcorper ultricies. Curabitur ornare, ligula semper consectetur sagittis, nisi diam iaculis velit, id fringilla sem nunc vel mi. Nam dictum, odio nec pretium volutpat, arcu ante placerat erat, non tristique elit urna et turpis. Quisque mi metus, ornare sit amet fermentum et, tincidunt et orci. Fusce eget orci a orci congue vestibulum.</p>"""}, ] contents_middle_right = [ {'type': 'html', 'content': """<p>Vivamus fermentum semper porta. Nunc diam velit, adipiscing ut tristique vitae, sagittis vel odio. Maecenas convallis ullamcorper ultricies. Curabitur ornare, ligula semper consectetur sagittis, nisi diam iaculis velit, id fringilla sem nunc vel mi. Nam dictum, odio nec pretium volutpat, arcu ante placerat erat, non tristique elit urna et turpis. Quisque mi metus, ornare sit amet fermentum et, tincidunt et orci. Fusce eget orci a orci congue vestibulum.</p>"""}, ] contents_lower = [ {'type': 'widget', 'content': json.dumps({'widget': 'ad', 'format': 'fullwidth_triple'})}, ] row = Row(version=version, order=0) row.save() column = Column(row=row, span=12, offset=0, order=0) column.save() for i in range(len(contents_upper)): content = Content(column=column, content=contents_upper[i]['content'], type=contents_upper[i]['type'], order=i) content.save() row = Row(version=version, order=1) row.save() column = Column(row=row, span=4, offset=0, order=0) column.save() for i in range(len(contents_middle_left)): content = Content(column=column, content=contents_middle_left[i]['content'], type=contents_middle_left[i]['type'], order=i) content.save() column = Column(row=row, span=4, offset=0, order=1) column.save() for i in range(len(contents_middle_center)): content = Content(column=column, content=contents_middle_center[i]['content'], type=contents_middle_center[i]['type'], order=i) content.save() column = Column(row=row, span=4, offset=0, order=2) column.save() for i in range(len(contents_middle_right)): content = Content(column=column, content=contents_middle_right[i]['content'], type=contents_middle_right[i]['type'], order=i) content.save() row = Row(version=version, order=2) row.save() column = Column(row=row, span=12, offset=0, order=0) column.save() for i in range(len(contents_lower)): content = Content(column=column, content=contents_lower[i]['content'], type=contents_lower[i]['type'], order=i) content.save()
def save(request, site, version): active_site = Site.get_cached_by_id(site) try: version = Version.objects.get(id=version) except Version.DoesNotExist: return HttpResponse(json.dumps({'version_does_not_exist': True})) response = {} # Wrap the entire save operation in an atomic transaction with transaction.atomic(): # Delete all existing rows version.rows.all().delete() posted_rows = json.loads(request.POST['rows']) for row in posted_rows: row_obj = Row(version=version, order=row['order']) row_obj.save() for column in row['columns']: column_obj = Column( row=row_obj, span=column['span'], offset=column['offset'], order=column['order'], ) column_obj.save() for content in column['contents']: content_obj = Content( column=column_obj, content=content['content'], type=content['type'], order=content['order'] ) content_obj.save() # Tags - common for pages and articles version.tags.clear() for tag in [t.lower() for t in json.loads(request.POST['tags'])]: obj, created = Tag.objects.get_or_create(name=tag) version.tags.add(obj) # Article/Page data if version.page is not None: page = version.page # Clear the cache *before* data assignment in case the slug has changed, to ensure the old slug cache is # invalidated page.clear_cache() ### Title ### page.title = request.POST['title'] page.slug = request.POST['slug'].strip().strip('/') ### Published state ### page.published = json.loads(request.POST['status']) # Record the modification page.modified_by = request.user page.modified_date = datetime.now() version.save() page.save() # Clear cache again with potentially new slug page.clear_cache() elif version.article is not None: article = version.article ### Authors ### publisher_list = json.loads(request.POST['authors']) if len(publisher_list) > 0: publishers = User.get_users().filter(id__in=publisher_list) version.publishers = publishers else: response['author_error'] = 'no_authors' ### Category ### article.category = request.POST['category'] ### Published state ### try: article.published = json.loads(request.POST['status']) datetime_string = '%s %s' % (request.POST['publish_date'], request.POST['publish_time']) article.pub_date = datetime.strptime(datetime_string, '%d.%m.%Y %H:%M') except ValueError: if article.published: # We're trying to publish, and an error occured. if request.POST['publish_date'] == '' and request.POST['publish_time'] == '': # Well, since we didn't specify the date, set it to now - and update it clientside now = datetime.now() response['publish_error'] = 'auto_now' response['publish_date'] = now.strftime('%d.%m.%Y') response['publish_time'] = now.strftime('%H:%M') article.pub_date = now else: # Parse error - inform, and don't publish response['publish_error'] = 'unparseable_datetime' article.published = False else: # An error occured, but we're not publishing so just nullify response['publish_error'] = 'error_nullify' article.pub_date = None ### Unpublish date ### try: article.unpub_date = datetime.strptime( request.POST['unpublish_date'], '%d.%m.%Y', ).date() except ValueError: article.unpub_date = None ### Thumbnail state ### if request.POST['thumbnail'] == 'none': article.thumbnail = None article.hide_thumbnail = True elif request.POST['thumbnail'] == 'default': # Verify server-side that there's at least one image image_content = False for row in posted_rows: for column in row['columns']: if any(content['type'] == 'image' for content in column['contents']): image_content = True break if not image_content: response['thumbnail_missing_image'] = True article.thumbnail = None article.hide_thumbnail = True else: article.thumbnail = None article.hide_thumbnail = False elif request.POST['thumbnail'] == 'specified': article.thumbnail = request.POST['thumbnail_url'] article.hide_thumbnail = False # Record the modification article.modified_by = request.user article.modified_date = datetime.now() version.save() article.save() cache.delete('articles.%s' % article.id) cache.delete('version.%s.thumbnail.small' % version.id) cache.delete('version.%s.thumbnail.medium' % version.id) return HttpResponse(json.dumps(response))