Esempio n. 1
0
def edit(request,
         id,
         form_class=AssociationForm,
         template_name="associations/edit.html"):
    asso = get_object_or_404(Association, pk=id)

    # check permission
    if not request.user.is_superuser:
        raise Http403

    form = form_class(instance=asso, user=request.user)

    if request.method == "POST":
        form = form_class(request.POST, instance=asso, user=request.user)
        if form.is_valid():
            asso = form.save()

            msg_string = 'Successfully updated %s' % unicode(asso)
            messages.add_message(request, messages.SUCCESS, _(msg_string))

            return HttpResponseRedirect(reverse('news.search'))

    return render_to_response(template_name, {
        'association': asso,
        'form': form
    },
                              context_instance=RequestContext(request))
Esempio n. 2
0
 def dispatch(self, request, *args, **kwargs):
     if self.request.user.is_authenticated:
         messages.add_message(self.request, messages.INFO,
                               "User already logged in ")
         return redirect('/dashboard')
     else:
         return super().dispatch(request, *args, **kwargs)
Esempio n. 3
0
    def get(self, *args, **kwargs):
        '''Add user to private skate group, create user profile and add user to ChildSkater model when the page is requested.'''
        try:
            group = self.group_model.objects.get(name=kwargs['slug'])
            self.request.user.groups.add(group)
        except Group.DoesNotExist:
            messages.add_message(self.request, messages.ERROR, 'The URL you provided is incorrect.  Please verify the URL and try again!')

        try:
            # If a profile already exists, do nothing
            profile = self.profile_model.objects.get(user=self.request.user)
        except ObjectDoesNotExist:
            # If no profile exists, create one
            profile = self.profile_model(user=self.request.user, slug=self.request.user.id)
            profile.save()

        try:
            # Check if user is already in ChildSkater model
            ChildSkater.objects.get(user=self.request.user, first_name=self.request.user.first_name, last_name=self.request.user.last_name)
        except ObjectDoesNotExist:
            # Add user to ChildSkater model
            ChildSkater.objects.create(user=self.request.user, first_name=self.request.user.first_name, last_name=self.request.user.last_name, date_of_birth=None)
            pass

        return super().get(self.request, *args, **kwargs)
Esempio n. 4
0
 def get(self, request):
     context = {
         'user': self.request.user,
     }
     if not request.user.phone_number_verified:
         messages.add_message (self.request, messages.INFO,
                               "User Not verified.")
     return render (self.request, self.template_name, context)
Esempio n. 5
0
    def dados_validos_para_alterar_senha(self, request, senha_atual):
        valido = True
        username = request.user.username
        user = authenticate(request, username=username, password=senha_atual)

        if user is None:
            messages.add_message(request, messages.INFO,
                                 'A senha informada está incorreta')
            valido = False
        return valido
Esempio n. 6
0
 def post(self, request):
     username = request.POST.get('username')
     password = request.POST.get('password')
     user = authenticate(username=username, password=password)
     if user is not None:
         login(request, user)
         return redirect('/')
     else:
         messages.add_message(
             request, messages.ERROR,
             'Verifique as credenciais digitadas e tente novamente')
     return redirect('/usuario/entrar')
Esempio n. 7
0
    def form_valid(self, form):
        self.object = form.save(commit=False)
        # Get the slug for the PrivateSkate object to use in the success_url
        slug = self.object.skate_date.private_skate.slug
        self.success_url = reverse_lazy('private_skates:skate-dates', kwargs={'slug': slug})
        user_credit = UserCredit.objects.get(user=self.request.user)
        credit_used = False # Used to set the success message
        price = 0

        # Get the number of skaters and goalies that are registered for the skate
        registered_skaters = models.PrivateSkateDate.registered_skaters(self.object.skate_date)

        try:
            # If goalie or skater spots are full, do not save object and set message
            if self.object.goalie and registered_skaters['num_goalies'] == self.object.skate_date.private_skate.max_goalies:
                messages.add_message(self.request, messages.ERROR, 'Sorry, goalie spots are full!')
                return redirect('private_skates:skate-dates', slug=slug)
            elif not self.object.goalie and registered_skaters['num_skaters'] == self.object.skate_date.private_skate.max_skaters:
                messages.add_message(self.request, messages.ERROR, 'Sorry, skater spots are full!')
                return redirect('private_skates:skate-dates', slug=slug)

            # Get the price of the skate
            if self.object.goalie:
                price = self.object.skate_date.private_skate.goalie_price
                if price == 0:
                    self.object.paid = True
            else:
                price = self.object.skate_date.private_skate.skater_price

            # Determine if user credit was used to register
            if user_credit.balance >= price and user_credit.paid:
                self.object.paid = True
                user_credit.balance -= price
                if user_credit.balance == 0:
                    user_credit.paid = False
                user_credit.save()
                credit_used = True
            else:
                if price != 0:
                    self.add_to_cart(price)
            self.object.save()
        except:
            pass

        # If all goes well, set success message and return
        if price == 0:
            messages.add_message(self.request, messages.INFO, 'You have successfully registered for the skate!')
        elif credit_used:
            messages.add_message(self.request, messages.INFO, f'You have successfully registered for the skate! ${price} in credit has been deducted from your balance.')
        else:
            messages.add_message(self.request, messages.INFO, 'To complete your registration, you must view your cart and pay for your item(s)!')
        return super().form_valid(form)
