def get(self, request, *args, **kwargs):
     try:
         self.queryset = UserCategory.objects.all().filter(owner=request.user)
         super(UserCategoryList, self).get(request, args, kwargs)
         return self.render_to_response(self.get_context_data())
     except Exception, e:
         logger.exception("Exception: " + str(e))
         messages.add_message(request, level=constants.ERROR, message="Sorry, something bad happened. Please retry!")
         return HttpResponseRedirect(reverse_lazy("loginmgmt:user_home"))
 def post(self, request, *args, **kwargs):
     try:
         folder_id = kwargs['pk']
         self.success_url = reverse_lazy('expense_folders:folder_details', args=(folder_id,))
         return super(AddExpenseFolderUsers, self).post(request, args, kwargs)
     except Exception, e:
         logger.exception("Exception: " + str(e))
         messages.add_message(request, level=constants.ERROR, message="Sorry, something bad happened. Please retry!")
         return HttpResponseRedirect(reverse_lazy('expense_folders:add_user_to_folder'))
    def get(self, request, *args, **kwargs):
        try:
            if request.user.is_authenticated():
                return HttpResponseRedirect(reverse('loginmgmt:user_home'))
        except Exception as e:
            logger.exception("Exception: " + str(e))
            messages.add_message(request, level=constants.ERROR, message="Sorry, something bad happened. Please retry!")
            return HttpResponseRedirect(reverse_lazy('sitehome:home'))

        return render(request, self.template_name)
 def get(self, request, *args, **kwargs):
     try:
         self.queryset = ExpenseFolder.objects.all().filter(
             Q(owner=request.user)
             | Q(id=(ExpenseFolderUserAssociation.objects.values("expense_folder_id").filter(user_id=request.user)))
         )
         super(ExpenseFolderList, self).get(request, args, kwargs)
         return self.render_to_response(self.get_context_data())
     except Exception, e:
         logger.exception("Exception: " + str(e))
         messages.add_message(request, level=constants.ERROR, message="Sorry, something bad happened. Please retry!")
         return HttpResponseRedirect(reverse_lazy("loginmgmt:user_home"))
 def get(self, request, *args, **kwargs):
     try:
         self.object = self.get_object()
         expense_form = self.get_form(self.get_form_class())
         expense_form.fields['expense_folder'].queryset = ExpenseFolder.objects.filter(owner_id=request.user)
         expense_form.fields['category'].queryset = UserCategory.objects.filter(owner_id=request.user)
         context = self.get_context_data(form=expense_form)
         return self.render_to_response(context)
     except Exception, e:
         logger.exception("Exception: " + str(e))
         messages.add_message(request, level=constants.ERROR, message="Sorry, something bad happened. Please retry!")
         return HttpResponseRedirect(reverse_lazy('loginmgmt:user_home'))
 def post(self, request, *args, **kwargs):
     try:
         form = self.get_form()
         if form.is_valid():
             default_folder_handler(form, request.user)
             return self.form_valid(form)
         else:
             return self.form_invalid(form)
     except Exception, e:
         logger.exception("Exception: " + str(e))
         messages.add_message(request, level=constants.ERROR, message="Sorry, something bad happened. Please retry!")
         return HttpResponseRedirect(reverse_lazy('loginmgmt:user_home'))
    def get(self, request, *args, **kwargs):
        try:
            if str(kwargs['pk']) != str(request.user.id):
                raise Exception("Trying to access invalid user!")

            super(UserProfileDetails, self).get(request, args, kwargs)
            return self.render_to_response(self.get_context_data())

        except Exception, e:
            logger.exception("Exception: " + str(e))
            messages.add_message(request, level=constants.ERROR,
                                 message="Sorry, no such user exists! Looks like you are lost, so we brought you back home!")
            return HttpResponseRedirect(reverse_lazy('loginmgmt:user_home'))
    def get(self, request, *args, **kwargs):
        try:
            if kwargs['pk']:
                expense = Expense.objects.all().filter(id=kwargs['pk'], added_by=request.user)
                if not expense:
                    raise Exception("User doesnt have access to this expense.")

            super(ExpenseDetails, self).get(request, args, kwargs)
            return self.render_to_response(self.get_context_data())

        except Exception, e:
            logger.exception("Exception: " + str(e))
            messages.add_message(request, level=constants.ERROR,
                                 message="No such expense exists! Looks like you are lost, so we brought you back home!")
            return HttpResponseRedirect(reverse_lazy('loginmgmt:user_home'))
    def get(self, request, *args, **kwargs):
        try:
            self.object = self.get_object()
            folder_id = self.object.id
            expense_folder = ExpenseFolder.objects.all().filter(
                Q(id=folder_id, owner=request.user) | Q(id=(
                    ExpenseFolderUserAssociation.objects.values('expense_folder_id').filter(expense_folder_id=folder_id,
                                                                                            user_id=request.user))))

            if not expense_folder:
                raise Exception("User doesnt have access to this folder.")

            context = self.get_context_data(object=self.object)
            folder_members = ExpenseFolderUserAssociation.objects.select_related().filter(expense_folder_id=folder_id)
            context['folder_members'] = folder_members

            return self.render_to_response(context)
        except Exception, e:
            logger.exception("Exception: " + str(e))
            messages.add_message(request, level=constants.ERROR, message="No such folder exists! Looks like you are lost, so we brought you back home!")
            return HttpResponseRedirect(reverse_lazy('loginmgmt:user_home'))
    def get(self, request, *args, **kwargs):
        try:
            folder_id = kwargs['pk']
            context = self.get_context_data()
            context['folder_id'] = folder_id

            form = self.get_form(self.get_form_class())

            # Building a filter where, we are only showing those users who are not yet added to the folder and are not owners
            form.fields['user_id'].queryset = User.objects.all().filter(
                    ~Q(id__in=(
                        ExpenseFolderUserAssociation.objects.values('user_id_id').filter(expense_folder_id=folder_id)))
                    & ~Q(id=(request.user.id)))

            context['form'] = form

            return self.render_to_response(context)
        except Exception, e:
            logger.exception("Exception: " + str(e))
            messages.add_message(request, level=constants.ERROR, message="Sorry, something bad happened. Please retry!")
            return HttpResponseRedirect(reverse_lazy('loginmgmt:user_home'))
    def post(self, request, *args, **kwargs):
        try:
            # use email as username
            email = request.POST['email']
            password = request.POST['password']

            # authenticate the user and get the user object
            user = authenticate(username=email, password=password)
            if user is not None:
                if user.is_active:
                    login(request, user)
                    logger.info("User %s logged in successfully!" % user.username)
                    return HttpResponseRedirect(reverse('loginmgmt:user_home'))
                else:
                    msg = "User %s is not active." % email
                    messages.add_message(request, level=constants.ERROR, message=msg)
                    logger.info(msg)
            else:
                msg = "User with email %s is not active." % email
                messages.add_message(request, level=constants.ERROR, message=msg)
                logger.info(msg)

        except Exception as e:
            messages.add_message(request, level=constants.ERROR, message="Failed to login. Please Retry!")
            logger.exception("Exception: " + str(e))

        return HttpResponseRedirect(reverse('loginmgmt:login'))
    def get(self, request, *args, **kwargs):
        try:
            expense_form = self.get_form(self.get_form_class())
            expense_form.fields['expense_folder'].queryset = ExpenseFolder.objects.filter(owner_id=request.user)
            expense_form.fields['category'].queryset = UserCategory.objects.filter(owner_id=request.user)

            # If a user is trying to add a expense from a folder, then set that as default selected
            if kwargs.has_key('pk'):
                folder_id = kwargs['pk']
                expense_form.initial['expense_folder'] = folder_id
            # Else get the default folder for the user and use it as default selected
            else:
                default_folder = ExpenseFolder.objects.values('id').filter(
                    Q(is_default_folder=True) & Q(owner=request.user))
                if default_folder:
                    expense_form.initial['expense_folder'] = default_folder[0]['id']

            context = self.get_context_data(form=expense_form)
            return self.render_to_response(context)
        except Exception, e:
            logger.exception("Exception: " + str(e))
            messages.add_message(request, level=constants.ERROR, message="Sorry, something bad happened. Please retry!")
            return HttpResponseRedirect(reverse_lazy('loginmgmt:user_home'))
