Example #1
0
def edit_membership(request, slug):
    school = get_object_or_404(School, slug=slug)
    if request.method == 'POST':
        form = school_forms.SchoolAddCourseForm(request.POST)
        if form.is_valid():
            course_url = form.cleaned_data['course_url']
            try:
                add_course_to_list(course_url, slug)
                messages.success(request,
                                 _('The course was added to this school.'))
                return http.HttpResponseRedirect(
                    reverse('schools_edit_membership',
                            kwargs=dict(slug=school.slug)))
            except Exception as e:
                messages.error(request, e.message)
        else:
            messages.error(
                request,
                _("There was an error adding %s to this school.") % slug)
    else:
        form = school_forms.SchoolAddCourseForm()
    return render_to_response('schools/school_edit_membership.html', {
        'school': school,
        'form': form,
        'courses': get_courses_by_list(slug),
        'membership_tab': True,
    },
                              context_instance=RequestContext(request))
Example #2
0
def edit_featured(request, slug):
    school = get_object_or_404(School, slug=slug)
    if request.method == 'POST':
        form = school_forms.SchoolAddCourseForm(request.POST)
        if form.is_valid():
            course_url = form.cleaned_data['course_url']
            try:
                add_course_to_list(course_url, '{0}_featured'.format(slug))
                msg = _('The course is now featured for this school.')
                messages.success(request, msg)
                return http.HttpResponseRedirect(reverse(
                    'schools_edit_featured', kwargs=dict(slug=school.slug)))
            except:
                pass
        msg = _("There was an error marking the course as featured.")
        messages.error(request, msg)
    else:
        form = school_forms.SchoolAddCourseForm()

    featured = get_courses_by_list("{0}_featured".format(slug))
    return render_to_response('schools/school_edit_featured.html', {
        'school': school,
        'form': form,
        'featured': featured,
        'featured_tab': True,
    }, context_instance=RequestContext(request))
Example #3
0
    def update_learn_api(self):
        if not self.pk:
            return
        try:
            learn_model.update_course_listing(**self.get_learn_api_data())
        except:
            learn_model.add_course_listing(**self.get_learn_api_data())

        course_url = reverse('projects_show', kwargs={'slug': self.slug})
        course_lists = learn_model.get_lists_for_course(course_url)
        list_names = [ l['name'] for l in course_lists ]
        if self.not_listed or self.test:
            for list_name in list_names:
                learn_model.remove_course_from_list(course_url, list_name)
        else:
            desired_list = 'drafts'
            if not (self.under_development or self.archived):
                desired_list = 'listed'
            elif self.archived:
                desired_list = 'archived'
            possible_lists = ['drafts', 'listed', 'archived']
            possible_lists.remove(desired_list)
            for l in possible_lists:
                if l in list_names:
                    learn_model.remove_course_from_list(course_url, l)
            if desired_list not in list_names:
                learn_model.add_course_to_list(course_url, desired_list)
Example #4
0
def publish_course(course_uri):
    """ publish the course - also add course to the 'listed' list in the learn index """

    course_db = _get_course_db(course_uri)
    if not (course_db.draft or course_db.archived):
        return get_course(course_uri)

    course_db.draft = False
    course_db.archived = False
    course_db.save()

    course = get_course(course_uri)
    course_url = reverse("courses_slug_redirect", kwargs={"course_id": course["id"]})
    try:
        learn_model.remove_course_from_list(course_url, "drafts")
    except:
        try:
            learn_model.remove_course_from_list(course_url, "archived")
        except:
            pass

    learn_model.add_course_to_list(course_url, "listed")

    # TODO: Notify interested people in new course
    return course
Example #5
0
    def update_learn_api(self):
        if not self.pk:
            return
        try:
            learn_model.update_course_listing(**self.get_learn_api_data())
        except:
            learn_model.add_course_listing(**self.get_learn_api_data())

        course_url = reverse('projects_show', kwargs={'slug': self.slug})
        course_lists = learn_model.get_lists_for_course(course_url)
        list_names = [ l['name'] for l in course_lists ]
        if self.not_listed or self.test:
            for list_name in list_names:
                learn_model.remove_course_from_list(course_url, list_name)
        else:
            desired_list = 'drafts'
            if not (self.under_development or self.archived):
                desired_list = 'listed'
            elif self.archived:
                desired_list = 'archived'
            possible_lists = ['drafts', 'listed', 'archived']
            possible_lists.remove(desired_list)
            for l in possible_lists:
                if l in list_names:
                    learn_model.remove_course_from_list(course_url, l)
            if desired_list not in list_names:
                learn_model.add_course_to_list(course_url, desired_list)
