Beispiel #1
0
def category(request, slug):
    category = shortcuts.get_object_or_404(Category, slug=slug)
    tutorials = utils.fetch_tutorials()

    tutorials_to_display = []
    for tut in tutorials:
        if tut[0]['category'] != category.id:
            continue

        tutorials_to_display.append(tut)

    context = utils.get_global_context(request)
    context.update({
        'current_page':
        'category',
        'category':
        category,
        'tutorials_to_display':
        tutorials_to_display,
        'show_sharing_buttons':
        settings.SHOW_SHARING_BUTTONS,
        'show_sharing_buttons_inline':
        settings.SHOW_SHARING_BUTTONS_INLINE
    })

    return render(request, 'category.html', context)
Beispiel #2
0
def index(request):
    """
    The home page
    """
    global featured_tutorials, recent_tutorials
    context = utils.get_global_context(request)
    context.update({'current_page': 'home'})

    if settings.DEBUG:
        featured_tutorials = None
        recent_tutorials = None

    # Fetch the first three featured tutorials
    # Fetch the 3 recent tutorials
    if not featured_tutorials:
        featured_tutorials = list(Tutorial.objects.filter(featured=True).order_by('-date')[0:4])

    if not recent_tutorials:
        recent_tutorials = utils.fetch_tutorials(8)

    categories = None
    if settings.DEBUG:
        categories = list(Category.objects.all().order_by('order'))
    else:
        categories = list(Category.objects.filter(hidden=False).order_by('order'))

    context.update({'featured': featured_tutorials,
                    'recent_tutorials': recent_tutorials,
                    'categories': list(Category.objects.filter(hidden=False).order_by('order'))},
                    )
    return render(request, "index.html", context)
Beispiel #3
0
def index(request):
    """
    The home page
    """
    context = utils.get_global_context(request)
    context.update({'current_page': 'home'})

    # Fetch the first three featured tutorials
    # Fetch the 3 recent tutorials
    featured_tutorials = Tutorial.objects.filter(featured=True)
    context.update({'featured': featured_tutorials, 'recent_tutorials': utils.fetch_tutorials(3)})

    return render(request, "index.html", context)
Beispiel #4
0
def index(request):
    """
    The home page
    """
    context = utils.get_global_context(request)
    context.update({'current_page': 'home'})

    # Fetch the first three featured tutorials
    # Fetch the 3 recent tutorials
    featured_tutorials = Tutorial.objects.filter(featured=True)
    context.update({
        'featured': featured_tutorials,
        'recent_tutorials': utils.fetch_tutorials(8)
    })

    return render(request, "index.html", context)
Beispiel #5
0
def category(request, slug):
    category = shortcuts.get_object_or_404(Category, slug=slug)
    tutorials = utils.fetch_tutorials()

    tutorials_to_display = []
    for tut in tutorials:
        if tut[0]['category'] != category.id:
            continue

        tutorials_to_display.append(tut)

    context = utils.get_global_context(request)
    context.update({'current_page': 'category',
                    'category': category,
                    'tutorials_to_display': tutorials_to_display,
                    'show_sharing_buttons': settings.SHOW_SHARING_BUTTONS,
                    'show_sharing_buttons_inline': settings.SHOW_SHARING_BUTTONS_INLINE})

    return render(request, 'category.html', context)
Beispiel #6
0
def index(request):
    """
    The home page
    """
    global featured_tutorials, recent_tutorials
    context = utils.get_global_context(request)
    context.update({'current_page': 'home'})

    if settings.DEBUG:
        featured_tutorials = None
        recent_tutorials = None

    # Fetch the first three featured tutorials
    # Fetch the 3 recent tutorials
    if not featured_tutorials:
        featured_tutorials = list(
            Tutorial.objects.filter(featured=True).order_by('-date')[0:4])

    if not recent_tutorials:
        recent_tutorials = utils.fetch_tutorials(8)

    categories = None
    if settings.DEBUG:
        categories = list(Category.objects.all().order_by('order'))
    else:
        categories = list(
            Category.objects.filter(hidden=False).order_by('order'))

    context.update(
        {
            'featured':
            featured_tutorials,
            'recent_tutorials':
            recent_tutorials,
            'categories':
            list(Category.objects.filter(hidden=False).order_by('order'))
        }, )
    return render(request, "index.html", context)
