<commit_before>import decimal
from decimal import Decimal
from django.db import models, transaction
from django.contrib.auth.models import User, Group
from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver


# Everything about Finance
class MixinBalance(models.Model):

	class Meta:
		abstract = True
		
	current = models.DecimalField(max_digits=8, decimal_places=2, default=0)
	update_date = models.DateField(auto_now=True)
	
	def __str__(self):
		return self.current
    
    def formatting(self, price):
        return price.quantize(Decimal('0.01'), decimal.ROUND_UP)
        
    def credit(self, credit = 0.0):
    	self.current = self.formatting(self.current + credit)
    	self.save()
    	
    def debit(self, debit = 0.0):
    	self.current = self.formatting(self.current - debit)
    	self.save()


        
# Model Profil
class Profil(User):

    balance = models.OneToOneField(ProfilBalance)

    def build(self, dic):
        balance_user = ProfilBalance.build(dic)
        profil_user = self.create(user = dic[user], balance = balance_user)
    	return profil_user
    
# Everything about User Finance
class ProfilBalance(MixinBalance):

	def build(self, dic):
		balance = ProfilBalance.objects.create()
		return balance
    
# Everything about Product
class Product(models.Model):
    
    name = models.CharField(max_length=64)
    description = models.TextField(max_length=512, null=True, blank=True)
    post_date = models.DateField(auto_now_add=True)
    balance = models.OneToOneField(ProductBalance)
    ownership = models.OneToOneField(ProductOwnership)
    
    def build(self, dic):
    	balance_product = ProductBalance.build(dic)
    	ownership_product = ProductOwership.build(dic)
    	product = self.objects.create(name = dic["name"],
    					description = dic["description"],
    					balance = balance_product,
    					owners = ownership_product)
    	return product

    def __str__(self):
        return self.name
            
# Everything about Product Finance
class ProductBalance(MixinBalance):
    
    initial_cost = models.DecimalField(max_digits=8, decimal_places=2)
    step = models.DecimalField(max_digits=8, decimal_places=2,
                               null=True, blank=True)
                               
   	def build(self, dic):
		balance = self.objects.create(dic[step])
		return balance
                               
    # recalcul le prix de la prochaine utilisation
    def update_current_cost(self, nb_use):
        if self.step and nb_use * self.step <= self.cost:
            self.current_cost = self.step
        else:
            self.current_cost = self.cost / (1 + nb_use)
            self.current_cost = self.formatting(self.current_cost)
        self.save()
        
# Everything about Ownership
class ProductOwnership(models.Model):
    
    first_owner = models.ForeignKey(Profil)
    is_public = models.BooleanField(default=True)
    nb_use = models.IntegerFields(default = 0)
    private_group = models.ForeignKey(PrivateGroup, null=True, blank=True)
    product_group = models.OneToOneField(Group, null=True, blank=True)
    update_date = models.DateField(auto_now=True)
    
    def build(self, dic):
    	product_group_owners = ProductGroup.build(dic)
    	ownership = self.objects.create(first_owner = dic["first_owner"],
    							is_public = dic["is_public"],
    							product_group = product_group_owners)
    	ownership.update_nb_use()
    	return product
    		
    def add_private_group(self, dic):
    	self.private_group = PrivateGroup.build(self, dic)
    	self.save()
    	return self
    
    def update_nb_use(self):
    	if self.nb_use == 0:
    		group = self.product_group.objects.all()
		self.nb_use = group.count()
	else:
		self.nb_use += 1
	self.save()<commit_msg>Update models.py<commit_after>import decimal
from decimal import Decimal
from django.db import models, transaction
from django.contrib.auth.models import User, Group
from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver


# Everything about Finance
class MixinBalance(models.Model):

	class Meta:
		abstract = True
		
	current = models.DecimalField(max_digits=8, decimal_places=2, default=0)
	update_date = models.DateField(auto_now=True)
	
	def __str__(self):
		return self.current
    
    def formatting(self, price):
        return price.quantize(Decimal('0.01'), decimal.ROUND_UP)
        
    def credit(self, credit = 0.0):
    	self.current = self.formatting(self.current + credit)
    	self.save()
    	
    def debit(self, debit = 0.0):
    	self.current = self.formatting(self.current - debit)
    	self.save()


        
# Model Profil
class Profil(User):

    balance = models.OneToOneField(ProfilBalance)

    def build(self, dic):
        balance_user = ProfilBalance.build(dic)
        profil_user = self.create(user = dic[user], balance = balance_user)
    	return profil_user
    
# Everything about User Finance
class ProfilBalance(MixinBalance):

	def build(self, dic):
		balance = ProfilBalance.objects.create()
		return balance
    
# Everything about Product
class Product(models.Model):
    
    name = models.CharField(max_length=64)
    description = models.TextField(max_length=512, null=True, blank=True)
    post_date = models.DateField(auto_now_add=True)
    balance = models.OneToOneField(ProductBalance)
    ownership = models.OneToOneField(ProductOwnership)
    
    def build(self, dic):
    	balance_product = ProductBalance.build(dic)
    	ownership_product = ProductOwership.build(dic)
    	product = self.objects.create(name = dic["name"],
    					description = dic["description"],
    					balance = balance_product,
    					owners = ownership_product)
    	return product

    def __str__(self):
        return self.name
            
# Everything about Product Finance
class ProductBalance(MixinBalance):
    
    initial_cost = models.DecimalField(max_digits=8, decimal_places=2)
    step = models.DecimalField(max_digits=8, decimal_places=2,
                               null=True, blank=True)
                               
   	def build(self, dic):
		balance = self.objects.create(dic[step])
		return balance
                               
    # recalcul le prix de la prochaine utilisation
    def update_current_cost(self, nb_use):
        if self.step and nb_use * self.step <= self.cost:
            self.current_cost = self.step
        else:
            self.current_cost = self.cost / (1 + nb_use)
            self.current_cost = self.formatting(self.current_cost)
        self.save()
        
# Everything about Ownership
class ProductOwnership(models.Model):
    
    first_owner = models.ForeignKey(Profil)
    is_public = models.BooleanField(default=True)
    nb_use = models.IntegerFields(default = 0)
    private_group = models.ForeignKey(PrivateGroup, null=True, blank=True)
    product_group = models.OneToOneField(Group, null=True, blank=True)
    update_date = models.DateField(auto_now=True)
    
    def build(self, dic):
    	product_group_owners = ProductGroup.build(dic)
    	ownership = self.objects.create(first_owner = dic["first_owner"],
    							is_public = dic["is_public"],
    							product_group = product_group_owners)
    	ownership.update_nb_use()
    	if ownership.is_public = False:
    		ownership.add_private_group(dic)
    	return product
    		
    def add_private_group(self, dic):
    	self.private_group = PrivateGroup.build(self, dic)
    	self.save()
    	return self
    
    def update_nb_use(self):
    	if self.nb_use == 0:
    		group = self.product_group.objects.all()
		self.nb_use = group.count()
	else:
		self.nb_use += 1
	self.save()
{'fraction_non_alphanumeric': 0.09540544058310556, 'fraction_numerical': 0.005987244565924769, 'mean_word_length': 3.0083463745435575, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '612316', 'n_tokens_mistral': 2472, 'n_tokens_neox': 2371, 'n_words': 488}