Ejemplo n.º 1
0
 def setUp(self):
     self.login()
     self.root_page = Page.objects.get(id=1)
     self.home_page = self.root_page.add_child(instance=HomePage(
         title='New America'))
     self.all_press_releases_home_page = self.home_page.add_child(
         instance=AllPressReleasesHomePage(
             title='New America Press Releases'))
     self.program_page_1 = self.home_page.add_child(
         instance=Program(title='OTI',
                          name='OTI',
                          description='OTI',
                          location=False,
                          depth=3))
     self.second_program = self.home_page.add_child(
         instance=Program(title='Education',
                          name='Education',
                          slug='education',
                          description='Education',
                          location=False,
                          depth=3))
     self.program_press_releases_page = self.program_page_1\
         .add_child(instance=ProgramPressReleasesPage(
             title='OTI Press Releases')
         )
     self.press_release = PressRelease(title='Press Release 1',
                                       slug='press-release-1',
                                       date='2016-02-10',
                                       depth=5)
     self.program_press_releases_page.add_child(instance=self.press_release)
     self.press_release.save()
Ejemplo n.º 2
0
 def setUp(self):
     self.login()
     self.root_page = Page.objects.get(id=1)
     self.home_page = self.root_page.add_child(instance=HomePage(
         title='New America'))
     self.all_quoted_home_page = self.home_page.add_child(
         instance=AllQuotedHomePage(title='New America In The News'))
     self.program_page_1 = self.home_page.add_child(
         instance=Program(title='OTI',
                          name='OTI',
                          description='OTI',
                          location=False,
                          depth=3))
     self.second_program = self.home_page.add_child(
         instance=Program(title='Education',
                          name='Education',
                          slug='education',
                          description='Education',
                          location=False,
                          depth=3))
     self.program_quoted_page = self.program_page_1\
         .add_child(instance=ProgramQuotedPage(
             title='OTI In The News')
         )
     self.quoted = Quoted(title='Quoted 1',
                          slug='quoted-1',
                          date='2016-02-10',
                          depth=5)
     self.program_quoted_page.add_child(instance=self.quoted)
     self.quoted.save()
Ejemplo n.º 3
0
 def setUp(self):
     self.login()
     self.root_page = Page.objects.get(id=1)
     self.home_page = self.root_page.add_child(instance=HomePage(
         title='New America'))
     self.all_events_home_page = self.home_page.add_child(
         instance=AllEventsHomePage(title="All Events at New America!"))
     self.program_page_1 = self.home_page.add_child(
         instance=Program(title='OTI',
                          name='OTI',
                          description='OTI',
                          location=False,
                          depth=3))
     self.second_program = self.home_page.add_child(
         instance=Program(title='Education',
                          name='Education',
                          slug='education',
                          description='Education',
                          location=False,
                          depth=3))
     self.program_events_page = self.program_page_1.add_child(
         instance=ProgramEventsPage(title='OTI Events'))
     self.event = self.program_events_page.add_child(
         instance=Event(title='Event 1',
                        date='2016-02-10',
                        rsvp_link='http://www.newamerica.org',
                        soundcloud_url='http://www.newamerica.org'))
Ejemplo n.º 4
0
 def setUp(self):
     self.login()
     self.root_page = Page.objects.get(id=1)
     self.home_page = self.root_page.add_child(instance=HomePage(
         title='New America'))
     self.all_policy_papers_home_page = self.home_page.add_child(
         instance=AllPolicyPapersHomePage(
             title='New America Policy Papers'))
     self.program_page_1 = self.home_page.add_child(
         instance=Program(title='OTI',
                          name='OTI',
                          description='OTI',
                          location=False,
                          depth=3))
     self.second_program = self.home_page.add_child(
         instance=Program(title='Education',
                          name='Education',
                          slug='education',
                          description='Education',
                          location=False,
                          depth=3))
     self.program_policy_papers_page = self.program_page_1\
         .add_child(instance=ProgramPolicyPapersPage(
             title='OTI Policy Papers')
         )
     self.policy_paper = PolicyPaper(title='Policy Paper 1',
                                     slug='policy-paper-1',
                                     date='2016-02-10',
                                     depth=5)
     self.program_policy_papers_page.add_child(instance=self.policy_paper)
     self.policy_paper.save()
