def save(self): muaccount = self.cleaned_data['muaccount'] total, imported = 0, 0 for row in csv.reader(self.cleaned_data['csv_file']): if row: try: tag, question, answer = row except ValueError: #default behaviour continue total +=1 try: Question.objects.get(html=question, muaccount=muaccount) except Question.DoesNotExist: if not muaccount.members.all().count(): continue user = random.choice(muaccount.members.all()) question = Question.objects.create(author=user, last_activity_by=user, html=question, title=truncate_letters(question, 40), tagnames=tag.lower(), muaccount=muaccount, ) Answer(question=question, author=random.choice(muaccount.members.exclude(id=user.id) or muaccount.members.all()), html=answer, ).save() imported += 1 return imported, total
def __str__(self): return "{text} ({type}, {visibility}, {guid})".format( text=truncate_letters(self.text, 30), guid=self.guid, visibility=self.visibility, type=self.content_type, )
def truncateletters(value, arg): """ Truncates a string after a certain number of letters Argument: Number of letters to truncate after """ from django_extensions.utils.text import truncate_letters try: length = int(arg) except ValueError: # invalid literal for int() return value # Fail silently return truncate_letters(value, length)
def fetch_og_preview(content, urls): """Fetch first opengraph entry for a list of urls.""" for url in urls: # See first if recently cached already if OpenGraphCache.objects.filter(url=url, modified__gte=now() - datetime.timedelta(days=7)).exists(): opengraph = OpenGraphCache.objects.get(url=url) Content.objects.filter(id=content.id).update(opengraph=opengraph) return opengraph # OpenGraph is kinda broken - make sure we destroy any old data before fetching OpenGraph.__data__ = {} try: og = OpenGraph(url=url) except (requests.exceptions.ConnectionError, AttributeError): continue if not og or ("title" not in og and "site_name" not in og and "description" not in og and "image" not in og): continue try: title = og.title if "title" in og else og.site_name if "site_name" in og else "" description = og.description if "description" in og else "" image = og.image if "image" in og and not content.is_nsfw else "" try: with transaction.atomic(): opengraph = OpenGraphCache.objects.create( url=url, title=truncate_letters(safe_text(title), 250), description=safe_text(description), image=safe_text(image), ) except DataError: continue except IntegrityError: # Some other process got ahead of us opengraph = OpenGraphCache.objects.get(url=url) Content.objects.filter(id=content.id).update(opengraph=opengraph) return opengraph Content.objects.filter(id=content.id).update(opengraph=opengraph) return opengraph return False
def fetch_og_preview(content, urls): """Fetch first opengraph entry for a list of urls.""" for url in urls: # See first if recently cached already if OpenGraphCache.objects.filter(url=url, modified__gte=now() - datetime.timedelta(days=7)).exists(): opengraph = OpenGraphCache.objects.get(url=url) Content.objects.filter(id=content.id).update(opengraph=opengraph) return opengraph try: og = OpenGraph(url=url, parser="lxml") except AttributeError: continue if not og or ("title" not in og and "site_name" not in og and "description" not in og and "image" not in og): continue try: title = og.title if "title" in og else og.site_name if "site_name" in og else "" description = og.description if "description" in og else "" image = og.image if "image" in og and not content.is_nsfw else "" try: with transaction.atomic(): opengraph = OpenGraphCache.objects.create( url=url, title=truncate_letters(safe_text(title), 250), description=safe_text(description), image=safe_text(image), ) except DataError: continue except IntegrityError: # Some other process got ahead of us opengraph = OpenGraphCache.objects.get(url=url) Content.objects.filter(id=content.id).update(opengraph=opengraph) return opengraph Content.objects.filter(id=content.id).update(opengraph=opengraph) return opengraph return False
def test_short_text(self): self.assertEqual(self.public_content.short_text, truncate_letters(self.public_content.text, 50))
def test_with_non_ascii_characters(self): self.assertEquals( u'\u5ce0 (\u3068\u3046\u3052 t\u014dg...', truncate_letters("峠 (とうげ tōge - mountain pass)", 10) )
def test_truncate_with_range(self): for i in range(10, -1, -1): self.assertEqual( u'hello tests'[:i] + '...', truncate_letters("hello tests", i) )
def test_truncate_text(self): self.assertEquals(u"hello...", truncate_letters("hello tests", 5))
def test_truncate_more_than_text_length(self): self.assertEquals(u"hello tests", truncate_letters("hello tests", 100))
def __unicode__(self): return "%s - %s" % (self.full_code, truncate_letters(self.description, 70))
def short_text(self): return truncate_letters(self.text, 50) or ""
def __str__(self): return "{text} ({guid})".format(text=truncate_letters(self.text, 100), guid=self.guid)
def test_truncate_more_than_text_length(self): self.assertEqual(six.u("hello tests"), truncate_letters("hello tests", 100))
def test_with_non_ascii_characters(self): self.assertEqual(six.u('\u5ce0 (\u3068\u3046\u3052 t\u014dg...'), truncate_letters("峠 (とうげ tōge - mountain pass)", 10))
def test_truncate_with_range(self): for i in range(10, -1, -1): self.assertEqual( six.u('hello tests'[:i]) + '...', truncate_letters("hello tests", i))
def test_truncate_text(self): self.assertEqual(six.u("hello..."), truncate_letters("hello tests", 5))
def __unicode__(self): return truncate_letters(self.code, 30)
def __str__(self): return "%s / %s" % (self.url, truncate_letters(self.title, 30))
def __str__(self): return "%s / %s" % ( self.url, truncate_letters(self.title, 30) )
def get_package(self, obj): return '<span title="%s">%s</span>' % (obj.package.name, truncate_letters(obj.package.name, 20))