def force_integrity(request): """ controller to force recreate the json heirarchy and also take care of dangling meta files, and create missing meta files """ if request.method == "GET": for root, dirs, files in os.walk(DATABASE_DIR): for filename in files: if not filename.endswith('.zip'): if filename.endswith('.meta'): pathname = os.path.join(root, '.'.join(filename.split('.')[:-1])) if not (os.path.isfile(pathname)): os.remove(os.path.join(root, filename)) else: metafilename = filename + '.meta' metafile = os.path.join(root, metafilename) if not (os.path.isfile(metafile)): tags = generate_path_meta_tags(os.path.relpath(metafile, DATABASE_DIR)) with open(metafile,"w") as f: f.write('\n'.join(tags)) jsc.recreate_path(DATABASE_DIR, DATABASE_DICT_FILE_NAME) return redirect('/books/') else: return redirect('/books/')
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")
def finalize_approvals(request): """ controller to finalize all the approvals (calls the recreate path function from jsc) admin needs to click on finalize approvals once after manually pasting new files. """ if request.method == "GET": jsc.recreate_path(DATABASE_DIR, DATABASE_DICT_FILE_NAME) return redirect('/books/approve') else: return redirect('/books/approve')
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)
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})
def approve_unapproved_document(request): fileName = request.GET.get('name', 'none') seperatedlist = fileName.split(SEPARATOR) try: destination = DATABASE_DIR for directory in seperatedlist: destination = destination + "/" + directory shutil.copy(UNAPPROVED_DIR + fileName, destination) jsc.recreate_path(DATABASE_DIR, DATABASE_DICT_FILE_NAME) return redirect('/books/remove_unapproved_document?name=' + fileName) except IOError as e: if e.errno != errno.ENOENT: raise os.makedirs(os.path.dirname(destination)) shutil.copy(UNAPPROVED_DIR + fileName, destination) jsc.recreate_path(DATABASE_DIR, DATABASE_DICT_FILE_NAME) return redirect('/books/remove_unapproved_document?name=' + fileName)
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)