def get_friends(self): site = Site.objects.get_current() SiteProfile = get_site_profile_model() table_name = SiteProfile._meta.db_table if (site.id == django_settings.SPEEDY_NET_SITE_ID): extra_select = { 'last_visit': 'SELECT last_visit FROM {} WHERE user_id = friendship_friend.from_user_id' .format(table_name), } qs = self.user.friends.all().extra( select=extra_select).order_by('-last_visit') return qs elif (site.id == django_settings.SPEEDY_MATCH_SITE_ID): extra_select = { 'last_visit': 'SELECT last_visit FROM {} WHERE user_id = friendship_friend.from_user_id' .format(table_name), 'like_exists': 'SELECT COUNT(1) FROM likes_userlike WHERE from_user_id = friendship_friend.from_user_id OR to_user_id=friendship_friend.from_user_id', 'messages_exists': 'SELECT COUNT(1) FROM im_chat WHERE ent1_id=friendship_friend.from_user_id OR ent2_id=friendship_friend.from_user_id' } qs = self.user.friends.all().extra( select=extra_select).order_by('-last_visit') qs = [ u for u in qs if (self.user.profile.get_matching_rank( other_profile=u.from_user.profile) > SiteProfile.RANK_0) or u.like_exists or u.messages_exists ] return qs else: raise NotImplementedError()
class ProfileNotificationsForm(forms.ModelForm): _profile_model = get_site_profile_model(profile_model=None) _profile_fields = () class Meta: model = User fields = ('notify_on_message',) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) for field in self._profile_model._meta.fields: if (field.name in self._profile_fields): self.fields[field.name] = field.formfield() self.fields[field.name].initial = getattr(self.instance.profile, field.name) self.helper = FormHelperWithDefaults() self.helper.add_input(Submit('submit', pgettext_lazy(context=self.instance.get_gender(), message='Save Changes'))) def save(self, commit=True): for field_name in self.fields.keys(): if (field_name in self._profile_fields): setattr(self.instance.profile, field_name, self.cleaned_data[field_name]) r = super().save(commit=commit) if (commit): self.instance.profile.save() return r
def get_like_list_from_queryset(self, user): from speedy.net.accounts.models import SiteProfile as SpeedyNetSiteProfile from speedy.match.accounts.models import SiteProfile as SpeedyMatchSiteProfile SiteProfile = get_site_profile_model() # Filter out users that are only active in another language. who_likes_me = User.objects.filter(pk__in=self.filter( to_user=user).values_list('from_user_id', flat=True)) who_likes_me = [ u.pk for u in who_likes_me if (u.speedy_match_profile.is_active) ] # Filter out users I blocked. blocked_users_ids = [ block.blocked_id for block in user.blocked_entities.all() ] who_likes_me = [ pk for pk in who_likes_me if (not (pk in blocked_users_ids)) ] return self.filter(to_user=user).filter( from_user__in=who_likes_me).prefetch_related( "from_user", "from_user__{}".format(SpeedyNetSiteProfile.RELATED_NAME), "from_user__{}".format(SpeedyMatchSiteProfile.RELATED_NAME), 'from_user__photo').distinct().order_by( '-from_user__{}__last_visit'.format( SiteProfile.RELATED_NAME))
def get_queryset(self): SiteProfile = get_site_profile_model() language_code = get_language() filter_dict = dict( speedy_match_site_profile__height__range=( SpeedyMatchSiteProfile.settings.MIN_HEIGHT_TO_MATCH, SpeedyMatchSiteProfile.settings.MAX_HEIGHT_TO_MATCH), speedy_match_site_profile__not_allowed_to_use_speedy_match=False, speedy_match_site_profile__active_languages__contains=[ language_code ], ) annotate_list = list() if (self.request.GET.get('likes_from_user')): annotate_list.append(Count('likes_from_user', distinct=True)) filter_dict["likes_from_user__count__gte"] = int( self.request.GET.get('likes_from_user')) if (self.request.GET.get('likes_to_user')): annotate_list.append(Count('likes_to_user', distinct=True)) filter_dict["likes_to_user__count__gte"] = int( self.request.GET.get('likes_to_user')) qs = User.objects.active().annotate(*annotate_list).filter( **filter_dict).order_by('-{}__last_visit'.format( SiteProfile.RELATED_NAME)) return qs
def get_queryset(self): # filter out users that are only active in another language who_likes_me = User.objects.filter(pk__in=UserLike.objects.filter(to_user=self.user).values_list('from_user_id', flat=True)) who_likes_me = [u.pk for u in who_likes_me if (u.speedy_match_profile.is_active)] SiteProfile = get_site_profile_model() table_name = SiteProfile._meta.db_table extra_select = { 'last_visit': 'SELECT last_visit FROM {} WHERE user_id = likes_userlike.to_user_id'.format(table_name), } return UserLike.objects.filter(from_user=self.user, to_user_id__in=who_likes_me).extra(select=extra_select).order_by('-last_visit')
def get_queryset(self): SiteProfile = get_site_profile_model() language_code = get_language() qs = User.objects.active( speedy_match_site_profile__height__range=( SpeedyMatchSiteProfile.settings.MIN_HEIGHT_TO_MATCH, SpeedyMatchSiteProfile.settings.MAX_HEIGHT_TO_MATCH), speedy_match_site_profile__not_allowed_to_use_speedy_match=False, speedy_match_site_profile__active_languages__contains=[ language_code ], ).order_by('-{}__last_visit'.format(SiteProfile.RELATED_NAME)) return qs
def get_queryset(self): SiteProfile = get_site_profile_model() if (self.request.GET.get('order_by') == 'number_of_friends'): qs = User.objects.all().order_by( F('{}__{}'.format( SpeedyNetSiteProfile.RELATED_NAME, to_attribute(name='number_of_friends'))).desc( nulls_last=True), '-{}__last_visit'.format(SiteProfile.RELATED_NAME)) else: qs = User.objects.all().order_by('-{}__last_visit'.format( SiteProfile.RELATED_NAME)) return qs
def get_context_data(self): from speedy.net.accounts.models import SiteProfile as SpeedyNetSiteProfile from speedy.match.accounts.models import SiteProfile as SpeedyMatchSiteProfile cd = super().get_context_data() SiteProfile = get_site_profile_model() all_friends = list(self.user.friends.all().prefetch_related( "from_user", "from_user__{}".format(SpeedyNetSiteProfile.RELATED_NAME), "from_user__{}".format(SpeedyMatchSiteProfile.RELATED_NAME), 'from_user__photo').distinct().order_by( '-from_user__{}__last_visit'.format(SiteProfile.RELATED_NAME))) cd.update({ 'friends': all_friends[:30], 'friends_count': len(all_friends), }) return cd
def get_like_list_mutual_queryset(self, user): from speedy.net.accounts.models import SiteProfile as SpeedyNetSiteProfile from speedy.match.accounts.models import SiteProfile as SpeedyMatchSiteProfile SiteProfile = get_site_profile_model() # filter out users that are only active in another language who_likes_me = User.objects.filter(pk__in=self.filter( to_user=user).values_list('from_user_id', flat=True)) who_likes_me = [ u.pk for u in who_likes_me if (u.speedy_match_profile.is_active) ] return self.filter( from_user=user, to_user_id__in=who_likes_me).prefetch_related( "to_user", "to_user__{}".format(SpeedyNetSiteProfile.RELATED_NAME), "to_user__{}".format( SpeedyMatchSiteProfile.RELATED_NAME)).distinct().order_by( '-to_user__{}__last_visit'.format( SiteProfile.RELATED_NAME))
class Meta: model = get_site_profile_model(profile_model=None) fields = ()
def get_total_number_of_members_text(self): SiteProfile = get_site_profile_model() total_number_of_members = User.objects.all().count() total_number_of_members_in_the_last_week = User.objects.filter( **{ "{}__last_visit__gte".format(SiteProfile.RELATED_NAME): now() - timedelta(days=7), }).count() total_number_of_members_in_the_last_month = User.objects.filter( **{ "{}__last_visit__gte".format(SiteProfile.RELATED_NAME): now() - timedelta(days=30), }).count() total_number_of_members_in_the_last_four_months = User.objects.filter( **{ "{}__last_visit__gte".format(SiteProfile.RELATED_NAME): now() - timedelta(days=120), }).count() total_number_of_members_text = _( "Admin: The total number of members on the site is {total_number_of_members}, of which {total_number_of_members_in_the_last_week} members entered the site in the last week, {total_number_of_members_in_the_last_month} members entered the site in the last month, and {total_number_of_members_in_the_last_four_months} members entered the site in the last four months." ).format( total_number_of_members='{:,}'.format(total_number_of_members), total_number_of_members_in_the_last_week='{:,}'.format( total_number_of_members_in_the_last_week), total_number_of_members_in_the_last_month='{:,}'.format( total_number_of_members_in_the_last_month), total_number_of_members_in_the_last_four_months='{:,}'.format( total_number_of_members_in_the_last_four_months), ) total_number_of_members_registered_in_the_last_week = User.objects.filter( date_created__gte=now() - timedelta(days=7), ).count() total_number_of_members_registered_in_the_last_month = User.objects.filter( date_created__gte=now() - timedelta(days=30), ).count() total_number_of_members_registered_in_the_last_four_months = User.objects.filter( date_created__gte=now() - timedelta(days=120), ).count() total_number_of_members_registered_more_than_four_months_ago = User.objects.filter( date_created__lte=now() - timedelta(days=120), ).count() total_number_of_members_registered_before_2019_08_01 = User.objects.filter( date_created__lte=datetime.strptime( '2019-08-01 00:00:00', '%Y-%m-%d %H:%M:%S').replace(tzinfo=timezone.utc), ).count() total_number_of_members_text += "\n" total_number_of_members_text += "\n" total_number_of_members_text += _( "Admin: {total_number_of_members_registered_in_the_last_week} members registered in the last week. {total_number_of_members_registered_in_the_last_month} members registered in the last month. {total_number_of_members_registered_in_the_last_four_months} members registered in the last four months. {total_number_of_members_registered_more_than_four_months_ago} members registered more than four months ago. {total_number_of_members_registered_before_2019_08_01} members registered before 1 August 2019." ).format( total_number_of_members_registered_in_the_last_week='{:,}'.format( total_number_of_members_registered_in_the_last_week), total_number_of_members_registered_in_the_last_month='{:,}'.format( total_number_of_members_registered_in_the_last_month), total_number_of_members_registered_in_the_last_four_months='{:,}'. format(total_number_of_members_registered_in_the_last_four_months), total_number_of_members_registered_more_than_four_months_ago='{:,}' .format( total_number_of_members_registered_more_than_four_months_ago), total_number_of_members_registered_before_2019_08_01='{:,}'.format( total_number_of_members_registered_before_2019_08_01), ) total_number_of_members_text += "\n" today = date.today() for year in range(2010, today.year + 2): total_number_of_members_registered_in_year = User.objects.filter( date_created__gte=datetime.strptime( '{year}-01-01 00:00:00'.format(year=year), '%Y-%m-%d %H:%M:%S').replace(tzinfo=timezone.utc), date_created__lt=datetime.strptime( '{year}-01-01 00:00:00'.format(year=year + 1), '%Y-%m-%d %H:%M:%S').replace(tzinfo=timezone.utc), ).count() total_number_of_members_text += "\n" total_number_of_members_text += _( "Admin: {total_number_of_members_registered_in_year} members registered in {year}." ).format( total_number_of_members_registered_in_year='{:,}'.format( total_number_of_members_registered_in_year), year=year, ) return total_number_of_members_text
def get_queryset(self): site_profile_model = get_site_profile_model() return super().get_queryset().select_related( site_profile_model.RELATED_NAME)
def get_queryset(self): site_profile_model = get_site_profile_model() return super().get_queryset().select_related(site_profile_model.RELATED_NAME)
from django.conf import settings as django_settings if (django_settings.LOGIN_ENABLED): from translated_fields import TranslatedFieldAdmin from speedy.core import admin from speedy.core.base.admin import ModelAdmin, ModelAdmin5000, ReadOnlyModelAdmin from speedy.core.messages.admin import MessageInlineAdmin from speedy.core.accounts.utils import get_site_profile_model from .models import Entity, ReservedUsername, User, UserEmailAddress SiteProfile = get_site_profile_model() class EntityAdmin(TranslatedFieldAdmin, ReadOnlyModelAdmin): readonly_fields = ('date_created', 'date_updated', 'id') inlines = [ MessageInlineAdmin, ] def has_delete_permission(self, request, obj=None): return True class ReservedUsernameAdmin(ModelAdmin5000): readonly_fields = ('date_created', 'date_updated', 'id') class UserAdmin(TranslatedFieldAdmin, ReadOnlyModelAdmin): readonly_fields = ('date_created', 'date_updated', 'id') inlines = [ MessageInlineAdmin, ] ordering = ('-{}__last_visit'.format(SiteProfile.RELATED_NAME), )