Exemple #1
0
 def test_auto_slugifies(self):
     activate(self.language)
     title = u'This is a title'
     author = self.create_person()
     article = Article.objects.create(
         title=title, author=author, owner=author.user,
         app_config=self.app_config, publishing_date=now(),
         is_published=True,
     )
     article.save()
     self.assertEquals(article.slug, 'this-is-a-title')
     # Now, let's try another with the same title
     article_1 = Article(
         title=title.lower(),
         author=author,
         owner=author.user,
         app_config=self.app_config,
         publishing_date=now(),
         is_published=True,
     )
     # Note, it cannot be the exact same title, else we'll fail the unique
     # constraint on the field.
     article_1.save()
     # Note that this should be "incremented" slug here.
     self.assertEquals(article_1.slug, 'this-is-a-title-1')
     article_2 = Article(
         title=title.upper(),
         author=author,
         owner=author.user,
         app_config=self.app_config,
         publishing_date=now(),
         is_published=True,
     )
     article_2.save()
     self.assertEquals(article_2.slug, 'this-is-a-title-2')
Exemple #2
0
def student_profile(self, year=timezone.now().year):
    gsoc_year = GsocYear.objects.filter(gsoc_year=year).first()
    if gsoc_year is None:
        return None
    return self.userprofile_set.filter(role=3, gsoc_year=gsoc_year).first()


auth.models.User.add_to_class("student_profile", student_profile)


def get_root_comments(self):
    return self.comment_set.filter(parent=None).all()


Article.add_to_class("get_root_comments", get_root_comments)


def save(self, *args, **kwargs):
    tags = settings.BLEACH_ALLOWED_TAGS
    attrs = bleach.sanitizer.ALLOWED_ATTRIBUTES
    attrs["iframe"] = [
        "src",
        "frameborder",
        "allow",
        "allowfullscreen",
        "width",
        "height",
    ]
    attrs["img"] = ["src", "alt"]
    attrs["*"] = ["class", "style"]
Exemple #3
0
def main():
    User = get_user_model()
    TZ_PRAGUE = pytz.timezone('Europe/Prague')

    ARTICLES_LIMIT = 666
    WORKING_DIR = os.path.dirname(os.path.abspath(__file__))
    print('Path, where data files are: ', WORKING_DIR)

    missing_cs_content_counter = 0
    missing_en_content_counter = 0
    number_of_cs_articles_created = 0
    number_of_en_articles_created = 0
    number_of_images_created = 0

    DATA = json.load(
        open(WORKING_DIR + '/data_for_migration/old_news.json', "r"))

    # Defining
    user = User.objects.get(username='******')
    another_app_config = NewsBlogConfig.objects.get(
        pk=1)  # Use current settings

    # Create articles
    for counter, article in enumerate(DATA):
        assert not article['expire_at'] and not article[
            'expired'], "Article {} is expired!".format(counter)

        if not article['text_cs'] and not article['perex_cs']:
            missing_cs_content_counter += 1
            continue
        if not article['text_en'] and not article['perex_en']:
            missing_en_content_counter += 1

        exists = Article.objects.filter(
            translations__title=article['title_cs']).exists()

        if not exists:  # Prevents creating duplicates
            cs_text, images_count = migrate_text(
                article['text_cs'] if article['text_cs'] else
                article['perex_cs'], user, WORKING_DIR)
            number_of_images_created += images_count
            cs = {
                "title": article['title_cs'],
                "slug": article['slug_cs'],
                "lead_in": cs_text,
            }

            en_text, images_count = migrate_text(
                article['text_en'] if article['text_en'] else
                article['perex_en'], user, WORKING_DIR)
            number_of_images_created += images_count
            en = {
                "title": article['title'],
                "slug": article['slug'],
                "lead_in": en_text,
            }

            converted_date = datetime.strptime(article['date'], '%Y-%m-%d')
            publishing_date = TZ_PRAGUE.localize(converted_date +
                                                 timedelta(hours=6))

            # Creating articles
            obj_article = Article(
                is_published=True,
                publishing_date=publishing_date,
                app_config=another_app_config,
                owner=user,
            )
            obj_article.save_base()
            obj_article.create_translation(language_code='cs', **cs)
            number_of_cs_articles_created += 1
            if article['text_en'] or article['perex_en']:
                obj_article.create_translation(language_code='en', **en)
                number_of_en_articles_created += 1

        if counter >= ARTICLES_LIMIT - 1:
            break

    print('Number of created CS articles: ' +
          str(number_of_cs_articles_created))
    print('Number of created EN articles: ' +
          str(number_of_en_articles_created))
    print('Number of created images: ' + str(number_of_images_created))

    print('Number of missing CS contents: ' + str(missing_cs_content_counter))
    print('Number of missing EN contents: ' + str(missing_en_content_counter))
@receiver(models.signals.post_save, sender=RegLink)
def create_send_reglink_schedulers(sender, instance, **kwargs):
    if instance.adduserlog is not None and instance.scheduler is None:
        instance.create_scheduler()


@receiver(models.signals.post_save, sender=RegLink)
def create_send_reg_reminder_schedulers(sender, instance, **kwargs):
    if instance.adduserlog is not None and instance.reminder is None:
        instance.create_reminder()


class Comment(models.Model):
    username = models.CharField(max_length=50)
    user = models.ForeignKey(User, null=True, on_delete=models.CASCADE)
    article = models.ForeignKey(Article, on_delete=models.CASCADE)
    content = models.TextField()
    parent = models.ForeignKey('self',
                               null=True,
                               on_delete=models.CASCADE,
                               related_name='replies')
    created_at = models.DateTimeField(auto_now_add=True)