Ejemplo n.º 5
0
    def setUp(self):
        self.login()
        site = Site.objects.get()
        page = Page.get_first_root_node()
        home = HomePage(title='New America')
        self.home_page = page.add_child(instance=home)

        site.root_page = home
        site.save()

        self.our_people_page = self.home_page.add_child(
            instance=OurPeoplePage(title='Our People', depth=3))
        self.program_page = self.home_page.add_child(
            instance=Program(title='OTI',
                             name='OTI',
                             description='OTI',
                             location=False,
                             depth=3))
        self.second_program = self.home_page.add_child(
            instance=Program(title='Education',
                             name='Education',
                             slug='education',
                             description='Education',
                             location=False,
                             depth=3))
        self.subprogram_page = self.program_page.add_child(
            instance=Subprogram(title='OTI Subprogram',
                                name='OTI Subprogram',
                                description='OTI Subprogram',
                                location=False,
                                depth=4))
        self.second_subprogram_page = self.program_page.add_child(
            instance=Subprogram(title='OTI Subprogram 2',
                                name='OTI Subprogram 2',
                                description='OTI Subprogram 2',
                                location=False,
                                depth=4))
        self.program_articles_page = self.program_page.add_child(
            instance=ProgramArticlesPage(title='Program Articles'))
        self.test_person = Person(
            title='Sana Javed',
            slug='sana-javed',
            first_name='Sana',
            last_name='Javed',
            role='Central Staff',
            depth=4,
        )
        self.our_people_page.add_child(instance=self.test_person)
        self.test_person.save()

        self.article = self.program_articles_page.add_child(
            instance=Article(title='Article 1', date='2016-02-02'))
        PostAuthorRelationship(author=self.test_person,
                               post=self.article).save()

        self.article_2 = self.program_articles_page.add_child(
            instance=Article(title='Article 2', date='2016-05-02'))
        PostAuthorRelationship(author=self.test_person,
                               post=self.article_2).save()
Ejemplo n.º 6
0
def test_clear_data():
    Program(name="test 1", description="desc").save()
    Program(name="test 2", description="desc").save()
    assert Program.objects.count() == 2

    seed.clear_data()

    assert Program.objects.count() == 0
Ejemplo n.º 7
0
def program_objects():
    """
    In real life, I would use factory_boy and set up
    factories so that I could easily add associated
    objects, and then test that the associated objects
    show up in my APIs.

    ex: Program has_many Sections
        Section has_many Activities

    For now, I'm just using a fixture to re-use to
    create Program objects.
    """
    Program(name="program name 1", description="program description 1").save()
    Program(name="program name 2", description="program description 2").save()
    Program(name="program name 2", description="program description 2").save()
Ejemplo n.º 8
0
    def save_file(uploaded_file, emission_date: str, program: Program):
        uploaded_file.name = "uploaded_" + uploaded_file.name
        uploaded_file_path = settings.FILE_UPLOAD_DIR + uploaded_file.name
        output_file_path = settings.FILE_UPLOAD_DIR + program.get_filename_for_date(
            emission_date)

        if os.path.isfile(uploaded_file_path) or os.path.isfile(
                output_file_path):
            raise FileBeingProcessedException

        upload_service = RemoteService()
        upload_service.check_file_for_date(program.normalized_name(),
                                           emission_date)

        with open(uploaded_file_path, 'wb+') as destination:
            for chunk in uploaded_file.chunks():
                destination.write(chunk)
