Change is_subscriber and was_subscriber to cached_properties

This commit is contained in:
Skia 2017-02-24 02:59:59 +01:00
parent 11e05050f7
commit dea234f98a
10 changed files with 17 additions and 15 deletions

View File

@ -122,7 +122,7 @@ class Club(models.Model):
sub = User.objects.filter(pk=user.pk).first() sub = User.objects.filter(pk=user.pk).first()
if sub is None: if sub is None:
return False return False
return sub.is_subscribed() return sub.is_subscribed
def get_membership_for(self, user): def get_membership_for(self, user):
""" """
@ -151,7 +151,7 @@ class Membership(models.Model):
def clean(self): def clean(self):
sub = User.objects.filter(pk=self.user.pk).first() sub = User.objects.filter(pk=self.user.pk).first()
if sub is None or not sub.is_subscribed(): if sub is None or not sub.is_subscribed:
raise ValidationError(_('User must be subscriber to take part to a club')) raise ValidationError(_('User must be subscriber to take part to a club'))
if Membership.objects.filter(user=self.user).filter(club=self.club).filter(end_date=None).exists(): if Membership.objects.filter(user=self.user).filter(club=self.club).filter(end_date=None).exists():
raise ValidationError(_('User is already member of that club')) raise ValidationError(_('User is already member of that club'))

View File

@ -184,9 +184,11 @@ class User(AbstractBaseUser):
def to_dict(self): def to_dict(self):
return self.__dict__ return self.__dict__
@cached_property
def was_subscribed(self): def was_subscribed(self):
return self.subscriptions.exists() return self.subscriptions.exists()
@cached_property
def is_subscribed(self): def is_subscribed(self):
s = self.subscriptions.last() s = self.subscriptions.last()
return s.is_valid_now() if s is not None else False return s.is_valid_now() if s is not None else False
@ -207,11 +209,11 @@ class User(AbstractBaseUser):
if group_id == settings.SITH_GROUP_PUBLIC_ID: if group_id == settings.SITH_GROUP_PUBLIC_ID:
return True return True
if group_id == settings.SITH_GROUP_SUBSCRIBERS_ID: if group_id == settings.SITH_GROUP_SUBSCRIBERS_ID:
return self.is_subscribed() return self.is_subscribed
if group_id == settings.SITH_GROUP_OLD_SUBSCRIBERS_ID: if group_id == settings.SITH_GROUP_OLD_SUBSCRIBERS_ID:
return self.was_subscribed() return self.was_subscribed
if group_name == settings.SITH_MAIN_MEMBERS_GROUP: # We check the subscription if asked if group_name == settings.SITH_MAIN_MEMBERS_GROUP: # We check the subscription if asked
return self.is_subscribed() return self.is_subscribed
if group_name[-len(settings.SITH_BOARD_SUFFIX):] == settings.SITH_BOARD_SUFFIX: if group_name[-len(settings.SITH_BOARD_SUFFIX):] == settings.SITH_BOARD_SUFFIX:
from club.models import Club from club.models import Club
name = group_name[:-len(settings.SITH_BOARD_SUFFIX)] name = group_name[:-len(settings.SITH_BOARD_SUFFIX)]

View File

