Example #1
0
def _add_hearing(hearing_data):
    print('Start adding hearing.')

    hearing = Hearing(
        slug=hearing_data['slug'],
        title=hearing_data['title'].strip(),
        lead=hearing_data['lead'].strip(),
        body=hearing_data['body'].strip(),
        opens_at=hearing_data['opens_at'],
        closes_at=hearing_data['closes_at'],
        published=hearing_data['published'],
    )

    hearing.main_image = Image(
        filename=hearing_data['main_image']['filename'],
        caption=hearing_data['main_image']['caption']
    )

    if hearing_data['area']:
        hearing.area = shape(hearing_data['area'])

    print('Main content added.')

    if 'alternatives' in hearing_data:
        for index, alternative_data in enumerate(hearing_data['alternatives']):
            alternative = Alternative(
                title=alternative_data['title'].strip(),
                lead=alternative_data['lead'].strip(),
                body=alternative_data['body'].strip(),
            )

            alternative.main_image = Image(
                filename=alternative_data['main_image']['filename'],
                caption=alternative_data['main_image']['caption']
            )

            hearing.alternatives.append(alternative)
            print('Alternative {index} added.'.format(index=index + 1))

    if 'sections' in hearing_data:
        for index, section_data in enumerate(hearing_data['sections']):
            section = Section(
                title=section_data['title'].strip(),
                lead=section_data['lead'].strip(),
                body=section_data['body'].strip(),
            )

            section.main_image = Image(
                filename=section_data['main_image']['filename'],
                caption=section_data['main_image']['caption']
            )

            hearing.sections.append(section)
            print('Section {index} added.'.format(index=index + 1))

    db.session.add(hearing)
    db.session.commit()
    print('Script completed! Hearing was successfully added.')
Example #2
0
def get_sample_hearing():
    hearing = Hearing(
        title=hearing_data['title'],
        lead=hearing_data['lead'],
        body=hearing_data['body'].strip(),
        opens_at=get_random_past_date(30),
        closes_at=get_random_future_date(30),
        published=True,
        main_image=get_image('Asemapiirros. Kuva: Arkkitehtitoimisto AJAK.'),
    )

    hearing.slug = str(datetime.utcnow())

    hearing.images.append(
        get_image('Arkkitehdin luonnos uudesta rakennuksesta.')
    )

    for alternative in alternatives:
        hearing.alternatives.append(
            Alternative(
                title=alternative['title'],
                lead=alternative['lead'].strip(),
                body=alternative['body'].strip(),
                main_image=get_image('Vaihtoehdon kuvan kuvateksti.'),
                images=[
                    get_image('Vaihtoehto voitaisiin toteuttaa myös näin.'),
                ]
            )
        )

    for i in range(randrange(15)):
        comment = comments[i % 3]
        hearing.comments.append(
            Comment(
                title=comment['title'],
                body=comment['body'].strip(),
                username=comment['username'],
                created_at=get_random_past_date(15)
            )
        )

    return hearing