Ejemplo n.º 9
0
def ad_media_loader(instance):
    percent_to_load = instance.percent_to_load
    sec_in_hour = 3600
    for area in instance.areas:
        area_stations_count = Station.objects.filter(
            area__in=instance.areas).count()
        stations_in_area = Station.objects.filter(
            area__in=instance.areas).order_by(
                '?')[:round(percent_to_load * (area_stations_count / 100))]
        for station in stations_in_area:
            """ check station for existed program for any needed hour in request  """
            for hour in instance.hours:
                """ Check for existed related hour program for station """
                program_rel = StationProgramRelation.objects.filter(
                    station=station, hour=hour).first()
                if program_rel:
                    try:
                        """ In case related hour program for station exist:

                            - create relation object for program <--> advertisment """
                        program = program_rel.program
                        program_duration = Ad.objects.filter(
                            pradmembership__program=program).aggregate(
                                Sum("duration"))["duration__sum"]
                        """ Check hour for enough free time for new advertisement (min 3 times in hour to show) """
                        if int((sec_in_hour - program_duration) /
                               instance.duration) >= 5:
                            ProgramAdMembership(
                                ad=instance,
                                program=program,
                                ad_index=ProgramAdMembership.objects.filter(
                                    program=program).count()).save()
                    except Exception as e:
                        raise e
                else:
                    try:
                        """ In case related hour program for station doesnt exist """
                        """ create program pbject """
                        Program(title=str(station.id) + "_" + hour,
                                description=str(station.id) + "_" + hour +
                                "_temporarily").save()
                        program = Program.objects.filter(
                            title=str(station.id) + "_" + hour,
                            description=str(station.id) + "_" + hour +
                            "_temporarily").first()
                        """ create relation object program <--> advertisment """
                        ProgramAdMembership(ad=instance,
                                            program=program,
                                            ad_index=0).save()
                        """ create relation object station <--> program (for current hour) """
                        StationProgramRelation(program=program,
                                               station=station,
                                               hour=hour).save()
                    except Exception as e:
                        raise e
        instance.is_active = True
        instance.save()
Ejemplo n.º 10
0
    def download_archive_file(self, program: Program, emission_date: str):
        filename = program.get_filename_for_date(emission_date)
        archive_url = "http://{}".format(settings.ARCHIVE_SERVER_IP)
        session = requests.Session()
        session.auth = (settings.ARCHIVE_SERVER_USERNAME,
                        settings.ARCHIVE_SERVER_PASSWORD)
        session.post(archive_url)

        r = session.get(archive_url + '/archive/' + program.normalized_name() +
                        "/" + filename,
                        stream=True)

        resp = StreamingHttpResponse(r.iter_content(8096))
        resp['headers'] = "X-Accel-Redirect"
        resp['Content-Disposition'] = 'attachment; filename={}'.format(
            filename)
        resp['Accept-Ranges'] = 'bytes'
        resp['Content-Type'] = 'audio/mpeg'

        return resp
Ejemplo n.º 11
0
 def setUp(self):
     self.login()
     self.root_page = Page.objects.get(id=1)
     self.home_page = self.root_page.add_child(instance=HomePage(
         title='New America'))
     self.program_page_1 = self.home_page.add_child(
         instance=Program(title='OTI',
                          name='OTI',
                          description='OTI',
                          location=False,
                          depth=3))
Ejemplo n.º 12
0
    def setUp(self):
        self.login()
        site = Site.objects.get()
        page = Page.get_first_root_node()
        home = HomePage(title='New America')
        home_page = page.add_child(instance=home)

        site.root_page = home
        site.save()

        # People objects to test template tags
        # that create byline and determine author order
        program_page_1 = home_page.add_child(
            instance=Program(title='OTI',
                             name='OTI',
                             slug='oti',
                             description='OTI',
                             location=False,
                             depth=3))
        program_page_1.save()

        our_people_page = home_page.add_child(instance=OurPeoplePage(
            title='Our People',
            depth=3,
        ))
        our_people_page.save()

        self.first_person = Person(
            title='First Person',
            slug='first-person',
            first_name='first',
            last_name='person',
            role='Central Staff',
            depth=4,
        )
        our_people_page.add_child(instance=self.first_person)

        # Using policy papers to test the other post types
        all_policy_papers_home_page = home_page.add_child(
            instance=AllPolicyPapersHomePage(title="Policy Papers"))

        program_policy_papers_page = program_page_1.add_child(
            instance=ProgramPolicyPapersPage(title='OTI Policy Papers',
                                             slug='oti-policy-papers'))
        self.policy_paper = PolicyPaper(title='Policy Paper 1',
                                        slug='policy-paper-1',
                                        date='2016-06-15',
                                        depth=5)
        program_policy_papers_page.add_child(instance=self.policy_paper)
        self.policy_paper.save()
        PostAuthorRelationship(author=self.first_person,
                               post=self.policy_paper).save()
        all_policy_papers_home_page.save()
