示例#1
0
def loginAction(request):
    """
    Method to handle login/register request.
    """
    if request.POST:
        p = request.POST
        # Retrieve or create the user.
        username = p.get('username', None)
        password = p.get('password', None)
        if not username or not password:
            return _redirect_home_with_msg(request, "Need username and password.")
        try:
            user = User.objects.get(username=username, password=password)
        except User.DoesNotExist:
            # Create the user and login
            user = create_user(request, username, password)
        user = authenticate(username=username, password=password)
        login(request, user)
        return _redirect_home_with_msg(request, "Welcome back!")
示例#2
0
def favorites(request):
    """
    Method which handles favorites request
    """
    if request.GET:
        g = request.GET
        action = g.get('a', None)
        fromS = g.get('fromS', None)
        viaS = g.get('viaS', None)
        ftype = g.get('type', DEPARTURES)
        
        # Check if we are trying to delete an existing favorite
        if action == 'd':
            id = g.get('id', None)
            if not id:
                return _redirect_home_with_msg(request, "ID missing. Cannot delete favorite.")
            try:
                fav = Favorite.objects.get(id=id, user=request.user)
                fav.delete()
                return _redirect_home_with_msg(request, "Favorite removed successfully.")
            except Favorite.DoesNotExist:
                return _redirect_home_with_msg(request, "Invalid favorite id.")
        
        if not fromS:
            return _redirect_home_with_msg(request, "location missing. cannot save favorite!")
        
        crslist = _getCRS(fromS)
        filterCrslist = _getCRS(viaS)
        if not crslist:
            return _redirect_home_with_msg(request, "Invalid station name")
        if len(crslist) > 1 or (filterCrslist and len(filterCrslist) > 1):
            return _redirect_home_with_msg(request, "Multiple station names retrieved.")
        
        fsn, crs = crslist[0]
        vsn = filterCrs = None
        if filterCrslist:
            vsn, filterCrs = filterCrslist[0]
        
        # Construct a good favorite name
        fname = fsn + ' ' + ftype
        desc = ''
        if vsn:
            desc = 'Via ' + vsn
        
        # See if the user is logged in.
        loggedIn = _checkLoggedIn(request)
        return render_to_response('fav.html', {'fname' : fname,
                                'fromS' : fromS,
                                'viaS' : viaS,
                                'ftype' : ftype,
                                'desc' : desc,
                                'loggedIn' : loggedIn,
                                },
                                context_instance=RequestContext(request))
    
    if request.POST:
        p = request.POST
        fromS = p.get('fromS', None)
        viaS = p.get('viaS', None)
        ftype = p.get('type', 'Departures')
        fname = p.get('fname', None)
        desc = p.get('desc', '')
        if not fname or not fromS:
            return _redirect_home_with_msg(request, "Missing values. Favorite not saved.")
        
        # Retrieve or create the user.
        username = p.get('username', None)
        password = p.get('password', None)
        loggedIn = _checkLoggedIn(request)
        
        if not loggedIn:
            if not username or not password or username.strip() == '' or password.strip() == '':
                return _redirect_home_with_msg(request, "Cannot save favorite without registering.")
        
        user = None
        if getattr(request, 'user'):
            user = request.user
        if not user or user.is_anonymous():
            try:
                user = User.objects.get(username=username, password=password)
            except User.DoesNotExist:
                # Create the user and login
                user = create_user(request, username, password)
                user = authenticate(username=username, password=password)
                login(request, user)
        
        # Go ahead and save the favorite
        fav = Favorite(user=user, fname=fname, desc=desc,
                       ftype=ftype, fromS=fromS, viaS=viaS)
        fav.save()
        return _redirect_home_with_msg(request, "Favorite saved.")