Exemple #1
0
def add_monitor_course_page(request):
    if 'url' not in request.POST or not request.POST[
            'url'] or 'shortname' not in request.POST or not request.POST[
                'shortname']:
        return JsonError('Must provide both URL and short name.')
    url, shortname = request.POST['url'], request.POST['shortname']
    if not urlvalid(url):
        return JsonError('Invalid URL.')
    user_profile = request.user.profile
    if CoursePageMonitor.objects.filter(user_profile=user_profile,
                                        shortname=shortname).count():
        return JsonError('Short name already in use.')
    if CoursePageMonitor.objects.filter(user_profile=user_profile,
                                        url=url).count():
        return JsonError('Already monitored this page.')
    try:
        if getsize(url) > MAXIMUM_SIZE:
            return JsonError('Page too large')
    except ValueError:
        return JsonError(
            'Cannot access url or we cannot determine the file size.')
    CoursePageMonitor.objects.create(user_profile=user_profile,
                                     url=url,
                                     shortname=shortname)
    return JsonSuccess()
Exemple #2
0
def add_shortlink(request):
    try:
        if not request.POST.get('shortname') or not request.POST.get('url'):
            return JsonError('Must provide both shortname and url.')
        shortname = request.POST['shortname']
        user_profile = request.user.profile
        url = request.POST['url']
        if not urlvalid(url):
            return JsonError('Url format incorrect (common mistake: must start with http or https)')
        if ShortLink.objects.filter(shortname=shortname, user_profile=user_profile).count():
            return JsonError('Name already in use.')
        ShortLink.objects.create(shortname=shortname, url=url, user_profile=user_profile)
        return JsonSuccess('Shortlink is successfully added.')
    except Exception as e:
        print repr(e)
        return JsonError('Unknown error')
Exemple #3
0
def edit_shortlink(request, shortname):
    link_obj_list = ShortLink.objects.filter(user_profile=request.user.profile,
                                             shortname=shortname)
    if request.method == 'POST':
        try:
            if not request.POST.get('shortname') or not request.POST.get(
                    'url'):
                return JsonError('Must provide both shortname and url.')
            if not link_obj_list.count():
                return JsonError('Shortlink not found.')
            if link_obj_list.count() > 1:
                return JsonError('Multiple ShortLink instances found.')
            url = request.POST['url']
            shortname = request.POST['shortname']
            linkobj = link_obj_list[0]
            print url
            if not urlvalid(url):
                return JsonError(
                    'Url format incorrect (common mistake: must start with http or https)'
                )
            if linkobj.shortname != shortname and ShortLink.objects.filter(
                    shortname=shortname,
                    user_profile=request.user.profile).count():
                return JsonError('Name already in use.')
            linkobj.shortname = shortname
            linkobj.url = url
            linkobj.save()
            return JsonSuccess('Updated.')
        except Exception as e:
            print repr(e)
            return JsonError('Unknown error.')
    else:
        if not link_obj_list.count():
            return redirecterror(request, 'Shortlink not found.')
        if link_obj_list.count() > 1:
            return redirecterror(request,
                                 'Multiple ShortLink instances found.')
        linkobj = link_obj_list[0]
        params = {}
        params['form'] = ShortLinkForm(instance=linkobj)
        params['shortlink'] = linkobj
        params.update(csrf(request))
        return xrender(request, 'edit_shortlink.html', params)
Exemple #4
0
def add_shortlink(request):
    try:
        if not request.POST.get('shortname') or not request.POST.get('url'):
            return JsonError('Must provide both shortname and url.')
        shortname = request.POST['shortname']
        user_profile = request.user.profile
        url = request.POST['url']
        if not urlvalid(url):
            return JsonError(
                'Url format incorrect (common mistake: must start with http or https)'
            )
        if ShortLink.objects.filter(shortname=shortname,
                                    user_profile=user_profile).count():
            return JsonError('Name already in use.')
        ShortLink.objects.create(shortname=shortname,
                                 url=url,
                                 user_profile=user_profile)
        return JsonSuccess('Shortlink is successfully added.')
    except Exception as e:
        print repr(e)
        return JsonError('Unknown error')
Exemple #5
0
def add_monitor_course_page(request):
    if (
        "url" not in request.POST
        or not request.POST["url"]
        or "shortname" not in request.POST
        or not request.POST["shortname"]
    ):
        return JsonError("Must provide both URL and short name.")
    url, shortname = request.POST["url"], request.POST["shortname"]
    if not urlvalid(url):
        return JsonError("Invalid URL.")
    user_profile = request.user.profile
    if CoursePageMonitor.objects.filter(user_profile=user_profile, shortname=shortname).count():
        return JsonError("Short name already in use.")
    if CoursePageMonitor.objects.filter(user_profile=user_profile, url=url).count():
        return JsonError("Already monitored this page.")
    try:
        if getsize(url) > MAXIMUM_SIZE:
            return JsonError("Page too large")
    except ValueError:
        return JsonError("Cannot access url or we cannot determine the file size.")
    CoursePageMonitor.objects.create(user_profile=user_profile, url=url, shortname=shortname)
    return JsonSuccess()
Exemple #6
0
def edit_shortlink(request, shortname):
    link_obj_list = ShortLink.objects.filter(user_profile=request.user.profile, shortname=shortname)
    if request.method == 'POST':
        try:
            if not request.POST.get('shortname') or not request.POST.get('url'):
                return JsonError('Must provide both shortname and url.')
            if not link_obj_list.count():
                return JsonError('Shortlink not found.')
            if link_obj_list.count() > 1:
                return JsonError('Multiple ShortLink instances found.')
            url = request.POST['url']
            shortname = request.POST['shortname']
            linkobj = link_obj_list[0]
            print url
            if not urlvalid(url):
                return JsonError('Url format incorrect (common mistake: must start with http or https)')
            if linkobj.shortname != shortname and ShortLink.objects.filter(shortname=shortname, user_profile=request.user.profile).count():
                return JsonError('Name already in use.')
            linkobj.shortname = shortname
            linkobj.url = url
            linkobj.save()
            return JsonSuccess('Updated.')
        except Exception as e:
            print repr(e)
            return JsonError('Unknown error.')
    else:
        if not link_obj_list.count():
            return redirecterror(request, 'Shortlink not found.')
        if link_obj_list.count() > 1:
            return redirecterror(request, 'Multiple ShortLink instances found.')
        linkobj = link_obj_list[0]
        params = {}
        params['form'] = ShortLinkForm(instance=linkobj)
        params['shortlink'] = linkobj
        params.update(csrf(request))
        return xrender(request, 'edit_shortlink.html', params)