Exemplo n.º 1
0
def generate_share(request, cid):
    """generate a temporary share link for a container

       Parameters
       ==========
       cid: the container to generate a share link for
    """
    container = get_container(cid)
    edit_permission = container.has_edit_permission(request)

    if edit_permission:
        days = request.POST.get("days", None)
        if days is not None:
            days = int(days)
            try:
                expire_date = calculate_expiration_date(days)
                share, _ = Share.objects.get_or_create(container=container,
                                                       expire_date=expire_date)
                share.save()

                # Generate an expiration task
                django_rq.enqueue(expire_share, sid=share.id, eta=expire_date)

                link = reverse(
                    "download_share",
                    kwargs={
                        "cid": container.id,
                        "secret": share.secret
                    },
                )

                expire_date = datetime.strftime(expire_date, "%b %m, %Y")
                response = {
                    "status": "success",
                    "days": days,
                    "expire": expire_date,
                    "link": link,
                }
            except:
                response = {"status": "error", "days": days}

        return JsonResponse(response)

    return JsonResponse(
        {"error": "You are not allowed to perform this action."})
Exemplo n.º 2
0
def generate_share(request, cid):
    '''generate a temporary share link for a container
    :param cid: the container to generate a share link for
    '''
    container = get_container(cid)
    edit_permission = container.has_edit_permission(request)

    if edit_permission == True:
        days = request.POST.get('days', None)
        if days is not None:
            days = int(days)
            try:
                expire_date = calculate_expiration_date(days)
                share, created = Share.objects.get_or_create(
                    container=container, expire_date=expire_date)
                share.save()

                # Generate an expiration task
                expire_share.apply_async(kwargs={"sid": share.id},
                                         eta=expire_date)

                link = reverse('download_share',
                               kwargs={
                                   'cid': container.id,
                                   'secret': share.secret
                               })

                expire_date = datetime.strftime(expire_date, '%b %m, %Y')
                response = {
                    "status": "success",
                    "days": days,
                    "expire": expire_date,
                    "link": link
                }
            except:
                response = {"status": "error", "days": days}

        return JsonResponse(response)

    return JsonResponse(
        {"error": "You are not allowed to perform this action."})