示例#1
0
def invite_players(request, game_id):
    game = get_object_or_404(Game, id=game_id)
    if not request.user.has_perm('edit_game', game) or not game.is_scheduled():
        raise PermissionDenied(
            "You don't have permission to edit this Game event, or it has already started"
        )
    initial_data = {"message": game.hook}
    if request.method == 'POST':
        form = CustomInviteForm(request.POST, initial=initial_data)
        if form.is_valid():
            player = get_object_or_404(
                User, username__iexact=form.cleaned_data['username'])
            if get_queryset_size(
                    player.game_invite_set.filter(relevant_game=game)) > 0:
                #player is already invited. Maybe update invitation instead?
                raise PermissionDenied("Player already invited")
            if player == game.creator or player == game.gm:
                raise PermissionDenied(
                    "You can't invite players to someone else's game")
            game_invite = Game_Invite(
                invited_player=player,
                relevant_game=game,
                invite_text=form.cleaned_data['message'],
                as_ringer=form.cleaned_data['invite_as_ringer'])
            with transaction.atomic():
                game_invite.save()
                game_invite.notify_invitee(request, game)
            return HttpResponseRedirect(
                reverse('games:games_view_game', args=(game.id, )))
        else:
            print(form.errors)
            return None
    else:
        return HttpResponseRedirect(
            reverse('games:games_view_game', args=(game.id, )))
示例#2
0
def create_game(request):
    if request.method == 'POST':
        form = make_game_form(user=request.user,
                              game_status=GAME_STATUS[0][0])(request.POST)
        if form.is_valid():
            game = Game(
                title=form.cleaned_data['title'],
                creator=request.user,
                gm=request.user,
                required_character_status=form.
                cleaned_data['required_character_status'],
                hook=form.cleaned_data['hook'],
                created_date=timezone.now(),
                scheduled_start_time=form.cleaned_data['scheduled_start_time'],
                open_invitations=form.cleaned_data['open_invitations'],
                status=GAME_STATUS[0][0],
                cell=form.cleaned_data['cell'])
            if form.cleaned_data['scenario']:
                game.scenario = form.cleaned_data['scenario']
            with transaction.atomic():
                game.save()
                if form.cleaned_data['invite_all_members']:
                    for member in game.cell.cellmembership_set.exclude(
                            member_player=game.gm):
                        game_invite = Game_Invite(
                            invited_player=member.member_player,
                            relevant_game=game,
                            invite_text=game.hook,
                            as_ringer=False)
                        game_invite.save()
                        game_invite.notify_invitee(request, game)
            game_url = reverse('games:games_view_game', args=(game.id, ))
            messages.add_message(
                request, messages.SUCCESS,
                mark_safe(
                    "Your Game has been created Successfully."
                    "<br>"
                    "<a href='" + game_url + "'> Click Here</a> "
                    "if you do not want to invite anyone else at this time."))
            return HttpResponseRedirect(
                reverse('games:games_invite_players', args=(game.id, )))
        else:
            print(form.errors)
            return None
    else:
        # Build a game form.
        form = make_game_form(user=request.user, game_status=GAME_STATUS[0][0])
        context = {
            'form': form,
        }
        return render(request, 'games/edit_game.html', context)