Ejemplo n.º 13
0
def get_program(program_id):
    """
    Gets the program from the new database
    using the program_id from the 
    old database API 
    """
    program_id = str(program_id)
    old_program = mapped_programs[program_id]
    try:
        program = Program.objects.get(title=old_program)
    except ObjectDoesNotExist:
        program = Program(
            name=old_program,
            title=old_program,
            slug=slugify(old_program),
            description=old_program,
            depth=3,
        )
        home_page.add_child(instance=program)
        program.save()
    return program
Ejemplo n.º 14
0
    def create(self, validated_data):
        versions_data = validated_data.pop('versions')
        instance = Program(name=validated_data['name'])
        instance.save()

        for v in versions_data:
            version = Version(name=v['name'], program=instance)

            try:
                version.save()
            except IntegrityError:
                pass

        areas_data = validated_data.pop('areas')
        area_instances = []
        for v in areas_data:
            area = Area(name=v['name'], program=instance)
            area.save()
            try:
                area_instances.append(area)
            except IntegrityError:
                raise NotAcceptable(
                    detail={
                        'message': 'A version with this name already exist.',
                        'version': v
                    })
            instance.save()

        # instance = Program.objects.get(id=instance.id)
        return instance
Ejemplo n.º 15
0
 def setUp(self):
     self.login()
     self.root_page = Page.objects.get(id=1)
     self.home_page = self.root_page.add_child(instance=HomePage(
         title='New America'))
     self.program_page = self.home_page.add_child(
         instance=Program(title='OTI',
                          name='OTI',
                          description='OTI',
                          location=False,
                          depth=3))
     self.second_program = self.home_page.add_child(
         instance=Program(title='Education',
                          name='Education',
                          slug='education',
                          description='Education',
                          location=False,
                          depth=3))
     self.program_articles_page = self.program_page.add_child(
         instance=ProgramArticlesPage(title='Program Articles'))
     self.article = self.program_articles_page.add_child(
         instance=Article(title='Article 1', date='2016-02-02'))
Ejemplo n.º 16
0
 def setUp(self):
     self.login()
     self.root_page = Page.objects.get(id=1)
     self.home_page = self.root_page.add_child(instance=HomePage(
         title='New America'))
     self.program_page_1 = self.home_page.add_child(
         instance=Program(title='OTI',
                          name='OTI',
                          description='OTI',
                          location=False,
                          depth=3))
     self.second_program = self.home_page.add_child(
         instance=Program(title='Education',
                          name='Education',
                          slug='education',
                          description='Education',
                          location=False,
                          depth=3))
     self.program_blog_posts_page = self.program_page_1.add_child(
         instance=ProgramBlogPostsPage(title='OTI Blog'))
     self.blog_post = self.program_blog_posts_page.add_child(
         instance=BlogPost(title='Blog Post 1', date='2016-02-10'))
