<commit_before>from django.conf import settings
from django.db import transaction
from django.db.models.signals import post_save
from django.contrib.sites.models import RequestSite
from django.contrib.auth.models import User
from django.db import models
from django.db.models import Q

from django_thumbs.db.models import ImageWithThumbsField

from registration import models as regmodels
from registration.signals import user_registered

from sanitizer.models import SanitizedTextField

from operator import itemgetter

from editor.models import NewQuestion, NewExam, Question, Exam, EditorTag, Project, TimelineItem

class RegistrationManager(regmodels.RegistrationManager):
    @transaction.atomic
    def create_inactive_user(self, username, first_name, last_name, email, password,
                             site, send_email=True):
        """
        Create a new, inactive ``User``, generate a
        ``RegistrationProfile`` and email its activation key to the
        ``User``, returning the new ``User``.

        By default, an activation email will be sent to the new
        user. To disable this, pass ``send_email=False``.
        
        """
        new_user = User.objects.create_user(username, email, password,first_name=first_name,last_name=last_name)
        new_user.is_active = False
        new_user.save()

        registration_profile = self.create_profile(new_user)

        if send_email:
            registration_profile.send_activation_email(site)

        return new_user

class RegistrationProfile(regmodels.RegistrationProfile):
    objects = RegistrationManager()


class UserProfile(models.Model):
    user = models.OneToOneField(User)
    language = models.CharField(max_length=100,default='en-GB')
    bio = SanitizedTextField(default='',allowed_tags=settings.SANITIZER_ALLOWED_TAGS,allowed_attributes=settings.SANITIZER_ALLOWED_ATTRIBUTES)
    question_basket = models.ManyToManyField(NewQuestion,blank=True,related_name='baskets',through='BasketQuestion')
    personal_project = models.ForeignKey(Project,null=True,on_delete=models.SET_NULL)
    avatar = ImageWithThumbsField(upload_to='avatars',sizes=((20,20),(40,40),(150,150)),blank=True,null=True,max_length=255,verbose_name='Profile image')

    def sorted_tags(self):
        qs = self.user.own_questions
        tags = EditorTag.objects.filter(question__author=self.user).distinct()
        tag_counts = [(tag,len(qs.filter(tags__id=tag.id))) for tag in tags]
        tag_counts.sort(key=itemgetter(1),reverse=True)

        return tag_counts

    @property
    def recent_questions(self):
        return NewQuestion.objects.filter(editoritem__author=self.user).order_by('-editoritem__last_modified')[:10]

    def projects(self):
        return (Project.objects.filter(owner=self.user) | Project.objects.filter(projectaccess__user=self.user)).distinct()

    def all_timeline(self):
        projects = self.user.own_projects.all() | Project.objects.filter(projectaccess__in=self.user.project_memberships.all())
        items = TimelineItem.objects.filter(
            Q(editoritems__in=self.user.watched_items.all()) | 
            Q(editoritems__project__in=projects) |
            Q(projects__in=projects)
        )

        return items.order_by('-date')

    def public_timeline(self):
        return self.user.timelineitems.order_by('-date')
        
class BasketQuestion(models.Model):
    class Meta:
        ordering = ['qn_order']
        unique_together = ('profile','question')
        
    profile = models.ForeignKey(UserProfile)
    question = models.ForeignKey(NewQuestion)
    qn_order = models.PositiveIntegerField()

<commit_msg>sort own projects by name<commit_after>from django.conf import settings
from django.db import transaction
from django.db.models.signals import post_save
from django.contrib.sites.models import RequestSite
from django.contrib.auth.models import User
from django.db import models
from django.db.models import Q
from django.db.models.functions import Lower

from django_thumbs.db.models import ImageWithThumbsField

from registration import models as regmodels
from registration.signals import user_registered

from sanitizer.models import SanitizedTextField

from operator import itemgetter

from editor.models import NewQuestion, NewExam, Question, Exam, EditorTag, Project, TimelineItem

class RegistrationManager(regmodels.RegistrationManager):
    @transaction.atomic
    def create_inactive_user(self, username, first_name, last_name, email, password,
                             site, send_email=True):
        """
        Create a new, inactive ``User``, generate a
        ``RegistrationProfile`` and email its activation key to the
        ``User``, returning the new ``User``.

        By default, an activation email will be sent to the new
        user. To disable this, pass ``send_email=False``.
        
        """
        new_user = User.objects.create_user(username, email, password,first_name=first_name,last_name=last_name)
        new_user.is_active = False
        new_user.save()

        registration_profile = self.create_profile(new_user)

        if send_email:
            registration_profile.send_activation_email(site)

        return new_user

class RegistrationProfile(regmodels.RegistrationProfile):
    objects = RegistrationManager()


class UserProfile(models.Model):
    user = models.OneToOneField(User)
    language = models.CharField(max_length=100,default='en-GB')
    bio = SanitizedTextField(default='',allowed_tags=settings.SANITIZER_ALLOWED_TAGS,allowed_attributes=settings.SANITIZER_ALLOWED_ATTRIBUTES)
    question_basket = models.ManyToManyField(NewQuestion,blank=True,related_name='baskets',through='BasketQuestion')
    personal_project = models.ForeignKey(Project,null=True,on_delete=models.SET_NULL)
    avatar = ImageWithThumbsField(upload_to='avatars',sizes=((20,20),(40,40),(150,150)),blank=True,null=True,max_length=255,verbose_name='Profile image')

    def sorted_tags(self):
        qs = self.user.own_questions
        tags = EditorTag.objects.filter(question__author=self.user).distinct()
        tag_counts = [(tag,len(qs.filter(tags__id=tag.id))) for tag in tags]
        tag_counts.sort(key=itemgetter(1),reverse=True)

        return tag_counts

    @property
    def recent_questions(self):
        return NewQuestion.objects.filter(editoritem__author=self.user).order_by('-editoritem__last_modified')[:10]

    def projects(self):
        return (Project.objects.filter(owner=self.user) | Project.objects.filter(projectaccess__user=self.user)).distinct().order_by(Lower('name'))

    def all_timeline(self):
        projects = self.user.own_projects.all() | Project.objects.filter(projectaccess__in=self.user.project_memberships.all())
        items = TimelineItem.objects.filter(
            Q(editoritems__in=self.user.watched_items.all()) | 
            Q(editoritems__project__in=projects) |
            Q(projects__in=projects)
        )

        return items.order_by('-date')

    def public_timeline(self):
        return self.user.timelineitems.order_by('-date')
        
class BasketQuestion(models.Model):
    class Meta:
        ordering = ['qn_order']
        unique_together = ('profile','question')
        
    profile = models.ForeignKey(UserProfile)
    question = models.ForeignKey(NewQuestion)
    qn_order = models.PositiveIntegerField()


{'fraction_non_alphanumeric': 0.09900990099009901, 'fraction_numerical': 0.006238980062389801, 'mean_word_length': 4.033447098976109, 'pattern_counts': {'":': 0, '<': 3, '<?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': '463795', 'n_tokens_mistral': 2176, 'n_tokens_neox': 2084, 'n_words': 402}