示例#3
0
def edit_game(request, game_id):
    game = get_object_or_404(Game, id=game_id)
    initial_data = {
        'hook': game.hook,
        'scenario': game.scenario,
        'required_character_status': game.required_character_status,
        'start_time': convert_to_localtime(game.scheduled_start_time),
        'cell': game.cell,
        'only_over_18': game.is_nsfw,
        'invitation_mode': game.invitation_mode,
        'list_in_lfg': game.list_in_lfg,
        'allow_ringers': game.allow_ringers,
        'max_rsvp': game.max_rsvp,
        'gametime_url': game.gametime_url,
        'mediums': game.mediums.all(),
    }
    GameForm = make_game_form(user=request.user)
    if not game.player_can_edit(request.user):
        raise PermissionDenied(
            "You don't have permission to edit this Game event.")
    if not game.is_scheduled():
        raise PermissionDenied(
            "You cannot edit a Game event once it has started.")
    if request.method == 'POST':
        form = GameForm(request.POST, initial=initial_data)
        if form.is_valid():
            title = form.cleaned_data[
                'title'] if "title" in form.cleaned_data and form.cleaned_data[
                    'title'] else "untitled"
            game.title = title
            game.hook = form.cleaned_data['hook']
            start_time = form.cleaned_data['scheduled_start_time']
            if "timezone" in form.cleaned_data:
                account = request.user.account
                account.timezone = form.cleaned_data["timezone"]
                account.save()
                start_time = change_time_to_current_timezone(start_time)
            game.scheduled_start_time = start_time
            game.required_character_status = form.cleaned_data[
                'required_character_status']
            game.scenario = form.cleaned_data['scenario']
            game.cell = form.cleaned_data['cell']
            game.invitation_mode = form.cleaned_data['invitation_mode']
            game.list_in_lfg = form.cleaned_data['list_in_lfg']
            game.allow_ringers = form.cleaned_data['allow_ringers']
            game.max_rsvp = form.cleaned_data['max_rsvp']
            game.gametime_url = form.cleaned_data['gametime_url']
            if 'only_over_18' in form.cleaned_data:
                game.is_nsfw = form.cleaned_data['only_over_18']
            if form.cleaned_data['scenario']:
                game.scenario = form.cleaned_data['scenario']
                game.title = game.scenario.title
            with transaction.atomic():
                game.save()
                game.mediums.set(form.cleaned_data['mediums'])
                if hasattr(form.changed_data, 'invite_all_members'
                           ) and form.cleaned_data['invite_all_members']:
                    for member in game.cell.cellmembership_set.exclude(
                            member_player=game.gm):
                        game_invite = Game_Invite(
                            invited_player=member.member_player,
                            relevant_game=game,
                            invite_text=game.hook,
                            as_ringer=False)
                        game_invite.save()
                        game_invite.notify_invitee(request, game)
            return HttpResponseRedirect(
                reverse('games:games_view_game', args=(game.id, )))
        else:
            print(form.errors)
            return None
    else:
        # Build a game form.
        form = GameForm(initial=initial_data)
        context = {
            'game': game,
            'form': form,
        }
        return render(request, 'games/edit_game.html', context)
示例#4
0
def create_game(request, cell_id=None):
    if not request.user.is_authenticated:
        raise PermissionDenied("You must be logged in to create a Game")
    cell = None
    if cell_id:
        cell = get_object_or_404(Cell, id=cell_id)
    GameForm = make_game_form(user=request.user)
    if request.method == 'POST':
        form = GameForm(request.POST)
        if form.is_valid():
            start_time = form.cleaned_data['scheduled_start_time']
            if "timezone" in form.cleaned_data:
                account = request.user.account
                account.timezone = form.cleaned_data["timezone"]
                account.save()
                start_time = change_time_to_current_timezone(start_time)
            title = form.cleaned_data[
                'title'] if "title" in form.cleaned_data and form.cleaned_data[
                    'title'] else "untitled"
            form_cell = form.cleaned_data['cell']
            if not form_cell.get_player_membership(request.user):
                raise PermissionDenied("You are not a member of this World.")
            if not (form_cell.player_can_manage_games(request.user)
                    or form_cell.player_can_run_games(request.user)):
                raise PermissionDenied(
                    "You do not have permission to run Games in this World")
            game = Game(
                title=title,
                creator=request.user,
                gm=request.user,
                required_character_status=form.
                cleaned_data['required_character_status'],
                hook=form.cleaned_data['hook'],
                created_date=timezone.now(),
                scheduled_start_time=start_time,
                status=GAME_STATUS[0][0],
                cell=form_cell,
                invitation_mode=form.cleaned_data['invitation_mode'],
                list_in_lfg=form.cleaned_data['list_in_lfg'],
                allow_ringers=form.cleaned_data['allow_ringers'],
                max_rsvp=form.cleaned_data['max_rsvp'],
                gametime_url=form.cleaned_data['gametime_url'],
            )
            if 'only_over_18' in form.cleaned_data:
                game.is_nsfw = form.cleaned_data['only_over_18']
            if form.cleaned_data['scenario']:
                game.scenario = form.cleaned_data['scenario']
                game.title = game.scenario.title
            with transaction.atomic():
                game.save()
                game.mediums.set(form.cleaned_data['mediums'])
                if form.cleaned_data['invite_all_members']:
                    for member in game.cell.cellmembership_set.exclude(
                            member_player=game.gm):
                        game_invite = Game_Invite(
                            invited_player=member.member_player,
                            relevant_game=game,
                            invite_text=game.hook,
                            as_ringer=False)
                        if member.member_player.has_perm(
                                "view_scenario", game.scenario):
                            game_invite.as_ringer = True
                        game_invite.save()
                        game_invite.notify_invitee(request, game)
            messages.add_message(
                request, messages.SUCCESS,
                mark_safe("Your Game has been created Successfully."))
            return HttpResponseRedirect(
                reverse('games:games_invite_players', args=(game.id, )))
        else:
            print(form.errors)
            return None
    else:
        # Build a game form.
        form = GameForm(initial={"cell": cell})
        context = {
            'form': form,
        }
        return render(request, 'games/edit_game.html', context)