diff --git a/psycity/core/models.py b/psycity/core/models.py index 110a447..681c6e0 100644 --- a/psycity/core/models.py +++ b/psycity/core/models.py @@ -208,7 +208,7 @@ class Team(BaseModel): last_bank_action = models.DateTimeField(blank=True, null=True) today_bought_question = models.IntegerField(default=0) channel_id = models.CharField(max_length=100) - channel_role = models.CharField(max_length=100) + channel_role = models.CharField(max_length=100, null=False, blank=False, unique=True, primary_key=True) def __str__(self): return self.name @@ -348,6 +348,11 @@ class CONTRACT_TYPES(models.TextChoices): ) terms = models.TextField() + question = models.ForeignKey('Question', + on_delete=models.CASCADE, + related_name='contract_question_to_question', + blank=True, + null=True) first_party_agree = models.BooleanField() second_party_agree = models.BooleanField() archive = models.BooleanField() # todo isn't it avail in state? diff --git a/psycity/models_retrieve_api/serializers.py b/psycity/models_retrieve_api/serializers.py index 7dd50dd..f5f975f 100644 --- a/psycity/models_retrieve_api/serializers.py +++ b/psycity/models_retrieve_api/serializers.py @@ -12,7 +12,6 @@ class TeamListSerializer(ModelSerializer): class Meta: model = Team fields = [ - "id", "name", "state", "team_role", diff --git a/psycity/team_api/serializers.py b/psycity/team_api/serializers.py index 8015ee8..9a538f3 100644 --- a/psycity/team_api/serializers.py +++ b/psycity/team_api/serializers.py @@ -18,7 +18,7 @@ BankSensorInstall, Question, ) -from team_api.utils import cost_validation, ModelSerializerAndABCMetaClass +from team_api.utils import cost_validation, ModelSerializerAndABCMetaClass, question_validation from datetime import timedelta from abc import ABC, abstractmethod from abc import ABC, abstractmethod, ABCMeta @@ -181,9 +181,9 @@ class Meta: -def required(value): +def required(value, field_name): if value is None: - raise serializers.ValidationError('This field is required') + raise serializers.ValidationError(f'{field_name} is required') class ContractRegisterSerializer(serializers.ModelSerializer): id = serializers.SerializerMethodField() @@ -193,32 +193,38 @@ class Meta: "id", "first_party_team", "second_party_team", + "first_party_player", + "second_party_player", "contract_type", + "question", "cost", "terms", ) - def base_team_validation(self, team_id): - required(team_id) - return team_id def get_id(self, obj): return obj.id - def validate_first_party_team(self, team_id): - self.base_team_validation(team_id) - return team_id - - def validate_second_party_team(self, team_id): - self.base_team_validation(team_id) - return team_id def contract_type_validation(self, attrs): contract_type = attrs.get("contract_type") + + print('attrs', attrs) if contract_type == "question_ownership_transfer": - first = attrs.get("first_party_team") + first: Team = attrs.get("first_party_team") + second: Team = attrs.get("second_party_team") + question: Question = attrs.get("question") + cost = attrs.get("cost") + required(first, 'first_party_team') + required(second, 'second_party_team') + required(question, 'question') + required(cost, 'cost') + cost_validation(attrs.get("cost"), first) + question_validation(attrs.get("question"), first) + + elif contract_type == "bank_rubbery_sponsorship": try: @@ -237,11 +243,17 @@ def contract_type_validation(self, attrs): cost_validation(attrs.get("cost"), citizen_team) + elif contract_type == "homeless_solve_question": + first = attrs.get("first_party_team") + second = attrs.get("second_party_player") + + elif contract_type == "bodyguard_for_the_homeless": raise exceptions.NotAcceptable("Not this endpoint") elif contract_type == "other": raise exceptions.NotAcceptable("Not Implemented in here :)") + def validate(self, attrs): @@ -249,7 +261,7 @@ def validate(self, attrs): return attrs class ContractApprovementSerializer(serializers.ModelSerializer): - team = serializers.IntegerField() + team = serializers.CharField() class Meta: model = Contract fields = ( diff --git a/psycity/team_api/utils.py b/psycity/team_api/utils.py index 0704e0a..c85eb06 100644 --- a/psycity/team_api/utils.py +++ b/psycity/team_api/utils.py @@ -1,9 +1,10 @@ from django.http import Http404 +from django.shortcuts import get_object_or_404 from rest_framework.response import Response from rest_framework import serializers from rest_framework import exceptions, status from psycity.settings import DEBUG -from core.models import Team, BankDepositBox +from core.models import Team, BankDepositBox, Question from drf_yasg import openapi from functools import wraps from abc import ABCMeta @@ -100,6 +101,14 @@ def cost_validation(cost, team:Team): ) # log.warning(f"Team {team.name} cant effort {cost} amount of money") return True + +def question_validation(question: Question, first_team: Team): + if question.last_owner != first_team: + raise exceptions.NotAcceptable( + f"Quesiton owner is not first party team" + ) + return True + def response(func): @wraps(func) diff --git a/psycity/team_api/views/contract.py b/psycity/team_api/views/contract.py index 2f98777..1df30bc 100644 --- a/psycity/team_api/views/contract.py +++ b/psycity/team_api/views/contract.py @@ -116,6 +116,12 @@ def perform_update(self, serializer): def pay(self, contract:Contract): try: + if contract.contract_type == 'question_ownership_transfer': + contract.question.last_owner = contract.second_party_team + contract.question.save() + elif contract.contract_type == 'homeless_solve_question': + # todo: + ... contract.first_party_team.wallet -= contract.cost contract.first_party_team.save() contract.second_party_team.wallet += contract.cost