Ejemplo n.º 17
0
 def create(self, request):
     try:
         data = JSONParser().parse(request)
         serializer = ProgramSerializer(data=data)
         if serializer.is_valid():
             pr = Program()
             pr.name = serializer.validated_data.get('name')
             pr.description = serializer.validated_data.get('description')
             pr.level = serializer.validated_data.get('level')
             pr.weeks = serializer.validated_data.get('weeks')
             pr.save()
             predef_routines = []
             PF = serializer.validated_data.get('routines')
             for c in PF:
                 predef_routines.append(c.id)
             pr.predef_routines.set(predef_routines)
             pr.save()
             return Response(serializer.data,
                             status=status.HTTP_201_CREATED)
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
     except Program.DoesNotExist:
         return Response(status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 18
0
    def get_uploaded_dates(self, program: Program):
        self.open_ssh_archive()
        ftp_client = self.ssh_client.open_sftp()
        try:
            ftp_client.chdir(settings.ARCHIVE_SERVER_UPLOAD_DIRECTORY +
                             program.normalized_name())
        except IOError:
            return []

        files = ftp_client.listdir()
        self.close_connections()

        dates = []
        for file in files:
            date_weekday = file[:-4].replace(program.normalized_name(), "")
            if len(date_weekday) == 9:  # has weekday
                without_weekday = date_weekday[:-1]
                dates.append(without_weekday[:4] + "-" + without_weekday[4:6] +
                             "-" + without_weekday[6:])
            else:
                dates.append(date_weekday[:4] + "-" + date_weekday[4:6] + "-" +
                             date_weekday[6:])
        return dates
Ejemplo n.º 19
0
 def setUp(self):
     self.login()
     self.root_page = Page.objects.get(id=1)
     self.home_page = self.root_page.add_child(instance=HomePage(
         title='New America'))
     self.all_podcasts_home_page = self.home_page.add_child(
         instance=AllPodcastsHomePage(title="All Podcasts at New America!"))
     self.program_page_1 = self.home_page.add_child(
         instance=Program(title='OTI',
                          name='OTI',
                          description='OTI',
                          location=False,
                          depth=3))
     self.second_program = self.home_page.add_child(
         instance=Program(title='Education',
                          name='Education',
                          slug='education',
                          description='Education',
                          location=False,
                          depth=3))
     self.program_podcasts_page = self.program_page_1.add_child(
         instance=ProgramPodcastsPage(title='OTI Podcasts'))
     self.podcast = self.program_podcasts_page.add_child(
         instance=Podcast(title='Podcast 1', date='2016-02-10'))
Ejemplo n.º 20
0
    def setUp(self):
        self.login()
        site = Site.objects.get()
        page = Page.get_first_root_node()
        home = HomePage(title='New America')
        self.home_page = page.add_child(instance=home)
        self.root_page = Page.objects.get(id=1)

        site.root_page = home
        site.save()

        all_events_home_page = self.home_page.add_child(
            instance=AllEventsHomePage(title="Events"))

        self.program_page = self.home_page.add_child(
            instance=Program(title='OTI',
                             name='OTI',
                             description='OTI',
                             location=False,
                             depth=3))
        self.program_articles_page = self.program_page.add_child(
            instance=ProgramArticlesPage(title='Program Articles'))
        self.article = self.program_articles_page.add_child(
            instance=Article(title='Article 1', date='2016-02-02'))
        self.article.save()

        self.program_events_page = self.program_page.add_child(
            instance=ProgramEventsPage(title='OTI Events', slug='oti-events'))
        self.today_event = self.program_events_page.add_child(
            instance=Event(title='Today Event',
                           date=str(date.today()),
                           rsvp_link='http://www.newamerica.org',
                           soundcloud_url='http://www.newamerica.org'))
        self.future_event_morning = self.program_events_page.add_child(
            instance=Event(title='Future Event',
                           date=str(date.today() + timedelta(days=5)),
                           start_time=str((datetime.datetime.now() -
                                           timedelta(hours=5)).time()),
                           rsvp_link='http://www.newamerica.org',
                           soundcloud_url='http://www.newamerica.org'))
        self.future_event_afternoon = self.program_events_page.add_child(
            instance=Event(title='Future Event',
                           date=str(date.today() + timedelta(days=5)),
                           start_time=str((datetime.datetime.now() +
                                           timedelta(hours=5)).time()),
                           rsvp_link='http://www.newamerica.org',
                           soundcloud_url='http://www.newamerica.org'))
        self.past_event = self.program_events_page.add_child(
            instance=Event(title='Past Event',
                           date=str(date.today() - timedelta(days=5)),
                           rsvp_link='http://www.newamerica.org',
                           soundcloud_url='http://www.newamerica.org'))
        self.today_early_morning_event = self.program_events_page.add_child(
            instance=Event(title='Early Morning Event',
                           date=str(date.today()),
                           start_time=str((datetime.datetime.now() -
                                           timedelta(hours=5)).time()),
                           rsvp_link='http://www.newamerica.org',
                           soundcloud_url='http://www.newamerica.org'))
        self.multiday_event_ending_today = self.program_events_page.add_child(
            instance=Event(title='Early Morning Event',
                           date=str(date.today() - timedelta(days=5)),
                           end_date=str(date.today()),
                           rsvp_link='http://www.newamerica.org',
                           soundcloud_url='http://www.newamerica.org'))
 def handle(self, *args, **options):
     if Lift.objects.exists() or Program.objects.exists():
         print('Program data already loaded...exiting.')
         print(ALREADY_LOADED_ERROR_MESSAGE)
         return
     print("Creating lifts.")
     for lift_name in LIFT_NAMES:
         lift = Lift(exercise=lift_name)
         lift.save()
     print("Loading program data.")
     for row in DictReader(open('./program_data.csv')):
         program = Program()
         program.username = row['Username']
         program.program = row['Program']
         program.version = row['Version']
         program.description = row['Description']
         raw_submission_date = row['Date']
         submission_date = UTC.localize(
             datetime.strptime(raw_submission_date, DATETIME_FORMAT))
         program.date = submission_date
         program.save()
         raw_lift_names = row['Lifts']
         lift_names = [name for name in raw_lift_names.split(', ') if name]
         for lift_name in lift_names:
             lift = Lift.objects.get(exercise=lift_name)
             program.lifts.add(lift)
         program.save()
     print("Done.")
Ejemplo n.º 22
0
    def create_program(home_page, program_data={}):
        program = home_page.add_child(instance=Program(
            **PostFactory.program_data(**program_data)))

        return program
Ejemplo n.º 23
0
    def setUp(self):
        self.login()
        site = Site.objects.get()
        page = Page.get_first_root_node()
        home = HomePage(title='New America')
        home_page = page.add_child(instance=home)

        site.root_page = home
        site.save()

        all_events_home_page = home_page.add_child(instance=AllEventsHomePage(
            title="Events"))
        program_page_1 = home_page.add_child(
            instance=Program(title='OTI',
                             name='OTI',
                             slug='oti',
                             description='OTI',
                             location=False,
                             depth=3))
        program_page_1.save()
        second_program = home_page.add_child(
            instance=Program(title='Education',
                             name='Education',
                             slug='education',
                             description='Education',
                             location=False,
                             depth=3))
        second_program.save()
        program_events_page = program_page_1.add_child(
            instance=ProgramEventsPage(title='OTI Events', slug='oti-events'))
        program_events_page.save()
        second_program_events_page = second_program.add_child(
            instance=ProgramEventsPage(title='Education Events',
                                       slug='education-events'))
        second_program_events_page.save()
        today_event = program_events_page.add_child(
            instance=Event(title='Today Event',
                           date=str(date.today()),
                           rsvp_link='http://www.newamerica.org',
                           soundcloud_url='http://www.newamerica.org'))
        today_event.save()
        future_event = program_events_page.add_child(
            instance=Event(title='Future Event',
                           date=str(date.today() + timedelta(days=5)),
                           rsvp_link='http://www.newamerica.org',
                           soundcloud_url='http://www.newamerica.org'))
        future_event.save()
        past_event = program_events_page.add_child(
            instance=Event(title='Past Event',
                           date=str(date.today() - timedelta(days=5)),
                           rsvp_link='http://www.newamerica.org',
                           soundcloud_url='http://www.newamerica.org'))
        past_event.save()

        second_today_event = second_program_events_page.add_child(
            instance=Event(title='Today Event',
                           date=str(date.today()),
                           rsvp_link='http://www.newamerica.org',
                           soundcloud_url='http://www.newamerica.org'))
        second_today_event.save()
        second_future_event = second_program_events_page.add_child(
            instance=Event(title='Future Event',
                           date=str(date.today() + timedelta(days=5)),
                           rsvp_link='http://www.newamerica.org',
                           soundcloud_url='http://www.newamerica.org'))
        second_future_event.save()
        second_past_event = second_program_events_page.add_child(
            instance=Event(title='Past Event',
                           date=str(date.today() - timedelta(days=5)),
                           rsvp_link='http://www.newamerica.org',
                           soundcloud_url='http://www.newamerica.org'))
        second_past_event.save()