-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modify game-event html for adding the chat html adding tests for the chat adding new view function that handle the chat adding urls for the new view function
- Loading branch information
1 parent
57e7242
commit 56d83ec
Showing
7 changed files
with
403 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,36 @@ | ||
from django.db import models | ||
from player.models import Player | ||
from game_event.models import GameEvent | ||
from game_event_player.models import GameEventPlayer | ||
from django.core.validators import MinLengthValidator | ||
from django.core.exceptions import ValidationError | ||
|
||
|
||
class Message(models.Model): | ||
user_id = models.ForeignKey(Player, on_delete=models.CASCADE) | ||
game_event_id = models.ForeignKey(GameEvent, on_delete=models.CASCADE) | ||
time_sent = models.DateTimeField(auto_now_add=True) | ||
text = models.CharField(max_length=255, validators=[MinLengthValidator(1)]) | ||
|
||
@staticmethod | ||
def create(player, game_event, text): | ||
Message.validate_message(player, game_event, text) | ||
message = Message(user_id=player, | ||
game_event_id=game_event, | ||
text=text) | ||
|
||
message.save() | ||
return message | ||
|
||
@staticmethod | ||
def validate_message(player, game_event, text): | ||
game_event = GameEvent.objects.get(pk=game_event.pk) | ||
player_participates = GameEventPlayer.objects.filter(player=player, game_event=game_event).exists() | ||
errors = [] | ||
if player_participates is False: | ||
errors.append("Just players from this event can send messages on chat") | ||
if text.strip() == '': | ||
errors.append("Message can't be empty") | ||
|
||
if errors: | ||
raise ValidationError("\n".join(errors)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,25 @@ | ||
# from django.shortcuts import render | ||
from django.shortcuts import render, redirect | ||
from game_event.models import GameEvent | ||
from player.models import Player | ||
from .models import Message | ||
from django.contrib.auth.decorators import login_required | ||
from django.core.exceptions import ValidationError | ||
from django.contrib import messages | ||
|
||
# Create your views here. | ||
|
||
@login_required(login_url="/login/") | ||
def save_text_message(request, id): | ||
try: | ||
event = GameEvent.objects.get(pk=id) | ||
except GameEvent.DoesNotExist: | ||
return render(request, 'game_event/game-event.html', {}) | ||
player = Player.objects.get(user=request.user) | ||
if request.method == 'POST': | ||
text = request.POST.get('mytextbox', '') | ||
try: | ||
Message.create(player, event, text) | ||
except ValidationError as e: | ||
error_messages = e.messages[0].split("\n") | ||
for error in error_messages: | ||
messages.error(request, error) | ||
return redirect('game_event', id=id) |
Oops, something went wrong.