def contact_seller(request): response = reply_object() form = MessageForm(request.POST, request=request) if form.is_valid(): response = form.new_message() else: response["code"] = settings.APP_CODE["FORM ERROR"] response["errors"] = form.errors return HttpResponse(simplejson.dumps(response))
def post(self, request, *args, **kwargs): form = MessageForm(request.POST) if form.is_valid(): message = form.save() message.save() messages = Message.objects.all() cache.set('messages', pickle.dumps(messages)) return HttpResponseRedirect(reverse_lazy('app:message_list')) return HttpResponseRedirect(reverse_lazy('app:message_list'))
def message_thread_add(request): if not request.POST: return HttpResponse("waiting") response = reply_object() form = MessageForm(request.POST, request=request) if form.is_valid(): response = form.add_to_thread() else: response["code"] = settings.APP_CODE["FORM ERROR"] response["errors"] = form.errors return render_to_response("message_submit.html", {"response_data": simplejson.dumps(response)})
def send_message(request): if request.method == 'POST': form = MessageForm(request.POST) form.fields['sender'].required = False if form.is_valid(): msg = form.save(commit=False) recipient = msg.recipient.user.username msg.sender = request.user.profile msg.save() messages.info(request, f'Message sent to {recipient}.') return redirect(reverse('home')) form = MessageForm() return render(request, 'app/send_message.html', {'form': form})
def game_play_view(request, game_id): red_tag = request.GET.get('redirect', None) try: game = Game.objects.get(pk=game_id, is_active=True) except ObjectDoesNotExist: return render(request, '404.html', {'redirect': red_tag}) if not request.user.is_anonymous: try: transaction = Transaction.objects.filter(game=game, player=request.user, payment_result='success').first() except ObjectDoesNotExist: transaction = 0 else: transaction = 0 if request.method == 'GET': args = {'game': game, 'transaction': transaction, 'redirect': red_tag} return render(request, 'game/game_play.html', args) elif request.method == 'POST': resp = { "error": None, "result": None } transaction = None try: transaction = Transaction.objects.get(game=game, player=request.user, payment_result='success') except ObjectDoesNotExist: transaction = Transaction.objects.create(game=game, player=request.user, amount=0, payment_reference=0, timestamp=timezone.now(), payment_result='success') request.user.inventory.add(transaction.game) form = MessageForm(request.POST) if not form.is_valid(): resp['error'] = form.errors return JsonResponse(status=400, data=resp) # Specific message: if form.cleaned_data['messageType'] == 'SCORE': score_form = MessageScoreForm(request.POST) if not score_form.is_valid(): resp['error'] = form.errors return JsonResponse(status=400, data=resp) new_score = GameScore.objects.create(player=request.user, game=game, score=score_form.cleaned_data['score'], scoreDate=timezone.now()) # Check if there is a high score for this game, and update it if the new_score is better, # or create a new one if there isn't any try: game_high_score = game.high_score.score if new_score.score > game_high_score: Game.objects.filter(pk=game_id).update(high_score=new_score) except AttributeError: Game.objects.filter(pk=game_id).update(high_score=new_score) return JsonResponse(status=201, data=resp) elif form.cleaned_data['messageType'] == 'SAVE': save_form = MessageSaveForm(request.POST) if not save_form.is_valid(): resp['error'] = form.errors return JsonResponse(status=400, data=resp) GameState.objects.create(player=request.user, game=game, saveDate=timezone.now(), gameState=save_form.cleaned_data['gameState']) return JsonResponse(status=201, data=resp) elif form.cleaned_data['messageType'] == 'LOAD_REQUEST': load_form = MessageLoadForm(request.POST) if not load_form.is_valid(): resp['error'] = form.errors return JsonResponse(status=400, data=resp) save_game = GameState.objects.filter(player=request.user, game=game).order_by("-saveDate") if save_game.exists(): resp['result'] = save_game[0].gameState return JsonResponse(status=200, data=resp) else: resp['result'] = None resp['error'] = "No save game found." return JsonResponse(status=400, data=resp) else: resp['error'] = 'Invalid message type.' return JsonResponse(status=400, data=resp) else: return HttpResponse(status=405, content='Invalid method.')