Example #6
0
def unpublish_course(course_uri):
    course_db = _get_course_db(course_uri)

    if course_db.draft and not course_db.archived:
        return get_course(course_uri)

    course_db.archived = False
    course_db.draft = True
    course_db.save()

    course = get_course(course_uri)
    course_url = reverse('courses_slug_redirect',
                         kwargs={'course_id': course["id"]})

    try:
        learn_model.remove_course_from_list(course_url, 'listed')
    except:
        try:
            learn_model.remove_course_from_list(course_url, 'archived')
        except:
            log.error('Course not in any lists')

    learn_model.add_course_to_list(course_url, "drafts")

    return course
Example #7
0
def publish_course(course_uri):
    """ publish the course - also add course to the 'listed' list in the learn index """

    course_db = _get_course_db(course_uri)
    if not (course_db.draft or course_db.archived):
        return get_course(course_uri)

    course_db.draft = False
    course_db.archived = False
    course_db.save()

    course = get_course(course_uri)
    course_url = reverse('courses_slug_redirect',
                         kwargs={'course_id': course["id"]})
    try:
        learn_model.remove_course_from_list(course_url, "drafts")
    except:
        try:
            learn_model.remove_course_from_list(course_url, "archived")
        except:
            pass

    learn_model.add_course_to_list(course_url, "listed")

    # TODO: Notify interested people in new course
    return course
Example #8
0
def edit_membership(request, slug):
    school = get_object_or_404(School, slug=slug)
    if request.method == 'POST':
        form = school_forms.SchoolAddCourseForm(request.POST)
        if form.is_valid():
            course_url = form.cleaned_data['course_url']
            try:
                add_course_to_list(course_url, slug)
                messages.success(request,
                    _('The course was added to this school.'))
                return http.HttpResponseRedirect(reverse('schools_edit_membership',
                    kwargs=dict(slug=school.slug)))
            except Exception as e:   
                messages.error(request, e.message)
        else:
            messages.error(request,
                _("There was an error adding %s to this school.") % slug)
    else:
        form = school_forms.SchoolAddCourseForm()
    return render_to_response('schools/school_edit_membership.html', {
        'school': school,
        'form': form,
        'courses': get_courses_by_list(slug),
        'membership_tab': True,
    }, context_instance=RequestContext(request))
Example #9
0
def create_course(title,
                  hashtag,
                  description,
                  language,
                  organizer_uri,
                  based_on_uri=None):
    course_db = db.Course(title=title,
                          short_title=hashtag,
                          description=description,
                          language=language,
                          creator_uri=organizer_uri)

    if based_on_uri:
        based_on = _get_course_db(based_on_uri)
        course_db.based_on = based_on

    course_db.save()

    about = content_model.create_content(**{
        "title": _("About"),
        "content": "",
        "author_uri": organizer_uri
    })
    add_course_content("/uri/course/{0}".format(course_db.id), about['uri'])
    create_course_cohort("/uri/course/{0}".format(course_db.id), organizer_uri)
    course = get_course("/uri/course/{0}".format(course_db.id))

    learn_api_data = get_course_learn_api_data(course['uri'])
    learn_model.add_course_listing(**learn_api_data)
    learn_model.add_course_to_list(learn_api_data['course_url'], "drafts")

    # TODO notify admins
    return course
Example #10
0
    def update_learn_api(self):
        if not self.pk:
            return
        try:
            learn_model.update_course_listing(**self.get_learn_api_data())
        except:
            learn_model.add_course_listing(**self.get_learn_api_data())

        course_url = reverse("projects_show", kwargs={"slug": self.slug})
        course_lists = learn_model.get_lists_for_course(course_url)
        list_names = [l["name"] for l in course_lists]
        if self.not_listed or self.test:
            for list_name in list_names:
                learn_model.remove_course_from_list(course_url, list_name)
        else:
            desired_list = "drafts"
            if not (self.under_development or self.archived):
                desired_list = "listed"
            elif self.archived:
                desired_list = "archived"
            possible_lists = ["drafts", "listed", "archived"]
            possible_lists.remove(desired_list)
            for l in possible_lists:
                if l in list_names:
                    learn_model.remove_course_from_list(course_url, l)
            if desired_list not in list_names:
                learn_model.add_course_to_list(course_url, desired_list)
Example #11
0
def edit_featured(request, slug):
    school = get_object_or_404(School, slug=slug)
    if request.method == 'POST':
        form = school_forms.SchoolAddCourseForm(request.POST)
        if form.is_valid():
            course_url = form.cleaned_data['course_url']
            try:
                add_course_to_list(course_url, '{0}_featured'.format(slug))
                msg = _('The course is now featured for this school.')
                messages.success(request, msg)
                return http.HttpResponseRedirect(
                    reverse('schools_edit_featured',
                            kwargs=dict(slug=school.slug)))
            except:
                pass
        msg = _("There was an error marking the course as featured.")
        messages.error(request, msg)
    else:
        form = school_forms.SchoolAddCourseForm()

    featured = get_courses_by_list("{0}_featured".format(slug))
    return render_to_response('schools/school_edit_featured.html', {
        'school': school,
        'form': form,
        'featured': featured,
        'featured_tab': True,
    },
                              context_instance=RequestContext(request))