Beispiel #7
0
def tutorials(request, slug=None):
    """
    The tutorials home page
    """
    _num_related = knobs.num_related

    context = utils.get_global_context(request)
    context.update({'current_page': 'tutorials'})

    if slug:
        # This section defines what happens if a tutorial slug is mentioned
        tutorial = shortcuts.get_object_or_404(Tutorial, slug=slug)
        author = utils.get_aishack_user(tutorial.author)

        # Check with track this tutorial belongs to
        tt = TrackTutorials.objects.filter(tutorial=tutorial)
        if tt:
            track = tt[0].track
            track_length = track.tutorial_count()
        else:
            # Just pick the first one
            track = None
            track_length = 0

        page_title = tutorial.title
        if tutorial.series:
            page_title = "%s: %s" % (tutorial.series.name, tutorial.title)

        context.update({'tutorial': tutorial,
                        'track': track,
                        'track_length': track_length,
                        'tuts_in_track_read': 0,
                        'tuts_in_track_read_percent': 0,
                        'page_title': page_title,
                        'author': author.user,
                        'category_slug': tutorial.category.slug,
                        'author_email_md5': md5(author.user.email).hexdigest(),
                        'aishackuser': author,
                        'show_sharing_buttons': settings.SHOW_SHARING_BUTTONS,
                        'show_sharing_buttons_inline': settings.SHOW_SHARING_BUTTONS_INLINE})

        context.update({
            'meta_title': tutorial.title, 
            'meta_description': tutorial.excerpt,
            'meta_thumb': tutorial.post_thumb,
        })

        # Increment the read counter
        tutorial.read_count = tutorial.read_count + 1
        tutorial.save(update_fields=['read_count'])

        # The user isn't logged in - display the pre-processed related tutorials
        related_list = tutorial.related.all()[0:_num_related]

        series_first = None
        if tutorial.series:
            series_first = tutorial.series.tutorials.all()[0]

        context.update({'related_tuts': related_list, 'series_first': series_first})
    else:
        # Fetch all the tracks
        tracks = Track.objects.all()
        context.update({'tracks': tracks})

        # This section defines what happens if the url is just /tutorials/
        # fetch_tutorials discards tutorials that are part of a series and only
        # returns the first part (along with a list of parts in the series)

        output = utils.fetch_tutorials()
        tutorials_to_display = {}

        for tut in output:
            category = Category.objects.get(pk=tut[0]['category'])
            tutorials_to_display.setdefault(category, [])
            tutorials_to_display[category].append(tut)

        context.update({'tutorials_to_display': tutorials_to_display})

        if settings.DEBUG:
            # Debug mode, we show all categories
            categories = list(Category.objects.all().order_by('order'))
        else:
            # Production mode, we show only what's required
            categories = list(Category.objects.filter(hidden=False).order_by('order'))

        context.update({'categories': categories})

    return render(request, "tutorials.html", context)