@ -54,7 +54,7 @@
{% if user.memberships.filter(end_date=None).exists() or user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) or user == profile %} {% if user.memberships.filter(end_date=None).exists() or user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) or user == profile %}
{# if the user is member of a club, he can view the subscription state #} {# if the user is member of a club, he can view the subscription state #}
<p> <p>
{% if profile.is_subscribed() %} {% if profile.is_subscribed %}
{% if user == profile or user.is_root or user.is_board_member %} {% if user == profile or user.is_root or user.is_board_member %}
{{ user_subscription(profile) }} {{ user_subscription(profile) }}
{% endif %} {% endif %}

View File

@ -91,7 +91,7 @@
<h4>{% trans %}Elections{% endtrans %}</h4> <h4>{% trans %}Elections{% endtrans %}</h4>
<ul> <ul>
<li><a href="{{ url('election:list') }}">{% trans %}See available elections{% endtrans %}</a></li> <li><a href="{{ url('election:list') }}">{% trans %}See available elections{% endtrans %}</a></li>
{%- if user.is_subscribed() -%} {%- if user.is_subscribed -%}
<li><a href="{{ url('election:create') }}">{% trans %}Create a new election{% endtrans %}</a></li> <li><a href="{{ url('election:create') }}">{% trans %}Create a new election{% endtrans %}</a></li>
{%- endif -%} {%- endif -%}
</ul> </ul>

View File

@ -347,7 +347,7 @@ class Selling(models.Model):
self.customer.save() self.customer.save()
self.is_validated = True self.is_validated = True
u = User.objects.filter(id=self.customer.user.id).first() u = User.objects.filter(id=self.customer.user.id).first()
if u.was_subscribed(): if u.was_subscribed:
if self.product and self.product.id == settings.SITH_PRODUCT_SUBSCRIPTION_ONE_SEMESTER: if self.product and self.product.id == settings.SITH_PRODUCT_SUBSCRIPTION_ONE_SEMESTER:
sub = Subscription( sub = Subscription(
member=u, member=u,

View File

@ -80,7 +80,7 @@ class EbouticMain(TemplateView):
kwargs['basket'] = self.basket kwargs['basket'] = self.basket
kwargs['eboutic'] = Counter.objects.filter(type="EBOUTIC").first() kwargs['eboutic'] = Counter.objects.filter(type="EBOUTIC").first()
kwargs['categories'] = ProductType.objects.all() kwargs['categories'] = ProductType.objects.all()
if not self.request.user.was_subscribed(): if not self.request.user.was_subscribed:
kwargs['categories'] = kwargs['categories'].exclude(id=settings.SITH_PRODUCTTYPE_SUBSCRIPTION) kwargs['categories'] = kwargs['categories'].exclude(id=settings.SITH_PRODUCTTYPE_SUBSCRIPTION)
return kwargs return kwargs

View File

@ -271,7 +271,7 @@ class ElectionCreateView(CanCreateMixin, CreateView):
template_name = 'core/create.jinja' template_name = 'core/create.jinja'
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
if not request.user.is_subscribed(): if not request.user.is_subscribed:
raise PermissionDenied raise PermissionDenied
return super(ElectionCreateView, self).dispatch(request, *args, **kwargs) return super(ElectionCreateView, self).dispatch(request, *args, **kwargs)

View File

@ -60,7 +60,7 @@ class LaunderetteBookView(CanViewMixin, DetailView):
self.slot_type = request.POST['slot_type'] self.slot_type = request.POST['slot_type']
if 'slot' in request.POST.keys() and request.user.is_authenticated(): if 'slot' in request.POST.keys() and request.user.is_authenticated():
self.subscriber = request.user self.subscriber = request.user
if self.subscriber.is_subscribed(): if self.subscriber.is_subscribed:
self.date = dateparse.parse_datetime(request.POST['slot']).replace(tzinfo=pytz.UTC) self.date = dateparse.parse_datetime(request.POST['slot']).replace(tzinfo=pytz.UTC)
if self.slot_type == "WASHING": if self.slot_type == "WASHING":
if self.check_slot(self.slot_type): if self.check_slot(self.slot_type):

View File

@ -30,7 +30,7 @@ class Picture(SithFile):
def can_be_viewed_by(self, user): def can_be_viewed_by(self, user):
# file = SithFile.objects.filter(id=self.id).first() # file = SithFile.objects.filter(id=self.id).first()
return self.can_be_edited_by(user) or (self.is_in_sas and self.is_moderated and return self.can_be_edited_by(user) or (self.is_in_sas and self.is_moderated and
user.was_subscribed())# or user.can_view(file) user.was_subscribed)# or user.can_view(file)
def get_download_url(self): def get_download_url(self):
return reverse('sas:download', kwargs={'picture_id': self.id}) return reverse('sas:download', kwargs={'picture_id': self.id})
@ -107,7 +107,7 @@ class Album(SithFile):
def can_be_viewed_by(self, user): def can_be_viewed_by(self, user):
# file = SithFile.objects.filter(id=self.id).first() # file = SithFile.objects.filter(id=self.id).first()
return self.can_be_edited_by(user) or (self.is_in_sas and self.is_moderated and return self.can_be_edited_by(user) or (self.is_in_sas and self.is_moderated and
user.was_subscribed())# or user.can_view(file) user.was_subscribed)# or user.can_view(file)
def get_absolute_url(self): def get_absolute_url(self):
return reverse('sas:album', kwargs={'album_id': self.id}) return reverse('sas:album', kwargs={'album_id': self.id})

View File

@ -160,7 +160,7 @@ class AlbumUploadView(CanViewMixin, DetailView, FormMixin):
self.form = self.get_form() self.form = self.get_form()
parent = SithFile.objects.filter(id=self.object.id).first() parent = SithFile.objects.filter(id=self.object.id).first()
files = request.FILES.getlist('images') files = request.FILES.getlist('images')
if request.user.is_authenticated() and request.user.is_subscribed(): if request.user.is_authenticated() and request.user.is_subscribed:
if self.form.is_valid(): if self.form.is_valid():
self.form.process(parent=parent, owner=request.user, files=files, self.form.process(parent=parent, owner=request.user, files=files,
automodere=request.user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID)) automodere=request.user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID))
@ -192,7 +192,7 @@ class AlbumView(CanViewMixin, DetailView, FormMixin):
FileView.handle_clipboard(request, self.object) FileView.handle_clipboard(request, self.object)
parent = SithFile.objects.filter(id=self.object.id).first() parent = SithFile.objects.filter(id=self.object.id).first()
files = request.FILES.getlist('images') files = request.FILES.getlist('images')
if request.user.is_authenticated() and request.user.is_subscribed(): if request.user.is_authenticated() and request.user.is_subscribed:
if self.form.is_valid(): if self.form.is_valid():
self.form.process(parent=parent, owner=request.user, files=files, self.form.process(parent=parent, owner=request.user, files=files,
automodere=request.user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID)) automodere=request.user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID))