コード例 #1
0
ファイル: views.py プロジェクト: danbeaulieu/Twabble
def game(request, game_id=None):
    testdict['one'] = testdict['one'] + 1
    denied = None
    try:
        game = Game.objects.get(id=game_id)
        #members = game.members.all() - This gets all the User objects for this game through the membership relationship
        #words taken in this game
        wordsOwned = Ownership.objects.filter(game=game)
        if request.method == 'POST': # this means a join or a leave
            button = request.POST.get('Button', '')
            if button == 'Join Game':
                if game.get_access_display() != 'Public' and not allowed(game=game, user = request.user): # this is gross
                    denied = True
                else:
                    try:
                        m = Membership(gameid=game, userid=request.user, join_date=datetime.now(), total_score=0)
                        m.save()
                        follow(request)
                    except MySQLdb.IntegrityError:
                        pass
            if button == 'Leave Game':
                #delete membership
                try:
                    memb_to_leave = Membership.objects.get(gameid=game, userid=request.user)
                    memb_to_leave.delete()
                    # unfollow if memberships == 0
                except Membership.DoesNotExist:
                    pass 
            if button == 'Delete Game':
                try:
                    Membership.objects.filter(gameid=game).delete()
                    Ownership.objects.filter(game=game).delete()
                    #for mtd in memberships_to_delete:
                    #    mtd.delete()
                    game.delete()
                    # also delete ownerships
                    return games(request)
                except Game.DoesNotExist:
                    pass       
        memberships = Membership.objects.filter(gameid=game)
        tagCloudSize(memberships)
        if request.user.is_anonymous():
            m = None
        else:
            m = Membership.objects.get(gameid=game, userid=request.user)
    except Game.DoesNotExist:
        game = None
        m = None
        memberships = None
        wordsOwned = None
    except Membership.DoesNotExist:
        m = None
    if game != None and game.admin == request.user:
        #this is the admin
        return render_to_response('games/gameadmin.html', {'game' : game, 'member' : m, 'memberships' : memberships, 'wordsOwned' : wordsOwned}, context_instance = RequestContext(request))	
コード例 #2
0
ファイル: views.py プロジェクト: danbeaulieu/Twabble
@login_required
def join(request, game_id):
    # add a membership and follow this user
    try:
        g = Game.objects.get(id=game_id)
    except Game.DoesNotExist:
        g = None
        return render_to_response('games/game.html', {'game' : g, 'game_id' : game_id}, context_instance = RequestContext(request))
    if g.get_access_display() != 'Public':
        if allowed(game=g, user = request.user):
            return "you are allowed"
        else:
            return "You aren't friends"
        #else: don't make the membership, show an error
    m = Membership(gameid=g, userid=request.user, join_date=datetime.now())
    m.save()
    follow(request)
コード例 #3
0
ファイル: views.py プロジェクト: danbeaulieu/Twabble
#@login_required
def start(request):
    """Return form to make a game"""
    if request.method == 'POST': # If the form has been submitted...
        if not request.user.is_authenticated():
            return HttpResponseRedirect('/accounts/login/?next=%s' % request.path)
        form = NewGameForm(request.POST, httprequest=request) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # make a new game with this user as the admin, and add a membership relationship
            g = Game(name=form.cleaned_data['name'], admin=request.user, access=form.cleaned_data['invite'], creation_date=form.cleaned_data['start'])
            g.save()		
            m = Membership(gameid=g, userid=request.user, join_date=datetime.now(), total_score=0)
            m.save()
            follow(request)
            if form.cleaned_data['send_tweet']:
                access_token = request.session.get('access_token', None)
                access_token = oauth.OAuthToken.from_string(access_token)
                
                twitter_inst = oauthtwitter.OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token)
                twitter_inst.PostUpdate(status="I just created a game at Twabble.com . Join my game and play against me at http://www.PlayTwabble.com/g/%s" % g.id)
            #userprofile = request.user.get_profile()
            #if(userprofile.following == 0):
            #    # not following this user, need to to get the updates in our stream
            #    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
            #    # this creates a password manager
            #    passman.add_password(None, 'http://twitter.com', 'PlayTwabble', 'winter72')
            #    authhandler = urllib2.HTTPBasicAuthHandler(passman)
            #    # create the AuthHandler
            #    opener = urllib2.build_opener(authhandler)
            #    urllib2.install_opener(opener)
            #    req  = urllib2.Request('http://twitter.com/friendships/create/%s.json' % request.user, "")
            #    #urllib2.urlopen('http://twitter.com/friendships/create/%s.json' % request.user)
            #    try:				
            #        urllib2.urlopen(req)
            #        userprofile.following = 1
            #        userprofile.save()
            #    except:
            #        pass
				
            return HttpResponseRedirect('/g/%d' % (g.id) ) # Redirect after POST
    else:
        form = NewGameForm() # An unbound form