Python django.shortcuts.redirect() Examples

The following are 30 code examples for showing how to use django.shortcuts.redirect(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.shortcuts , or try the search function .

Example 1
Project: Servo   Author: fpsw   File: product.py    License: BSD 2-Clause "Simplified" License 7 votes vote down vote up
def delete_product(request, pk, group):
    from django.db.models import ProtectedError
    product = get_object_or_404(Product, pk=pk)

    if request.method == 'POST':
        try:
            product.delete()
            Inventory.objects.filter(product=product).delete()
            messages.success(request, _("Product deleted"))
        except ProtectedError:
            messages.error(request, _('Cannot delete product'))

        return redirect(list_products, group)

    action = request.path
    return render(request, 'products/remove.html', locals()) 
Example 2
Project: Servo   Author: fpsw   File: product.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def upload_gsx_parts(request, group=None):
    from servo.forms.product import PartsImportForm
    form = PartsImportForm()

    data = {'action': request.path}

    if request.method == "POST":

        form = PartsImportForm(request.POST, request.FILES)

        if form.is_valid():
            data = form.cleaned_data
            filename = "servo/uploads/products/partsdb.csv"
            destination = open(filename, "wb+")

            for chunk in data['partsdb'].chunks():
                destination.write(chunk)

            messages.success(request, _("Parts database uploaded for processing"))
            return redirect(list_products)

    data['form'] = form
    return render(request, "products/upload_gsx_parts.html", data) 
Example 3
Project: Servo   Author: fpsw   File: customer.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def edit_group(request, group='all'):
    if group == 'all':
        group = CustomerGroup()
    else:
        group = CustomerGroup.objects.get(slug=group)

    title = group.name
    form = GroupForm(instance=group)

    if request.method == "POST":
        form = GroupForm(request.POST, instance=group)
        if form.is_valid():
            group = form.save()
            messages.success(request, _(u'%s saved') % group.name)
            return redirect(index, group.slug)
        messages.error(request, form.errors['name'][0])
        return redirect(index)

    return render(request, "customers/edit_group.html", locals()) 
Example 4
Project: Servo   Author: fpsw   File: customer.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def move(request, pk, new_parent=None):
    """
    Moves a customer under another customer
    """
    customer = get_object_or_404(Customer, pk=pk)

    if new_parent is not None:
        if int(new_parent) == 0:
            new_parent = None
            msg = _(u"Customer %s moved to top level") % customer
        else:
            new_parent = Customer.objects.get(pk=new_parent)
            d = {'customer': customer, 'target': new_parent}
            msg = _(u"Customer %(customer)s moved to %(target)s") % d

        try:
            customer.move_to(new_parent)
            customer.save() # To update fullname
            messages.success(request, msg)
        except Exception as e:
            messages.error(request, e)

        return redirect(customer)

    return render(request, "customers/move.html", locals()) 
Example 5
Project: Servo   Author: fpsw   File: account.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def edit_calendar(request, pk=None, view="week"):
    from servo.models.calendar import CalendarForm
    calendar = Calendar(user=request.user)

    if pk:
        calendar = get_object_or_404(Calendar, pk=pk)
        if not calendar.user == request.user:
            messages.error(request, _('You can only edit your own calendar'))
            return redirect(calendars)

    if request.method == "POST":
        form = CalendarForm(request.POST, instance=calendar)

        if form.is_valid():
            calendar = form.save()
            messages.success(request, _("Calendar saved"))
            return redirect(view_calendar, calendar.pk, 'week')

    form = CalendarForm(instance=calendar)

    data = {'title': calendar.title}
    data['form'] = form
    data['action'] = request.path

    return render(request, "accounts/calendar_form.html", data) 
Example 6
Project: Servo   Author: fpsw   File: account.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def register(request):
    """
    New user applying for access
    """
    form = RegistrationForm()
    data = {'title': _("Register")}

    if request.method == 'POST':

        form = RegistrationForm(request.POST)

        if form.is_valid():
            user = User(is_active=False)
            user.email = form.cleaned_data['email']
            user.last_name = form.cleaned_data['last_name']
            user.first_name = form.cleaned_data['first_name']
            user.set_password(form.cleaned_data['password'])
            user.save()

            messages.success(request, _(u'Your registration is now pending approval.'))

            return redirect(login)

    data['form'] = form
    return render(request, 'accounts/register.html', data) 
Example 7
Project: Servo   Author: fpsw   File: device.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def delete_device(request, product_line, model, pk):
    dev = get_object_or_404(Device, pk=pk)

    if request.method == 'POST':
        from django.db.models import ProtectedError
        try:
            dev.delete()
            messages.success(request, _("Device deleted"))
        except ProtectedError:
            messages.error(request, _("Cannot delete device with GSX repairs"))
            return redirect(dev)

        return redirect(index)

    data = {'action': request.path}
    data['device'] = dev

    return render(request, "devices/remove.html", data) 
Example 8
Project: Servo   Author: fpsw   File: device.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def update_gsx_details(request, pk):
    """
    Updates devices GSX warranty details
    """
    device = get_object_or_404(Device, pk=pk)
    try:
        GsxAccount.default(request.user)
        device.update_gsx_details()
        messages.success(request, _("Warranty status updated successfully"))
    except Exception as e:
        messages.error(request, e)

    if request.session.get('return_to'):
        return redirect(request.session['return_to'])

    return redirect(device) 
Example 9
Project: Servo   Author: fpsw   File: order.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def close(request, pk):
    """Close this Service Order."""
    order = get_object_or_404(Order, pk=pk)

    if request.method == 'POST':
        try:
            order.close(request.user)
        except Exception as e:
            messages.error(request, e)
            return redirect(order)

        if request.session.get("current_order_id"):
            del(request.session['current_order_id'])
            del(request.session['current_order_code'])
            del(request.session['current_order_customer'])

        messages.success(request, _('Order %s closed') % order.code)

        return redirect(order)

    data = {'order': order, 'action': reverse(close, args=[pk])}
    return render(request, "orders/close.html", data) 
Example 10
Project: Servo   Author: fpsw   File: order.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def remove_user(request, pk, user_id):
    """
    Removes this user from the follower list, unsets assignee
    """
    order = get_object_or_404(Order, pk=pk)
    user  = get_object_or_404(User, pk=user_id)

    try:
        order.remove_follower(user)
        if user == order.user:
            order.set_user(None, request.user)
        msg = _('User %s removed from followers') % user
        order.notify("unset_user", msg, request.user)
    except Exception as e:
        messages.error(request, e)

    return redirect(order) 
Example 11
Project: Servo   Author: fpsw   File: order.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def device_from_product(request, pk, item_id):
    """
    Turns a SOI into a device and attaches it to this order
    """
    order = get_object_or_404(Order, pk=pk)
    soi = ServiceOrderItem.objects.get(pk=item_id)

    try:
        GsxAccount.default(request.user, order.queue)
        device = Device.from_gsx(soi.sn, user=request.user)
        device.save()
        event = order.add_device(device, request.user)
        messages.success(request, event)
    except Exception as e:
        messages.error(request, e)

    return redirect(order) 
Example 12
Project: Servo   Author: fpsw   File: order.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def remove_product(request, pk, item_id):
    order = get_object_or_404(Order, pk=pk)

    # The following is to help those who hit Back after removing a product
    try:
        item = ServiceOrderItem.objects.get(pk=item_id)
    except ServiceOrderItem.DoesNotExist:
        messages.error(request, _("Order item does not exist"))
        return redirect(order)

    if request.method == 'POST':
        msg = order.remove_product(item, request.user)
        messages.info(request, msg)
        return redirect(order)

    return render(request, 'orders/remove_product.html', locals()) 
Example 13
Project: Servo   Author: fpsw   File: note.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def copy(request, pk):
    """Copy a note with its attachments and labels."""
    note = get_object_or_404(Note, pk=pk)

    new_note = Note(created_by=request.user)
    new_note.body = note.body
    new_note.order = note.order
    new_note.subject = note.subject
    new_note.save()

    new_note.labels = note.labels.all()

    for a in note.attachments.all():  # also copy the attachments
        a.pk = None
        a.content_object = new_note
        a.save()
        new_note.attachments.add(a)

    return redirect(edit, pk=new_note.pk, order_id=note.order_id) 
Example 14
Project: Servo   Author: fpsw   File: note.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def delete_note(request, pk):
    """
    Deletes a note
    """
    note = get_object_or_404(Note, pk=pk)

    if request.method == 'POST':
        note.delete()
        messages.success(request, _("Note deleted"))

        if request.session.get('return_to'):
            url = request.session.get('return_to')
            del(request.session['return_to'])
        elif note.order_id:
            url = note.order.get_absolute_url()

        return redirect(url)

    return render(request, 'notes/remove.html', {'note': note}) 
Example 15
Project: Servo   Author: fpsw   File: gsx.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def import_repair(request, order_pk, device_pk):
    from servo.models import Device
    order = get_object_or_404(Order, pk=order_pk)
    device = get_object_or_404(Device, pk=device_pk)

    action = request.path
    form = ImportForm()

    if request.method == 'POST':
        form = ImportForm(request.POST)
        if form.is_valid():
            confirmation = form.cleaned_data['confirmation']
            try:
                repair = Repair.create_from_gsx(confirmation,
                                                order,
                                                device,
                                                request.user)
                messages.success(request, _('GSX repair %s imported successfully' % confirmation))
                return redirect(repair)
            except Exception as e:
                messages.error(request, e)
                return redirect(order)

    return render(request, "repairs/import_repair.html", locals()) 
Example 16
Project: Servo   Author: fpsw   File: gsx.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def add_part(request, repair, part):
    """
    Adds this part to this GSX repair
    """
    rep = get_object_or_404(Repair, pk=repair)
    soi = rep.order.serviceorderitem_set.get(pk=part)

    if request.method == "POST":
        try:
            part = rep.add_part(soi, request.user)
            data = {'part': part.part_number, 'repair': rep.confirmation}
            msg = _("Part %(part)s added to repair %(repair)s") % data
            messages.success(request, msg)
        except gsxws.GsxError, e:
            messages.error(request, e)

        return redirect(rep.order) 
Example 17
Project: Servo   Author: fpsw   File: gsx.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def delete_repair(request, pk):
    """Delete this unsubmitted GSX repair."""
    repair = get_object_or_404(Repair, pk=pk)

    if repair.is_submitted():
        messages.error(request, _('Submitted repairs cannot be deleted'))
        return redirect(repair.order)

    if request.method == 'POST':
        order = repair.order
        repair.delete()
        messages.success(request, _('GSX repair deleted'))
        return redirect(order)

    context = {'action': request.path}
    return render(request, 'repairs/delete_repair.html', context) 
Example 18
Project: Servo   Author: fpsw   File: shipments.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def add_to_return(request, pk, part=None):
    """
    Adds a part to a bulk return
    """
    data = {'action': request.path}

    if pk and part:
        shipment = get_object_or_404(Shipment, pk=pk)
        part = get_object_or_404(ServicePart, pk=part)
        shipment.servicepart_set.add(part)
        messages.success(request, _(u"Part %s added to return") % part.part_number)

        return redirect(edit_bulk_return)

    if request.method == "POST":
        query = request.POST.get('q')
        results = ServicePart.objects.filter(return_order=query)
        data = {'shipment': pk, 'results': results}

        return render(request, "shipments/add_to_return-results.html", data)

    return render(request, "shipments/add_to_return.html", data) 
Example 19
Project: django-classified   Author: slyapustin   File: views.py    License: MIT License 5 votes vote down vote up
def form_valid(self, form, formset):
        self.object = form.save()
        formset.instance = self.object
        formset.save()
        if hasattr(self, 'get_success_message'):
            self.get_success_message(form)
        return redirect(self.object.get_absolute_url()) 
Example 20
Project: django-classified   Author: slyapustin   File: views.py    License: MIT License 5 votes vote down vote up
def dispatch(self, *args, **kwargs):
        profile = Profile.get_or_create_for_user(self.request.user)
        if not profile.allow_add_item():
            messages.error(self.request, _('You have reached the limit!'))
            return redirect(reverse('django_classified:user-items'))

        return super(ItemCreateView, self).dispatch(*args, **kwargs) 
Example 21
Project: django-classified   Author: slyapustin   File: views.py    License: MIT License 5 votes vote down vote up
def get(self, request):
        area_slug = request.GET.get('area_slug')
        if area_slug:
            area = get_object_or_404(Area, slug=area_slug)
            area.set_for_request(request)
        else:
            Area.delete_for_request(request)

        next_url = self.request.GET.get('next') or reverse_lazy('django_classified:index')

        return redirect(next_url) 
Example 22
Project: MPContribs   Author: materialsproject   File: views.py    License: MIT License 5 votes vote down vote up
def download(request, project):
    cname = os.environ["PORTAL_CNAME"]
    s3obj = f"{S3_DOWNLOAD_URL}{cname}/{project}.json.gz"
    return redirect(s3obj)
    # TODO check if exists, generate if not, progressbar...
    # return HttpResponse(status=404) 
Example 23
Project: Servo   Author: fpsw   File: product.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def edit_category(request, slug=None, parent_slug=None):

    form = CategoryForm()
    category = ProductCategory()

    if slug is not None:
        category = get_object_or_404(ProductCategory, slug=slug)
        form = CategoryForm(instance=category)

    if parent_slug is not None:
        parent = get_object_or_404(ProductCategory, slug=parent_slug)
        form = CategoryForm(initial={'parent': parent.pk})

    if request.method == "POST":
        form = CategoryForm(request.POST, instance=category)
        if form.is_valid():
            try:
                category = form.save()
            except IntegrityError:
                error = _(u'Category %s already exists') % category.title
                messages.error(request, error)
                return redirect(list_products)
            messages.success(request, _(u"Category %s saved") % category.title)
            return redirect(category)
        else:
            messages.error(request, form.errors)
            return redirect(list_products)

    return render(request, "products/category_form.html", locals()) 
Example 24
Project: Servo   Author: fpsw   File: product.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def delete_category(request, slug):

    category = get_object_or_404(ProductCategory, slug=slug)

    if request.method == "POST":
        category.delete()
        messages.success(request, _("Category deleted"))
        return redirect(list_products)

    data = {'category': category}
    data['action'] = request.path
    return render(request, 'products/delete_category.html', data) 
Example 25
Project: Servo   Author: fpsw   File: product.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def update_price(request, pk):
    """
    Updates the price info from GSX
    """
    product = get_object_or_404(Product, pk=pk)
    try:
        GsxAccount.default(request.user)
        product.update_price()
        messages.success(request, _('Price info updated from GSX'))
    except Exception as e:
        messages.error(request, _('Failed to update price from GSX'))

    return redirect(product) 
Example 26
Project: Servo   Author: fpsw   File: customer.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def index(request, group='all'):
    data = prepare_view(request, group)
    request.session['customer_query'] = None

    if data['customer_list']:
        customer = data['customer_list'][0]
        return redirect(view, pk=customer.pk, group=group)

    return render(request, "customers/index.html", data) 
Example 27
Project: Servo   Author: fpsw   File: customer.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def add_order(request, customer_id, order_id):
    order = get_object_or_404(Order, pk=order_id)
    customer = get_object_or_404(Customer, pk=customer_id)
    order.customer = customer
    order.save()

    for d in order.devices.all():
        customer.devices.add(d)

    customer.save()
    messages.success(request, _('Customer added'))
    return redirect(order) 
Example 28
Project: Servo   Author: fpsw   File: customer.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def delete_group(request, group):
    group = get_object_or_404(CustomerGroup, slug=group)

    if request.method == "POST":
        group.delete()
        messages.success(request, _(u'%s deleted') % group.name)
        return redirect(index)

    return render(request, "customers/delete_group.html", locals()) 
Example 29
Project: Servo   Author: fpsw   File: customer.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def delete(request, pk=None, group='all'):

    customer = get_object_or_404(Customer, pk=pk)

    if request.method == "POST":
        customer.delete()
        messages.success(request, _("Customer deleted"))
        return redirect(index, group=group)
    else:
        data = {'action': request.path, 'customer': customer}
        return render(request, "customers/remove.html", data) 
Example 30
Project: Servo   Author: fpsw   File: customer.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def create_message(request, pk):
    return redirect("servo.views.note.edit", customer=pk)