Example #1
0
def download_course(request):
    """
        Function to serve the zip files of entire courses. The function updates the course_downloaded database appropriately.
    """
    course = request.GET.get('course', 'None')
    if course == 'None':
        return redirect('/books/')
    zip_location = os.path.join(DATABASE_DIR, course + '.zip')
    try:
        if not (os.path.isfile(zip_location)):
            zip_course(course)

        if not os.path.isfile(STATS_FILE):
            shutil.copy(stats_loc, STATS_FILE)

        with open(STATS_FILE, "r") as file:
            stats = json.load(file)
        course_stat = jsc.navigate_path(stats, course, True)

        try:
            course_stat['downloads'] += 1
        except:
            course_stat['downloads'] = 1
            course_stat['last'] = ''
        course_stat['last'] = datetime.now().strftime("%Y%m%d")

        stats = jsc.update_db(stats, course, course_stat)
        
        with open(STATS_FILE, "w") as file:
            json.dump(stats, file)
        
        return redirect(DATABASE_URL + '/' + course + '.zip')
    
    except OSError as e:
        return HttpResponse(status=400,content="Bad Request")
Example #2
0
def APIstructure(request):
    f = jsc.path_to_dict(DATABASE_DIR, DATABASE_DICT_FILE_NAME)
    path = request.GET.get('path', os.sep)
    depth = int(request.GET.get('depth', 3))
    try:
        db = jsc.navigate_path(f, path, False)
    except Exception as e:
        return Response({})
    else:
        truncated_db = jsc.truncate_db(db, depth)
        return Response(truncated_db)
Example #3
0
def tasks_handler(tasks):
    """
        Controller to modify database.json according to task
    """
    with open(DATABASE_DICT_FILE_NAME, "r") as file:
        data = json.load(file)
    for task in tasks:
        if task[TYPE]=="additions":
            db=data
            separated_path=(task[PATH]).split(os.sep)
            db=jsc.navigate_path(db, (os.sep).join(separated_path[:-1]), True)
            db[separated_path[-1]]=(DATABASE_DIR + task[PATH])[2:]
    with open(DATABASE_DICT_FILE_NAME, "w") as file:
        json.dump(data, file)
Example #4
0
def APIsearch(request):
    """
        gives a list of all path_prefix objects matching search terms
    """
    f = jsc.path_to_dict(DATABASE_DIR, DATABASE_DICT_FILE_NAME)  ####can this be drier? repeated code
    keyword_list = (request.GET.get('query', "")).split()
    path = request.GET.get('path', "/")
    path_prefix = search.get_path_prefix(path)
    try:
        db = jsc.navigate_path(f, path, False)
    except Exception as e:
        print("invalid path")
        return Response({})
    else:
        result = search.search_dic(db, path_prefix, keyword_list)
        return Response({"result": result})