Exemple #13
0
 def form_valid(self, form):
     form.save(commit=False)
     messages.add_message(self.request, messages.INFO,
                          _('Thank you ! Your message has been sent.'))
     return super().form_valid(form)
                    msg = "User with the email " + email + " already exists. Please try a different email address!"
                    raise Exception(msg)
                else:
                    if user:
                        # If a user is authenticated and created successfully then setup user defaults.
                        self.set_user_defaults(user)
                        # Next authenticate and login the user
                        user = authenticate(username=email, password=password)
                        if user.is_authenticated() and user.is_active:
                            login(request, user)
                            logger.info("User %s logged in successfully!", user.username)
                            return HttpResponseRedirect(reverse('loginmgmt:user_home'))
                        else:
                            msg = "User not authenticated or active: Username = %s", email
                            logger.info(msg)
                            messages.add_message(request, level=constants.ERROR, message=msg)
                    else:
                        msg = "Unable to add user. email %s already exists. Please Retry.", email
                        logger.error(msg)
                        messages.add_message(request, level=constants.ERROR, message=msg)

        except Exception as e:
            if not e:
                e = "Failed to add user. Please Retry!"
                logger.exception("Exception: " + str(e))
            messages.add_message(request, level=constants.ERROR, message=str(e))

        return HttpResponseRedirect(reverse('loginmgmt:register'))

    def set_user_defaults(self, user):
        # Once a user is created successfully, then create a default folder for that user.