def get_root_comments(self):
    return self.comment_set.filter(parent=None).all()


Article.add_to_class('get_root_comments', get_root_comments)
    def test_duplicate_title_and_language(self):
        """
        Test that if user attempts to create an article with the same name and
        in the same language as another, it will not raise exceptions.
        """
        title = "Sample Article"
        author = self.create_person()
        original_lang = settings.LANGUAGES[0][0]
        # Create an initial article in the first language
        article1 = Article(
            title=title, author=author, owner=author.user,
            app_config=self.app_config, publishing_date=now()
        )
        article1.set_current_language(original_lang)
        article1.save()

        # Now try to create an article with the same title in every possible
        # language and every possible language contexts.
        for context_lang, _ in settings.LANGUAGES:
            with override(context_lang):
                for article_lang, _ in settings.LANGUAGES:
                    try:
                        article = Article(author=author, owner=author.user,
                            app_config=self.app_config, publishing_date=now())
                        article.set_current_language(article_lang)
                        article.title = title
                        article.save()
                    except Exception:
                        self.fail('Creating article in process context "{0}" '
                            'and article language "{1}" with identical name '
                            'as another "{2}" article raises exception'.format(
                                context_lang,
                                article_lang,
                                original_lang,
                            ))
Exemple #6
0
    def test_duplicate_title_and_language(self):
        """
        Test that if user attempts to create an article with the same name and
        in the same language as another, it will not raise exceptions.
        """
        title = "Sample Article"
        author = self.create_person()
        original_lang = settings.LANGUAGES[0][0]
        # Create an initial article in the first language
        article1 = Article(
            title=title, author=author, owner=author.user,
            app_config=self.app_config, publishing_date=now(),
            is_published=True,
        )
        article1.set_current_language(original_lang)
        article1.save()

        # Now try to create an article with the same title in every possible
        # language and every possible language contexts.
        for context_lang, _ in settings.LANGUAGES:
            with override(context_lang):
                for article_lang, _ in settings.LANGUAGES:
                    try:
                        article = Article(
                            author=author, owner=author.user,
                            app_config=self.app_config, publishing_date=now(),
                            is_published=True,
                        )
                        article.set_current_language(article_lang)
                        article.title = title
                        article.save()
                    except Exception:
                        self.fail('Creating article in process context "{0}" '
                            'and article language "{1}" with identical name '
                            'as another "{2}" article raises exception'.format(
                                context_lang,
                                article_lang,
                                original_lang,
                            ))
Exemple #7
0
def student_profile(self, year=timezone.now().year):
    gsoc_year = GsocYear.objects.filter(gsoc_year=year).first()
    if gsoc_year is None:
        return None
    return self.userprofile_set.filter(role=3, gsoc_year=gsoc_year).first()


auth.models.User.add_to_class('student_profile', student_profile)


def get_root_comments(self):
    return self.comment_set.filter(parent=None).all()


Article.add_to_class('get_root_comments', get_root_comments)


def is_unclean(self):
    unclean_texts = (
        '<pre>',
        '</pre>',
        '&lt;',
        '&gt;',
    )
    for _ in unclean_texts:
        if _ in self.lead_in:
            return True
    return False

Exemple #8
0
    def download_post(self, post_tuple):

        post = post_tuple[0]
        photo_key = post_tuple[1]

        translation.activate('ru')
        print("Post {} is being downloaded".format(post['id']))

        title = self.truncate_text_to_title(post['text'], 50)

        try:
            attachment = post['attachments'][0]

            pic_url = attachment['photo'][photo_key]

            pic_response = requests.get(pic_url, allow_redirects=True)
            url_end_index = pic_url.find('.com')
            path_to_save = pic_url[url_end_index + 5:].replace('/', '_')

            pic_file = open(
                path_join(settings.MEDIA_ROOT, 'vk_pictures', path_to_save),
                'w+b')
            pic_file.write(pic_response.content)
            pic_file.seek(0)

            importer = FileImporter()
            folder = FileImporter.get_or_create_folder(importer,
                                                       ['base', 'subfolder'])
            file_obj = DjangoFile(pic_file, name=title + '.jpg')
            pic = importer.import_file(file_obj, folder)
            pic.save()
            pic_file.close()

        except KeyError:
            pic = None
            pass

        except:
            pic = None
            raise

        lead_in = SafeText(self.truncate_text_to_title(post['text'], 300))

        try:
            user = User.objects.get(username='******')
        except django.contrib.auth.models.DoesNotExist:
            raise Exception("Должен быть создан пользователь с именем 'vk'")
        try:
            author = Person.objects.language('ru').get(user=user)
        except django.contrib.auth.models.DoesNotExist:
            raise Exception(
                "Должен быть создан Aldryn NewsBlog человек со ссылкой на пользователя 'vk'"
            )

        try:
            config = NewsBlogConfig.objects.language('ru').all()[0]
        except IndexError:
            raise Exception("No available NewsBlog config!")

        publishing_date = datetime.fromtimestamp(int(post['date']))

        article = Article(author=author,
                          app_config=config,
                          owner=user,
                          title=title,
                          publishing_date=publishing_date,
                          featured_image=pic,
                          lead_in=lead_in,
                          is_published=True)
        article.set_current_language('ru')
        article.save()

        placeholder = article.content
        add_plugin(placeholder, 'TextPlugin', 'ru', body=post['text'])
        self.convert_hashtag_to_category(article)

        VkPost.objects.create(post_id=post['id'],
                              date=post['date'],
                              article=article)

        return article