Esempio n. 8
0
def getLogin(request):
    if request.user.is_authenticated:
        return redirect('index')
    else:
        if request.method == "POST":
            user = request.POST.get('user')
            password = request.POST.get('pass')
            auth = authenticate(request, username=user, password=password)
            if auth is not None:
                login(request, auth)
                return redirect('index')
            else:
                messages.add_message(request, messages.ERROR,
                                     'username or Password mismatch')
                return render(request, 'login.html')
    return render(request, 'login.html')
Esempio n. 9
0
def password(request):
    user = request.user
    if request.method == 'POST':
        form = ChangePasswordForm(request.POST)
        if form.is_valid():
            new_password = form.cleaned_data.get('new_password')
            user.set_password(new_password)
            user.save()
            update_session_auth_hash(request, user)
            messages.add_message(request, messages.SUCCESS,
                                 'Your password was successfully changed.')
            return redirect('authentication:profile_edit')

    else:
        form = ChangePasswordForm(instance=user)

    return render(request, 'authentication/password.html', {'form': form})
Esempio n. 10
0
    def delete(self, *args, **kwargs):
        # Clear the session from the cart
        skate_date = self.model.objects.filter(id=kwargs['pk']).values_list('skate_date', flat=True)
        skate_date_object = models.PrivateSkateDate.objects.all().filter(id=skate_date[0])
        skater_id = self.model.objects.filter(id=kwargs['pk']).values_list('skater', flat=True)
        skater = ChildSkater.objects.get(id=skater_id[0])
        skater_name = f"{skater.first_name} {skater.last_name}"
        cart_date = skate_date_object[0].date
        Cart.objects.filter(
            item=skate_date_object[0].private_skate.name, 
            event_date=cart_date,
            skater_name=skater_name
            ).delete()

        # Set success message and return
        messages.add_message(self.request, messages.SUCCESS, 'Skater has been removed from the skate session!')
        self.success_url = reverse_lazy('private_skates:skate-dates', kwargs={'slug': skate_date_object[0].private_skate.slug})
        return super().delete(*args, **kwargs)
Esempio n. 11
0
def add(request,
        form_class=AssociationForm,
        template_name="associations/add.html"):
    if request.method == "POST":
        form = form_class(request.POST)
        if form.is_valid():
            asso = form.save()

            msg_string = 'Successfully added %s' % unicode(asso)
            messages.add_message(request, messages.SUCCESS, _(msg_string))

            # TODO: init association data(navs,boxes,stories,......)

            return HttpResponseRedirect(reverse('home'))
    else:
        form = form_class(user=request.user)

    return render_to_response(template_name, {'form': form},
                              context_instance=RequestContext(request))
Esempio n. 12
0
      def form_valid(self, form):
          user = form.login(self.request)
          if user.two_factor_auth is False:
              login(self.request, user)
              return redirect('/dashboard')

          else:
             try:
                 response = send_verification_code(user)
                 pass
             except Exception  as e:
                 messages.add_message(self.request, messages.ERROR,
                                      data['message'])
                 return redirect('/login')

          if data['success'] == True:
              self.request.method = "GET"
              print (self.request.method)
              kwargs = {'user': user}
              return PhoneVerificationView (self.request, **kwargs)
          else:
              messages.add_message (self.request, messages.ERROR,
                                    data['message'])
              return redirect ('/login')
Esempio n. 13
0
 def message_test(self, request, queryset):
     messages.add_message(request, messages.SUCCESS, '操作成功123123123123')