Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
54 lines (46 sloc) 2.37 KB
#!/usr/bin/env python
#--------------------------------------------------------------------------------------------------------#
#------------traversing the static folder and getting all the links---------------#
#--------------------------------------------------------------------------------------------------------#
from django.core.management import setup_environ
from django.contrib.auth.models import User
from django.core.validators import email_re
from django.db import IntegrityError
from learning_outcomes.models import PdfTemp
import csv
import os
from learning import settings
setup_environ(settings)
#--------------------------------------------------------------------------------------------------------#
#--The structure pdf file names will be----------------------------------------------------------#
#---splitting the file name with delimiter ',' and returning
#--------------------------------------------------------------------------------------------------------#
def split_with_comma(name):
filename=name.split(',')
return filename[0],filename[1],filename[2],filename[3],filename[4],filename[5]
email=''
student=''
school=''
year_class=''
test_taken=''
subject_taken=''
#--------------------------------------------------------------------------------------------------------#
#-----------------joining the path /static/pdf so that it can be prefixed to the pdf file name-----------#
#--------------------------------------------------------------------------------------------------------#
path = os.path.join('static','pdf')
path1= os.path.join('/static','pdf')
os.chdir(os.path.join(os.path.dirname(__file__),path)) #----changing the directory to /static/pdf
pdf_in_folder = os.listdir(os.getcwd()) #-----listing all the files in the directory and adding it
#-----pdf_in_folder variable
#--retrieving all the links to check if the file is already added
#--so that duplicate values dont get added
pdfs =PdfTemp.objects.all()
pdf_in_db=[]
for pdf in pdfs:
pdf_in_db.append(pdf.pdf_path)
for pdf in pdf_in_folder:
email,student,school,year_class,test_taken,subject_taken=split_with_comma(pdf)
final_filename=os.path.join(path1,pdf)
if final_filename not in pdf_in_db:
save_in_db=PdfTemp.objects.create(pdf_path=final_filename,email=email,student_name=student,school_name=school,year=year_class,test=test_taken,subject=subject_taken)
save_in_db.save()