mirror of
https://github.com/ae-utbm/sith.git
synced 2024-12-25 09:11:13 +00:00
Change is_subscriber and was_subscriber to cached_properties
This commit is contained in:
parent
11e05050f7
commit
dea234f98a
@ -122,7 +122,7 @@ class Club(models.Model):
|
||||
sub = User.objects.filter(pk=user.pk).first()
|
||||
if sub is None:
|
||||
return False
|
||||
return sub.is_subscribed()
|
||||
return sub.is_subscribed
|
||||
|
||||
def get_membership_for(self, user):
|
||||
"""
|
||||
@ -151,7 +151,7 @@ class Membership(models.Model):
|
||||
|
||||
def clean(self):
|
||||
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'))
|
||||
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'))
|
||||
|
@ -184,9 +184,11 @@ class User(AbstractBaseUser):
|
||||
def to_dict(self):
|
||||
return self.__dict__
|
||||
|
||||
@cached_property
|
||||
def was_subscribed(self):
|
||||
return self.subscriptions.exists()
|
||||
|
||||
@cached_property
|
||||
def is_subscribed(self):
|
||||
s = self.subscriptions.last()
|
||||
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:
|
||||
return True
|
||||
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:
|
||||
return self.was_subscribed()
|
||||
return self.was_subscribed
|
||||
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:
|
||||
from club.models import Club
|
||||
name = group_name[:-len(settings.SITH_BOARD_SUFFIX)]
|
||||
|
@ -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 the user is member of a club, he can view the subscription state #}
|
||||
<p>
|
||||
{% if profile.is_subscribed() %}
|
||||
{% if profile.is_subscribed %}
|
||||
{% if user == profile or user.is_root or user.is_board_member %}
|
||||
{{ user_subscription(profile) }}
|
||||
{% endif %}
|
||||
|
@ -91,7 +91,7 @@
|
||||
<h4>{% trans %}Elections{% endtrans %}</h4>
|
||||
<ul>
|
||||
<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>
|
||||
{%- endif -%}
|
||||
</ul>
|
||||
|
@ -347,7 +347,7 @@ class Selling(models.Model):
|
||||
self.customer.save()
|
||||
self.is_validated = True
|
||||
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:
|
||||
sub = Subscription(
|
||||
member=u,
|
||||
|
@ -80,7 +80,7 @@ class EbouticMain(TemplateView):
|
||||
kwargs['basket'] = self.basket
|
||||
kwargs['eboutic'] = Counter.objects.filter(type="EBOUTIC").first()
|
||||
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)
|
||||
return kwargs
|
||||
|
||||
|
@ -271,7 +271,7 @@ class ElectionCreateView(CanCreateMixin, CreateView):
|
||||
template_name = 'core/create.jinja'
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
if not request.user.is_subscribed():
|
||||
if not request.user.is_subscribed:
|
||||
raise PermissionDenied
|
||||
return super(ElectionCreateView, self).dispatch(request, *args, **kwargs)
|
||||
|
||||
|
@ -60,7 +60,7 @@ class LaunderetteBookView(CanViewMixin, DetailView):
|
||||
self.slot_type = request.POST['slot_type']
|
||||
if 'slot' in request.POST.keys() and request.user.is_authenticated():
|
||||
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)
|
||||
if self.slot_type == "WASHING":
|
||||
if self.check_slot(self.slot_type):
|
||||
|
@ -30,7 +30,7 @@ class Picture(SithFile):
|
||||
def can_be_viewed_by(self, user):
|
||||
# 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
|
||||
user.was_subscribed())# or user.can_view(file)
|
||||
user.was_subscribed)# or user.can_view(file)
|
||||
|
||||
def get_download_url(self):
|
||||
return reverse('sas:download', kwargs={'picture_id': self.id})
|
||||
@ -107,7 +107,7 @@ class Album(SithFile):
|
||||
def can_be_viewed_by(self, user):
|
||||
# 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
|
||||
user.was_subscribed())# or user.can_view(file)
|
||||
user.was_subscribed)# or user.can_view(file)
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse('sas:album', kwargs={'album_id': self.id})
|
||||
|
@ -160,7 +160,7 @@ class AlbumUploadView(CanViewMixin, DetailView, FormMixin):
|
||||
self.form = self.get_form()
|
||||
parent = SithFile.objects.filter(id=self.object.id).first()
|
||||
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():
|
||||
self.form.process(parent=parent, owner=request.user, files=files,
|
||||
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)
|
||||
parent = SithFile.objects.filter(id=self.object.id).first()
|
||||
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():
|
||||
self.form.process(parent=parent, owner=request.user, files=files,
|
||||
automodere=request.user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID))
|
||||
|
Loading…
Reference in New Issue
Block a user