Example #12
0
def unpublish_course(course_uri):
    course_db = _get_course_db(course_uri)

    if course_db.draft and not course_db.archived:
        return get_course(course_uri)

    course_db.archived = False
    course_db.draft = True
    course_db.save()

    course = get_course(course_uri)
    course_url = reverse(
        'courses_slug_redirect',
        kwargs={'course_id': course["id"]}
    )

    try:
        learn_model.remove_course_from_list(course_url, 'listed')
    except:
        try:
            learn_model.remove_course_from_list(course_url, 'archived')
        except:
            log.error('Course not in any lists')

    learn_model.add_course_to_list(course_url, "drafts")

    return course
Example #13
0
def archive_course(course_uri):
    """ mark a course as archived - this will also disable editing of the course """
    course_db = _get_course_db(course_uri)
    if course_db.archived:
        return get_course(course_uri)

    course_db.archived = True
    course_db.save()

    course = get_course(course_uri)
    course_url = reverse(
        'courses_slug_redirect',
        kwargs={'course_id': course["id"]}
    )

    try:
        learn_model.remove_course_from_list(course_url, "listed")
    except:
        try: 
            learn_model.remove_course_from_list(course_url, "drafts")
        except:
            log.error('Course not in any lists')

    learn_model.add_course_to_list(course_url, "archived")
    return course
Example #14
0
def create_course(title, hashtag, description, language, organizer_uri, based_on_uri=None):
    course_db = db.Course(
        title=title,
        short_title=hashtag,
        description=description,
        language=language,
        creator_uri=organizer_uri
    )

    if based_on_uri:
        based_on = _get_course_db(based_on_uri)
        course_db.based_on = based_on

    course_db.save()

    about = content_model.create_content(
       **{"title": _("About"), "content": "", "author_uri": organizer_uri}
    )
    add_course_content("/uri/course/{0}".format(course_db.id), about['uri'])
    create_course_cohort("/uri/course/{0}".format(course_db.id), organizer_uri)
    course = get_course("/uri/course/{0}".format(course_db.id))

    learn_api_data = get_course_learn_api_data(course['uri'])
    learn_model.add_course_listing(**learn_api_data)
    learn_model.add_course_to_list(learn_api_data['course_url'], "drafts")

    # TODO notify admins
    return course
Example #15
0
    def test_course_list(self):
        """ test that course can be added to and removed from a list """

        add_course_listing(**self.test_course)
        create_list("test_list", "Test List", "")
        add_course_to_list(self.test_course["course_url"], "test_list")
        course_list = get_courses_by_list("test_list")

        self.assertTrue(len(course_list) == 1)

        remove_course_from_list(self.test_course["course_url"], "test_list")
        course_list = get_courses_by_list("test_list")

        self.assertTrue(len(course_list) == 0)
Example #16
0
    def test_course_list(self):
        """ test that course can be added to and removed from a list """

        add_course_listing(**self.test_course)
        create_list("test_list", "Test List", "")
        add_course_to_list(self.test_course["course_url"], "test_list")
        course_list = get_courses_by_list("test_list")

        self.assertTrue(len(course_list) == 1)

        remove_course_from_list(self.test_course["course_url"], "test_list")
        course_list = get_courses_by_list("test_list")

        self.assertTrue(len(course_list) == 0)
Example #17
0
def archive_course(course_uri):
    """ mark a course as archived - this will also disable editing of the course """
    course_db = _get_course_db(course_uri)
    if course_db.archived:
        return get_course(course_uri)

    course_db.archived = True
    course_db.save()

    course = get_course(course_uri)
    course_url = reverse('courses_slug_redirect',
                         kwargs={'course_id': course["id"]})

    try:
        learn_model.remove_course_from_list(course_url, "listed")
    except:
        try:
            learn_model.remove_course_from_list(course_url, "drafts")
        except:
            log.error('Course not in any lists')

    learn_model.add_course_to_list(course_url, "archived")
    return course