Beispiel #8
0
def tutorials(request, slug=None):
    """
    The tutorials home page
    """
    _num_related = 3

    context = utils.get_global_context(request)
    context.update({'current_page': 'tutorials'})

    if slug:
        # This section defines what happens if a tutorial slug is mentioned
        tutorial = shortcuts.get_object_or_404(Tutorial, slug=slug)
        author = utils.get_aishack_user(tutorial.author)

        # Check with track this tutorial belongs to
        tt = TrackTutorials.objects.filter(tutorial=tutorial)
        if tt:
            track = tt[0].track
            track_length = track.tutorial_count()
        else:
            # Just pick the first one
            track = None
            track_length = 0

        context.update({'tutorial': tutorial,
                        'track': track,
                        'track_length': track_length,
                        'tuts_in_track_read': 0,
                        'tuts_in_track_read_percent': 0,
                        'page_title': tutorial.title,
                        'author': author.user,
                        'category_slug': tutorial.category.slug,
                        'author_email_md5': md5(author.user.email).hexdigest(),
                        'aishackuser': author})

        # Increment the read counter
        tutorial.read_count = tutorial.read_count + 1
        tutorial.save(update_fields=['read_count'])

        if request.user.is_authenticated():
            aishack_user = utils.get_aishack_user(request.user)
            m = TutorialRead(tutorial=tutorial, user=aishack_user)
            m.save()

            # The user is logged in - update the related list based on which tutorials have
            # already been read
            related_list = []
            for tut in tutorial.related.all():
                all_read = aishack_user.tutorials_read.all()

                for t in all_read:
                    if tut.pk == t.pk:
                        break
                else:
                    related_list.append(tut)

                    if len(related_list) == _num_related:
                        break

            # Check if the user has signed up for the track
            tuts_read = aishack_user.tutorials_read.all()
            if track in aishack_user.tracks_following.all():
                tuts_in_track = track.tutorials.all()
                track_tuts_read = []

                tuts_in_track_read = 0
                for tut in tuts_in_track:
                    if tut in tuts_read:
                        tuts_in_track_read += 1
                        track_tuts_read.append(tut)

                context.update({'track_following': True,
                                'tuts_in_track_read': tuts_in_track_read,
                                'tuts_in_track_read_percent': tuts_in_track_read*100/track_length,
                                'track_tuts_read': track_tuts_read})

            # Maybe the visitor has read everything?
            # TODO
            if len(related_list) < _num_related:
                # fetch three random indices
                related_list.append(0)
        else:
            # The user isn't logged in - display the pre-processed related tutorials
            related_list = tutorial.related.all()[0:3]

        context.update({'related_tuts': related_list})
    else:
        # Fetch tracks the user is following
        if request.user.is_authenticated():
            aishack_user = utils.get_aishack_user(request.user)
            tracks_following = aishack_user.tracks_following.all()
        else:
            tracks_following = []

        # Fetch all the tracks
        tracks = Track.objects.all()
        context.update({'tracks': tracks, 'tracks_following': tracks_following})

        # This section defines what happens if the url is just /tutorials/
        # fetch_tutorials discards tutorials that are part of a series and only
        # returns the first part (along with a list of parts in the series)
        tutorials_to_display = utils.fetch_tutorials()
        context.update({'tutorials_to_display': tutorials_to_display})

    return render(request, "tutorials.html", context)
Beispiel #9
0
def tutorials(request, slug=None):
    """
    The tutorials home page
    """
    _num_related = knobs.num_related

    context = utils.get_global_context(request)
    context.update({'current_page': 'tutorials'})

    if slug:
        # This section defines what happens if a tutorial slug is mentioned
        tutorial = shortcuts.get_object_or_404(Tutorial, slug=slug)
        author = utils.get_aishack_user(tutorial.author)

        # Check with track this tutorial belongs to
        tt = TrackTutorials.objects.filter(tutorial=tutorial)
        if tt:
            track = tt[0].track
            track_length = track.tutorial_count()
        else:
            # Just pick the first one
            track = None
            track_length = 0

        page_title = tutorial.title
        if tutorial.series:
            page_title = "%s: %s" % (tutorial.series.name, tutorial.title)

        context.update({
            'tutorial':
            tutorial,
            'track':
            track,
            'track_length':
            track_length,
            'tuts_in_track_read':
            0,
            'tuts_in_track_read_percent':
            0,
            'page_title':
            page_title,
            'author':
            author.user,
            'category_slug':
            tutorial.category.slug,
            'author_email_md5':
            md5(author.user.email).hexdigest(),
            'aishackuser':
            author,
            'show_sharing_buttons':
            settings.SHOW_SHARING_BUTTONS,
            'show_sharing_buttons_inline':
            settings.SHOW_SHARING_BUTTONS_INLINE
        })

        context.update({
            'meta_title': tutorial.title,
            'meta_description': tutorial.excerpt,
            'meta_thumb': tutorial.post_thumb,
        })

        # Increment the read counter
        tutorial.read_count = tutorial.read_count + 1
        tutorial.save(update_fields=['read_count'])

        # The user isn't logged in - display the pre-processed related tutorials
        related_list = tutorial.related.all()[0:_num_related]

        series_first = None
        if tutorial.series:
            series_first = tutorial.series.tutorials.all()[0]

        context.update({
            'related_tuts': related_list,
            'series_first': series_first
        })
    else:
        # Fetch all the tracks
        tracks = Track.objects.all()
        context.update({'tracks': tracks})

        # This section defines what happens if the url is just /tutorials/
        # fetch_tutorials discards tutorials that are part of a series and only
        # returns the first part (along with a list of parts in the series)

        output = utils.fetch_tutorials()
        tutorials_to_display = {}

        for tut in output:
            category = Category.objects.get(pk=tut[0]['category'])
            tutorials_to_display.setdefault(category, [])
            tutorials_to_display[category].append(tut)

        context.update({'tutorials_to_display': tutorials_to_display})

        if settings.DEBUG:
            # Debug mode, we show all categories
            categories = list(Category.objects.all().order_by('order'))
        else:
            # Production mode, we show only what's required
            categories = list(
                Category.objects.filter(hidden=False).order_by('order'))

        context.update({'categories': categories})

    return render(request, "tutorials.html", context)
