Пример #1
0
def track_view(request):
    if DEBUG:  # pragma: no cover
        print "============  T R A C K ==  V I E W =========================="
    id = request.matchdict['track_id']
    track = Track.get_by_track_id(id)

    # import pdb; pdb.set_trace()
    # redirect if id does not exist in database
    if not isinstance(track, Track):
        msg = "the track does not exist in the database!"
        return HTTPFound(route_url('not_found', request))
#     try:
#         print "== track.id: " + str(track.id)
#         print "== track.license.__len__(): " + str(track.license.__len__())
#     except AttributeError, a:
#         #'NoneType' object has no attribute 'license'
#         print "== AttributeError: "
#         print a
        # here we should redirect to NotFound or give some info

    #calculate for next/previous-navigation
    max_id = Track.get_max_id()
    # previous
    if track.id == 1:             # if looking at first id
        prev_id = max_id          # --> choose highest db row
    else:                         # if looking at any other id
        prev_id = track.id - 1    # --> choose previous
    # next
    if track.id != max_id:        # if not on highest id
        next_id = track.id + 1    # --> choose next
    elif track.id == max_id:      # if highest id
        next_id = 1               # --> choose first id ('wrap around'))

    # show who is watching. maybe we should log this ;-)
    viewer_username = authenticated_userid(request)
    #request.session.flash(
    #          "track.license.__len__(): " + str(track.license.__len__()))
    #request.session.flash("track.license.name: " + track.license[0].name)

    #request.session.flash(track.license.__len__())
# type(track.license) is  <class 'sqlalchemy.orm.collections.InstrumentedList'>

    if track.license.__len__() == 0:
        track_is_licensed = False
        license = License(name=u"All Rights Reserved.",
                          uri=u"", img=u"", author=u"default license")
        #request.session.flash("track_is_licensed: " + str(track_is_licensed))
    else:
        track_is_licensed = True
        license = track.license[0]
        #request.session.flash("track_is_licensed: " + str(track_is_licensed))
        #request.session.flash(
        #    "track.license.name: " + str(track.license[0].img))

    if DEBUG:  # pragma: no cover

        print "str(type(track.license)): " + str(type(license))
        print "str(dir(track.license)): " + str(dir(license))
        # print "str(help(track.license.pop())): "
        # + str(help(track.license.pop()))
        print "str(type(license)): " + str(type(license))
        # print "str(type(license.name)): " + str(type(license.name))
        # print str(dir(license))
        # print str(license.name)

    return {
        'track': track,
        'track_is_licensed': track_is_licensed,
        'license': license,
        'viewer_username': viewer_username,
        'prev_id': prev_id,
        'id': id,
        'next_id': next_id
        }