Exemplo n.º 1
0
    def post(self, request, car_id, task_id, **kwargs):
        """
        Handles any incoming post requests pointing to this view specifically
        for editing a task and deleting a task
        """
        if request.POST.get("delete"):
            response = APIHelper.delete_from_api('task/' + task_id,
                                                 self.request.user.auth_token)
            return HttpResponseRedirect('/')
        else:
            context = {}
            form = NewTaskForm(self.request.POST)

            if form.is_valid():
                form.cleaned_data['car_id'] = car_id
                if form.cleaned_data.get('completion_date') == '':
                    form.cleaned_data['completion_date'] = None
                APIHelper.put_to_api('task/' + task_id,
                                     self.request.user.auth_token,
                                     form.cleaned_data)
                context = self.get_context_data(car_id, task_id)
                context['message'] = 'Thank you! Your task has been updated.'
                return render(request, self.template_name, context)
            else:
                context['message'] = 'There was an error with your request.'
                return render(request, self.template_name, context)
Exemplo n.º 2
0
    def get_context_data(self, car_id, task_id, **kwargs):
        """
        Override the get_context_data method to add new data to the
        context dictionary that is passed to the template
        """
        context = super().get_context_data(**kwargs)
        car = APIHelper.get_from_api('car/' + car_id,
                                     self.request.user.auth_token)
        task = APIHelper.get_from_api('task/' + task_id,
                                      self.request.user.auth_token)
        context['car'] = car
        context['task'] = task

        return context
Exemplo n.º 3
0
    def post(self, request):
        """
        Handles any incoming post requests pointing to this view specifically
        for creating a new user
        """
        message = {}
        form = RegisterForm(self.request.POST)

        if form.is_valid():
            password = form.cleaned_data.get('password')
            confirmed_password = form.cleaned_data.get('confirmed_password')
            if password != confirmed_password:
                message = {
                    'msg':
                    'Your two passwords did not match. ' +
                    'Please re-register your account.'
                }
            else:
                del form.cleaned_data['confirmed_password']
                data = form.cleaned_data
                response = APIHelper.public_post_to_api('user/register/', data)
                if response != 200:
                    message = {
                        'msg':
                        'Username is already taken. ' +
                        'Please re-register your account.'
                    }
                else:
                    data = {
                        'username': form.cleaned_data.get('username'),
                        'password': form.cleaned_data.get('password')
                    }
                    # Create an API auth token for the new user
                    response = APIHelper.public_post_to_api('user/auth/', data)

                    message = {
                        'msg':
                        'Thank you for registering! ' +
                        'Please login with your new account.'
                    }
                    self.template_name = APP_TEMPLATE_DIR + "login.html"
        else:
            message = {
                'msg':
                'Something went wrong. ' + 'Please re-register your account.'
            }
            return render(request, self.template_name, message)
        return render(request, self.template_name, message)
Exemplo n.º 4
0
 def post(self, request, id, **kwargs):
     """
     Used to complete a task
     """
     form = CompleteTaskForm(self.request.POST)
     if form.is_valid():
         task_id = request.GET.get('task_id')
         task = APIHelper.get_from_api('task/' + task_id,
                                       self.request.user.auth_token)
         task['completion_date'] = form.cleaned_data['completion_date']
         APIHelper.put_to_api('task/' + task_id,
                              self.request.user.auth_token, task)
         context = self.get_context_data(id)
         context['message'] = 'Thank you, your task has been completed.'
         return render(request, self.template_name, context)
     else:
         return render(request, self.template_name)
Exemplo n.º 5
0
    def get_context_data(self, id, **kwargs):
        """
        Override the get_context_data method to add new data to the
        context dictionary that is passed to the template
        """
        context = super().get_context_data(**kwargs)
        car = APIHelper.get_from_api('car/' + id, self.request.user.auth_token)
        task_list = APIHelper.get_from_api('car/' + id + '/tasks/',
                                           self.request.user.auth_token)
        part_list = APIHelper.get_from_api('parts/',
                                           self.request.user.auth_token)

        context['car'] = car
        context['task_list'] = task_list
        context['part_list'] = part_list

        return context
Exemplo n.º 6
0
    def post(self, request, id, **kwargs):
        """
        Handles any incoming post requests pointing to this view specifically
        for creating a new task
        """
        context = self.get_context_data(id)
        form = NewTaskForm(self.request.POST)

        if form.is_valid():
            form.cleaned_data['car_id'] = id
            if form.cleaned_data.get('completion_date') == '':
                form.cleaned_data['completion_date'] = None
            APIHelper.post_to_api('car/' + id + '/tasks/',
                                  self.request.user.auth_token,
                                  form.cleaned_data)
            context['message'] = 'Thank you! Your task has been saved.'
            return render(request, self.template_name, context)
        else:
            context['message'] = 'There was an error with your request.'
            return render(request, self.template_name, context)
Exemplo n.º 7
0
    def get_context_data(self, **kwargs):
        """
        Override the get_context_data method to add new data to the
        context dictionary that is passed to the template
        """
        context = super().get_context_data(**kwargs)
        car_list = APIHelper.get_from_api('cars/',
                                          self.request.user.auth_token)
        context['car_list'] = car_list

        return context
Exemplo n.º 8
0
    def post(self, request, car_id, task_id, **kwargs):
        """
        Handles any incoming post requests pointing to this view specifically
        for creating a new part
        """
        context = {}
        form = NewPartForm(self.request.POST)
        context = self.get_context_data(car_id, task_id)


        if form.is_valid():
            form.cleaned_data['task_id'] = task_id
            APIHelper.post_to_api('parts/',
                                  self.request.user.auth_token,
                                  form.cleaned_data)
            context['message'] = 'Thank you! Your part has been saved.'
            return render(request, self.template_name, context)
        else:
            context['message'] = 'There was an error with your request.'
            return render(request, self.template_name, context)
Exemplo n.º 9
0
    def post(self, request, **kwargs):
        """
        Handles any incoming post requests pointing to this view specifically
        for creating a new car
        """
        context = self.get_context_data()
        form = NewCarForm(self.request.POST)

        if form.is_valid():
            form.cleaned_data['user_id'] = request.user.id
            response = APIHelper.post_to_api('cars/',
                                             self.request.user.auth_token,
                                             form.cleaned_data)
            print(response)
            context['message'] = 'Thank you! Your car has been saved.'
            return render(request, self.template_name, context)
        else:
            context['message'] = 'There was an error with your request.'
            return render(request, self.template_name, context)