Beispiel #10
0
def tutorials(request, slug=None):
    """
    The tutorials home page
    """
    _num_related = 3

    context = utils.get_global_context(request)
    context.update({'current_page': 'tutorials'})

    if slug:
        # This section defines what happens if a tutorial slug is mentioned
        tutorial = shortcuts.get_object_or_404(Tutorial, slug=slug)
        author = utils.get_aishack_user(tutorial.author)

        # Check with track this tutorial belongs to
        tt = TrackTutorials.objects.filter(tutorial=tutorial)
        if tt:
            track = tt[0].track
            track_length = track.tutorial_count()
        else:
            # Just pick the first one
            track = None
            track_length = 0

        context.update({
            'tutorial': tutorial,
            'track': track,
            'track_length': track_length,
            'tuts_in_track_read': 0,
            'tuts_in_track_read_percent': 0,
            'page_title': tutorial.title,
            'author': author.user,
            'category_slug': tutorial.category.slug,
            'author_email_md5': md5(author.user.email).hexdigest(),
            'aishackuser': author
        })

        context.update({
            'meta_title': tutorial.title,
            'meta_description': tutorial.excerpt,
            'meta_thumb': tutorial.post_thumb,
        })

        # Increment the read counter
        tutorial.read_count = tutorial.read_count + 1
        tutorial.save(update_fields=['read_count'])

        if request.user.is_authenticated():
            aishack_user = utils.get_aishack_user(request.user)
            m = TutorialRead(tutorial=tutorial, user=aishack_user)
            m.save()

            # The user is logged in - update the related list based on which tutorials have
            # already been read
            related_list = []
            for tut in tutorial.related.all():
                all_read = aishack_user.tutorials_read.all()

                for t in all_read:
                    if tut.pk == t.pk:
                        break
                else:
                    related_list.append(tut)

                    if len(related_list) == _num_related:
                        break

            # Check if the user has signed up for the track
            tuts_read = aishack_user.tutorials_read.all()
            if track in aishack_user.tracks_following.all():
                tuts_in_track = track.tutorials.all()
                track_tuts_read = []

                tuts_in_track_read = 0
                for tut in tuts_in_track:
                    if tut in tuts_read:
                        tuts_in_track_read += 1
                        track_tuts_read.append(tut)

                context.update({
                    'track_following':
                    True,
                    'tuts_in_track_read':
                    tuts_in_track_read,
                    'tuts_in_track_read_percent':
                    tuts_in_track_read * 100 / track_length,
                    'track_tuts_read':
                    track_tuts_read
                })

            # Maybe the visitor has read everything?
            # TODO
            if len(related_list) < _num_related:
                # fetch three random indices
                related_list.append(0)
        else:
            # The user isn't logged in - display the pre-processed related tutorials
            related_list = tutorial.related.all()[0:3]

        context.update({'related_tuts': related_list})
    else:
        # Fetch tracks the user is following
        if request.user.is_authenticated():
            aishack_user = utils.get_aishack_user(request.user)
            tracks_following = aishack_user.tracks_following.all()
        else:
            tracks_following = []

        # Fetch all the tracks
        tracks = Track.objects.all()
        context.update({
            'tracks': tracks,
            'tracks_following': tracks_following
        })

        # This section defines what happens if the url is just /tutorials/
        # fetch_tutorials discards tutorials that are part of a series and only
        # returns the first part (along with a list of parts in the series)
        tutorials_to_display = utils.fetch_tutorials()
        context.update({'tutorials_to_display': tutorials_to_display})

    return render(request, "tutorials.html", context)