def get_paste_text(hash, format=None, encrypted=False): """ Get paste text """ if format == "none": format = None if format and not encrypted: cache_result = cache.get("paste_content:%s:%s:formatted_text" % (hash, format)) if cache_result != None: return cache_result if settings.STORE_FORMATTED_PASTE_CONTENT: # We store the formatted paste content on the database, so # it should exist on the database # If it doesn't, generate it and save it try: paste_content = PasteContent.objects.get(hash=hash, format=format) cache.set("paste_content:%s:%s:formatted_text" % (hash, format), paste_content.text, None) except ObjectDoesNotExist: # We are retrieving formatted paste content, but it doesn't exist on the database, # so generate it try: unformatted_paste_content = PasteContent.objects.get(hash=hash, format="none") except ObjectDoesNotExist: return None text = PasteContent.add_paste_text(unformatted_paste_content.text, format) cache.set("paste_content:%s:formatted_text" % hash, text, None) return text return paste_content.text else: # We don't store the formatted paste content on the database, # so only generate it and then save it to cache unformatted_text = PasteContent.get_paste_text(hash, False, encrypted) if unformatted_text == None: return None text = highlighting.format_text(unformatted_text, format) cache.set("paste_content:%s:formatted_text" % hash, text, None) return text else: cache_result = cache.get("paste_content:%s:text" % hash) if cache_result != None: return cache_result try: paste_content = PasteContent.objects.get(hash=hash, format="none") except ObjectDoesNotExist: return None cache.set("paste_content:%s:text" % hash, paste_content.text, None) return paste_content.text
def add_paste_text(text, format=None): """ Adds paste text if it hasn't been added yet If format other than None is provided, Pygments will be used to highlight the text """ hash = hashlib.sha256(text.encode('utf-8')).hexdigest() if format != None: text = highlighting.format_text(text, format) elif format == None: format = "none" # Paste text may already exist if not PasteContent.objects.filter(hash=hash, format=format).exists(): paste_content = PasteContent(hash=hash, format=format, text=text) paste_content.save() else: # It already exists, just retrieve it try: paste_content = PasteContent.objects.get(hash=hash, format=format) except ObjectDoesNotExist: raise RuntimeError( "Paste content was deleted during execution.") return paste_content.text
def add_paste_text(text, format=None): """ Adds paste text if it hasn't been added yet If format other than None is provided, Pygments will be used to highlight the text """ hash = hashlib.sha256(text.encode('utf-8')).hexdigest() if format != None: text = highlighting.format_text(text, format) elif format == None: format = "none" # Paste text may already exist if not PasteContent.objects.filter(hash=hash, format=format).exists(): paste_content = PasteContent(hash=hash, format=format, text=text) paste_content.save() else: # It already exists, just retrieve it try: paste_content = PasteContent.objects.get(hash=hash, format=format) except ObjectDoesNotExist: raise RuntimeError("Paste content was deleted during execution.") return paste_content.text
def get_paste_text(hash, format=None, encrypted=False): """ Get paste text """ if format == "none": format = None if format and not encrypted: cache_result = cache.get("paste_content:%s:%s:formatted_text" % (hash, format)) if cache_result != None: return cache_result if settings.STORE_FORMATTED_PASTE_CONTENT: # We store the formatted paste content on the database, so # it should exist on the database # If it doesn't, generate it and save it try: paste_content = PasteContent.objects.get(hash=hash, format=format) cache.set( "paste_content:%s:%s:formatted_text" % (hash, format), paste_content.text, None) except ObjectDoesNotExist: # We are retrieving formatted paste content, but it doesn't exist on the database, # so generate it try: unformatted_paste_content = PasteContent.objects.get( hash=hash, format="none") except ObjectDoesNotExist: return None text = PasteContent.add_paste_text( unformatted_paste_content.text, format) cache.set("paste_content:%s:formatted_text" % hash, text, None) return text return paste_content.text else: # We don't store the formatted paste content on the database, # so only generate it and then save it to cache unformatted_text = PasteContent.get_paste_text( hash, False, encrypted) if unformatted_text == None: return None text = highlighting.format_text(unformatted_text, format) cache.set("paste_content:%s:formatted_text" % hash, text, None) return text else: cache_result = cache.get("paste_content:%s:text" % hash) if cache_result != None: return cache_result try: paste_content = PasteContent.objects.get(hash=hash, format="none") except ObjectDoesNotExist: return None cache.set("paste_content:%s:text" % hash, paste_content.text, None) return paste_content.text