Example #18
0
    def handle(self, *args, **options):
        listed = Project.objects.filter(not_listed=False,
                                        deleted=False,
                                        archived=False,
                                        test=False)

        # create lists for schools
        for school in School.objects.all():
            try:
                create_list(
                    school.slug, school.name,
                    "http://p2pu.org/en/schools/{0}".format(school.slug))
            except:
                pass

            try:
                create_list(
                    "{0}_featured".format(school.slug),
                    "{0} featured".format(school.name),
                    "http://p2pu.org/en/schools/{0}".format(school.slug))
            except:
                pass

        # create list for listed courses
        try:
            create_list("listed", "Listed courses", "")
        except:
            pass

        # create list for draft courses
        try:
            create_list("drafts", "Draft courses", "")
        except:
            pass

        listed = listed.filter(
            Q(category=Project.CHALLENGE)
            | Q(sign_up__status=Signup.MODERATED)
            | Q(sign_up__status=Signup.NON_MODERATED))

        listed = listed.order_by('created_on')

        for project in listed:
            project_tags = project.tags.all().values_list('name', flat=True)
            args = dict(course_url="/en/groups/{0}/".format(project.slug),
                        title=project.name,
                        description=project.short_description,
                        data_url="/en/groups/{0}/data".format(project.slug),
                        language=project.language,
                        thumbnail_url="http://p2pu.org{0}".format(
                            project.get_image_url()),
                        tags=project_tags)
            try:
                add_course_listing(**args)
            except:
                update_course_listing(**args)

            if project.under_development == True:
                try:
                    add_course_to_list(args["course_url"], "drafts")
                except:
                    pass
            else:
                try:
                    add_course_to_list(args["course_url"], "listed")
                except:
                    pass

            if project.school:
                try:
                    add_course_to_list(args['course_url'], project.school.slug)
                except:
                    pass

        # create lists
        try:
            create_list('community', "Community Picks", "")
        except:
            pass

        try:
            create_list('showcase', "Showcased", "")
        except:
            pass

        for project in listed.filter(community_featured=True):
            course_url = "/en/groups/{0}/".format(project.slug)
            try:
                add_course_to_list(course_url, 'community')
            except:
                pass

        for project in listed.filter(featured=True):
            course_url = "/en/groups/{0}/".format(project.slug)
            add_course_to_list(course_url, 'showcase')

        for school in School.objects.all():
            for project in school.featured.all():
                course_url = "/en/groups/{0}/".format(project.slug)
                list_name = "{0}_featured".format(school.slug)
                try:
                    add_course_to_list(course_url, list_name)
                except:
                    pass
    def handle(self, *args, **options):
        listed = Project.objects.filter(
            not_listed=False,
            deleted=False,
            archived=False,
            test=False
        )

        # create lists for schools
        for school in School.objects.all():
            try:
                create_list(
                    school.slug, school.name, 
                    "http://p2pu.org/en/schools/{0}".format(school.slug)
                )
            except:
                pass

            try:
                create_list(
                    "{0}_featured".format(school.slug),
                    "{0} featured".format(school.name),
                    "http://p2pu.org/en/schools/{0}".format(school.slug)
                )
            except:
                pass

        # create list for listed courses
        try:
            create_list("listed", "Listed courses", "")
        except:
            pass

        # create list for draft courses
        try:
            create_list("drafts", "Draft courses", "")
        except:
            pass


        listed = listed.filter(
            Q(category=Project.CHALLENGE)
            | Q(sign_up__status=Signup.MODERATED)
            | Q(sign_up__status=Signup.NON_MODERATED)
        )

        listed = listed.order_by('created_on')

        for project in listed:
            project_tags = project.tags.all().values_list('name', flat=True)
            args = dict(
                course_url = "/en/groups/{0}/".format(project.slug),
                title = project.name,
                description = project.short_description,
                data_url = "/en/groups/{0}/data".format(project.slug),
                language = project.language,
                thumbnail_url = "http://p2pu.org{0}".format(project.get_image_url()),
                tags = project_tags
            )
            try:
                add_course_listing(**args)
            except:
                update_course_listing(**args)

            if project.under_development == True:
                try:
                    add_course_to_list(args["course_url"], "drafts")
                except:
                    pass
            else:
                try:
                    add_course_to_list(args["course_url"], "listed")
                except:
                    pass

            if project.school:
                try:
                    add_course_to_list(args['course_url'], project.school.slug)
                except:
                    pass

        # create lists
        try:
            create_list('community', "Community Picks", "")
        except:
            pass

        try:
            create_list('showcase', "Showcased", "")
        except:
            pass

        for project in listed.filter(community_featured=True):
            course_url = "/en/groups/{0}/".format(project.slug)
            try:
                add_course_to_list(course_url, 'community')
            except:
                pass

        for project in listed.filter(featured=True):
            course_url = "/en/groups/{0}/".format(project.slug)
            add_course_to_list(course_url, 'showcase')

        for school in School.objects.all():
            for project in school.featured.all():
                course_url = "/en/groups/{0}/".format(project.slug)
                list_name = "{0}_featured".format(school.slug)
                try:
                    add_course_to_list(course_url, list_name)
                except:
                    pass