Skip to content

Commit

Permalink
[imp][fix] team pk to channel_role, contract auto change owner of que…
Browse files Browse the repository at this point in the history
…stion, auto pay. new contract type
  • Loading branch information
darloof committed Feb 24, 2024
1 parent 4755c66 commit f2bc752
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
7 changes: 6 additions & 1 deletion psycity/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down
1 change: 0 additions & 1 deletion psycity/models_retrieve_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class TeamListSerializer(ModelSerializer):
class Meta:
model = Team
fields = [
"id",
"name",
"state",
"team_role",
Expand Down
42 changes: 27 additions & 15 deletions psycity/team_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand All @@ -237,19 +243,25 @@ 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):
self.contract_type_validation(attrs)
return attrs

class ContractApprovementSerializer(serializers.ModelSerializer):
team = serializers.IntegerField()
team = serializers.CharField()
class Meta:
model = Contract
fields = (
Expand Down
11 changes: 10 additions & 1 deletion psycity/team_api/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions psycity/team_api/views/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f2bc752

Please sign in to comment.