From 2a47552668e17189e83f1d03b49761722bc3249b Mon Sep 17 00:00:00 2001 From: krishna Date: Mon, 23 Dec 2013 11:43:03 +0530 Subject: [PATCH 1/3] delete implemented in file --- .../ndf/templates/ndf/ImageDashboard.html | 14 ++-- .../ndf/templates/ndf/UploadDoc.html | 2 +- .../gnowsys_ndf/ndf/templates/ndf/file.html | 32 +++++++- .../gnowsys_ndf/ndf/templatetags/ndf_tags.py | 1 - gnowsys-ndf/gnowsys_ndf/ndf/urls/file.py | 1 + gnowsys-ndf/gnowsys_ndf/ndf/views/file.py | 76 ++++++++----------- .../gnowsys_ndf/ndf/views/imageDashboard.py | 4 +- 7 files changed, 71 insertions(+), 59 deletions(-) diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/ImageDashboard.html b/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/ImageDashboard.html index e4ef6d539a..2ca85fb135 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/ImageDashboard.html +++ b/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/ImageDashboard.html @@ -3,11 +3,11 @@ {% block title %} Image Gallery {% endblock %} {% block body_content %} -{% if alreadyUploadedFiles %} +{% if already_uploaded %}
Listed below files are already uploaded please choose different files @@ -26,13 +26,11 @@
-
{% csrf_token %} - - - - + {% if user.is_authenticated %} + + Upload -
+ {% endif %}
diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/UploadDoc.html b/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/UploadDoc.html index 54c7575b31..9ae9b10ea2 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/UploadDoc.html +++ b/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/UploadDoc.html @@ -42,7 +42,7 @@

Submit Documents

- +
diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/file.html b/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/file.html index 1130dfbc53..e7886acf3c 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/file.html +++ b/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/file.html @@ -1,20 +1,31 @@ {% extends "ndf/base.html" %} {% block title %} {{title}} {% endblock %} -{% block body_content %} -{% if user.is_authenticated %} +{% block body_content %} +{% load ndf_tags %} +{% if already_uploaded %} +
+ Listed below files are already uploaded please choose different files + +
+{% endif %}

-{% endif %} {% load url from future %} {% if files %} @@ -29,11 +40,18 @@

File Size

File Type

+
+

Posted by

+

Download

+
+

Delete

+
{% for file in files %} + {% get_user_object file.created_by as user_obj %}
{{file.name}} @@ -44,9 +62,15 @@

Download

{{file.mime_type}}
+
+ {{ user_obj.username }} +
Download
+
+ Delete +
{% endfor %}
diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/templatetags/ndf_tags.py b/gnowsys-ndf/gnowsys_ndf/ndf/templatetags/ndf_tags.py index aff45c4437..0e0a15e159 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/templatetags/ndf_tags.py +++ b/gnowsys-ndf/gnowsys_ndf/ndf/templatetags/ndf_tags.py @@ -7,7 +7,6 @@ from gnowsys_ndf.ndf.models import * from gnowsys_ndf.ndf.views.methods import check_existing_group from gnowsys_ndf.ndf.views.methods import get_drawers - ####################################################################################################################################### register = Library() diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/urls/file.py b/gnowsys-ndf/gnowsys_ndf/ndf/urls/file.py index 4c32e4dac9..495e9c8e03 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/urls/file.py +++ b/gnowsys-ndf/gnowsys_ndf/ndf/urls/file.py @@ -19,5 +19,6 @@ url(r'^documentList/', 'GetDoc', name='documentList'), url(r'^readDoc/(?P<_id>[\w-]+)$', 'readDoc', name='read_file'), url(r'^search/$', 'file_search', name='file_search'), + url(r'^delete_file/(?P<_id>[\w-]+)$', 'delete_file', name='delete_file'), ) diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/views/file.py b/gnowsys-ndf/gnowsys_ndf/ndf/views/file.py index f8c8dd3dd8..a4cccc6d1c 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/views/file.py +++ b/gnowsys-ndf/gnowsys_ndf/ndf/views/file.py @@ -8,7 +8,7 @@ from django.http import HttpResponse from django.shortcuts import render_to_response #, render uncomment when to use from django.template import RequestContext - +from django.core.urlresolvers import reverse from django_mongokit import get_database try: @@ -46,21 +46,6 @@ def file(request, group_name, file_id): - """ - * Renders a list of all 'Files' available withinthe database and group them acording to mimetype. - - """ - print 'check:', - # mime_types=[] - # if gst_file._id == ObjectId(file_id): - # title = gst_file.name - # filecollection=db[File.collection_name] - # for each in filecollection.distinct("mime_type"): - # mime_types.append(filecollection.find({"mime_type":each})) - # return render_to_response("ndf/file.html", {'title': title,'files':mime_types}, context_instance=RequestContext(request)) - # else: - # return HttpResponseRedirect(reverse('homepage')) - """ * Renders a list of all 'Files' available within the database. @@ -69,19 +54,19 @@ def file(request, group_name, file_id): title = gst_file.name filecollection=db[File.collection_name] files=filecollection.File.find({'_type': u'File'}) - return render_to_response("ndf/file.html", {'title': title,'files':files}, context_instance=RequestContext(request)) - + already_uploaded=request.GET.get("already_uploaded","") + already_uploaded=list(already_uploaded) + print already_uploaded + return render_to_response("ndf/file.html", {'title': title,'files':files,'already_uploaded':already_uploaded}, context_instance=RequestContext(request)) else: return HttpResponseRedirect(reverse('homepage')) def uploadDoc(request,group_name): - stId, mainPageUrl = "", "" - if request.method=="POST": - stId=request.POST.get("stId","") - mainPageUrl=request.POST.get("pageUrl","") - template="ndf/UploadDoc.html" - if stId and mainPageUrl: - variable = RequestContext(request, {'stId': stId, 'mainPageUrl': mainPageUrl}) + if request.method=="GET": + page_url=request.GET.get("next","") + template="ndf/UploadDoc.html" + if page_url: + variable = RequestContext(request, {'page_url': page_url}) else: variable=RequestContext(request,{}) return render_to_response(template,variable) @@ -94,8 +79,8 @@ def submitDoc(request,group_name): if request.method=="POST": mtitle = request.POST.get("docTitle","") userid = request.POST.get("user","") - mainPageUrl = request.POST.get("mainPageUrl","") - print "url",mainPageUrl + page_url = request.POST.get("page_url","") + print "url",page_url #memberOf = request.POST.get("memberOf","") i=1 for index,each in enumerate(request.FILES.getlist("doc[]","")): @@ -107,23 +92,14 @@ def submitDoc(request,group_name): i=i+1 if f: alreadyUploadedFiles.append(f) - if 'image' in mainPageUrl: - collection=db[File.collection_name] - imgcol=collection.File.find({'mime_type': {'$regex': 'image'}}) - variable=RequestContext(request,{'alreadyUploadedFiles':alreadyUploadedFiles,'imageCollection':imgcol}) - template="ndf/ImageDashboard.html" - return render_to_response(template,variable) - else: - return HttpResponseRedirect("/"+group_name+"/file"+"/"+gst_file._id.__str__()) - # filecollection=get_database()[File.collection_name] - # files=filecollection.File.find('_type': u'File') - # variable=RequestContext(request,{'alreadyUploadedFiles':alreadyUploadedFiles,'filecollection':files}) - # template='ndf/DocumentList.html' - # return render_to_response(template,variable) + #url = reverse(page_url, kwargs={'alreadyUploadedFiles':alreadyUploadedFiles}) + return HttpResponseRedirect(page_url+'?already_uploaded='+str(alreadyUploadedFiles)) + else: + return HttpResponseRedirect(reverse('homepage')) + def save_file(files,title,userid,memberOf,stId): fcol=db[File.collection_name] - print "stid",stId fileobj=fcol.File() #gst=gst_collection.GSystemType.one({"_id":ObjectId(stId)}) filemd5= hashlib.md5(files.read()).hexdigest() @@ -233,7 +209,7 @@ def convertVideo(files,userid): else: secs = duration durationTime = str(str(hrs)+":"+str(mins)+":"+str(secs)) # calculating Time duration of video in hrs,mins,secs - proc = subprocess.Popen(['ffmpeg', '-i', str("/tmp/"+userid+"/"+fileVideoName+"/"+fileVideoName), '-ss', "00:00:30", "-s", "128*128", "-f", "image2", "-frames:v", "1", str("/tmp/"+userid+"/"+fileVideoName+"/"+initialFileName+".png")]) # creating thumbnail of video using ffmpeg + proc = subprocess.Popen(['ffmpeg', '-i', str("/tmp/"+userid+"/"+fileVideoName+"/"+fileVideoName), '-ss', "00:00:30", "-s", "128*128", "-f", "image2", str("/tmp/"+userid+"/"+fileVideoName+"/"+initialFileName+".png")]) # creating thumbnail of video using ffmpeg proc.wait() background = Image.open("/tmp/"+userid+"/"+fileVideoName+"/"+initialFileName+".png") draw = ImageDraw.Draw(background) @@ -266,4 +242,18 @@ def file_search(request, group_name): variable=RequestContext(request,{'file_collection':file_search,'view_name':'file_search'}) return render_to_response(template,variable) - +def delete_file(request,group_name,_id): + """Delete file and its data + """ + file_collection = db[File.collection_name] + pageurl=request.GET.get("next","") + print "testurl",pageurl + try: + cur=file_collection.File.one({'_id':ObjectId(_id)}) + if cur.fs_file_ids: + for each in cur.fs_file_ids: + cur.fs.files.delete(each) + cur.delete() + except Exception as e: + print "Exception:",e + return HttpResponseRedirect(pageurl) diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/views/imageDashboard.py b/gnowsys-ndf/gnowsys_ndf/ndf/views/imageDashboard.py index 4b38629a1c..95745ba2a1 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/views/imageDashboard.py +++ b/gnowsys-ndf/gnowsys_ndf/ndf/views/imageDashboard.py @@ -17,8 +17,8 @@ def imageDashboard(request, group_name, image_id): imgcol = collection.File.find({'mime_type': {'$regex': 'image'}}) template = "ndf/ImageDashboard.html" - url = request.get_full_path() - variable = RequestContext(request, {'imageCollection':imgcol, 'pageUrl':url, 'stId':image_id}) + already_uploaded=request.GET.get("already_uploaded","") + variable = RequestContext(request, {'imageCollection':imgcol,'already_uploaded':already_uploaded }) return render_to_response(template, variable) def getImageThumbnail(request, group_name, _id): From 9587b4517d3accdaab878067b2a62484a67bb6d5 Mon Sep 17 00:00:00 2001 From: krishna Date: Tue, 24 Dec 2013 16:47:02 +0530 Subject: [PATCH 2/3] delete implemented for file,image,video --- .../ndf/templates/ndf/ImageDashboard.html | 7 ++-- .../gnowsys_ndf/ndf/templates/ndf/file.html | 5 +++ .../ndf/templates/ndf/videoDashboard.html | 16 ++++---- gnowsys-ndf/gnowsys_ndf/ndf/views/file.py | 39 ++++--------------- .../gnowsys_ndf/ndf/views/imageDashboard.py | 2 +- .../gnowsys_ndf/ndf/views/videoDashboard.py | 4 +- 6 files changed, 28 insertions(+), 45 deletions(-) diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/ImageDashboard.html b/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/ImageDashboard.html index 2ca85fb135..b2ffe25529 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/ImageDashboard.html +++ b/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/ImageDashboard.html @@ -15,7 +15,6 @@ {% endif %} {% get_group_name request.path as group_name %} -
@@ -26,11 +25,9 @@
- {% if user.is_authenticated %} Upload - {% endif %}
@@ -44,6 +41,10 @@
{{image.name}}
+ {% get_user_object image.created_by as user_obj %} + {% ifequal user_obj.username user.username %} + Delete + {% endifequal %} {% if not forloop.last and forloop.counter|divisibleby:"6" %} diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/file.html b/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/file.html index e7886acf3c..350e867b05 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/file.html +++ b/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/file.html @@ -68,9 +68,14 @@

Delete

Download
+
+ {% get_user_object file.created_by as user_obj %} + {% ifequal user_obj.username user.username %} Delete + {% endifequal %}
+ {% endfor %} diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/videoDashboard.html b/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/videoDashboard.html index 3fde85241f..b6878f0552 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/videoDashboard.html +++ b/gnowsys-ndf/gnowsys_ndf/ndf/templates/ndf/videoDashboard.html @@ -3,11 +3,11 @@ {% block title %} video Gallery {% endblock %} {% block body_content %} -{% if alreadyUploadedFiles %} +{% if already_uploaded %}
Listed below files are already uploaded please choose different files
    - {% for each in alreadyUploadedFiles %} + {% for each in already_uploaded %}
  • {{each}}
  • {% endfor %}
@@ -26,13 +26,9 @@
-
{% csrf_token %} - - - - + + Upload -
@@ -46,6 +42,10 @@
{{video.name}}
+ {% get_user_object video.created_by as user_obj %} + {% ifequal user_obj.username user.username %} + Delete + {% endifequal %} {% if not forloop.last and forloop.counter|divisibleby:"6" %} diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/views/file.py b/gnowsys-ndf/gnowsys_ndf/ndf/views/file.py index 1d2e05a704..2edaa4348a 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/views/file.py +++ b/gnowsys-ndf/gnowsys_ndf/ndf/views/file.py @@ -24,7 +24,7 @@ import os import subprocess import ox - +import urllib #from string import maketrans @@ -55,9 +55,7 @@ def file(request, group_name, file_id): title = gst_file.name filecollection=db[File.collection_name] files=filecollection.File.find({'_type': u'File'}) - already_uploaded=request.GET.get("already_uploaded","") - already_uploaded=list(already_uploaded) - print already_uploaded + already_uploaded=request.GET.getlist('var',"") return render_to_response("ndf/file.html", {'title': title,'files':files,'already_uploaded':already_uploaded}, context_instance=RequestContext(request)) else: return HttpResponseRedirect(reverse('homepage')) @@ -94,33 +92,15 @@ def submitDoc(request,group_name): i=i+1 if f: alreadyUploadedFiles.append(f) -<<<<<<< HEAD #url = reverse(page_url, kwargs={'alreadyUploadedFiles':alreadyUploadedFiles}) - return HttpResponseRedirect(page_url+'?already_uploaded='+str(alreadyUploadedFiles)) + str1='' + for each in alreadyUploadedFiles: + str1=str1+'var='+each+'&' + print str1 + return HttpResponseRedirect(page_url+'?'+str1) else: return HttpResponseRedirect(reverse('homepage')) -======= - if 'image' in mainPageUrl: - collection=db[File.collection_name] - imgcol=collection.File.find({'mime_type': {'$regex': 'image'}}) - variable=RequestContext(request,{'alreadyUploadedFiles':alreadyUploadedFiles,'imageCollection':imgcol}) - template="ndf/ImageDashboard.html" - return render_to_response(template,variable) - if 'video' in mainPageUrl: - collection=db[File.collection_name] - videocol=collection.File.find({'mime_type': {'$regex': 'video'}}) - variable=RequestContext(request,{'alreadyUploadedFiles':alreadyUploadedFiles,'videoCollection':videocol}) - template="ndf/videoDashboard.html" - return render_to_response(template,variable) - else: - return HttpResponseRedirect("/"+group_name+"/file"+"/"+gst_file._id.__str__()) - # filecollection=get_database()[File.collection_name] - # files=filecollection.File.find('_type': u'File') - # variable=RequestContext(request,{'alreadyUploadedFiles':alreadyUploadedFiles,'filecollection':files}) - # template='ndf/DocumentList.html' - # return render_to_response(template,variable) ->>>>>>> 81cd317afc582df478cf25eab36f3327d0451a63 def save_file(files,title,userid,memberOf,stId): fcol=db[File.collection_name] @@ -244,15 +224,12 @@ def convertVideo(files,userid): secs = duration videoDuration = "" durationTime = str(str(hrs)+":"+str(mins)+":"+str(secs)) # calculating Time duration of video in hrs,mins,secs -<<<<<<< HEAD - proc = subprocess.Popen(['ffmpeg', '-i', str("/tmp/"+userid+"/"+fileVideoName+"/"+fileVideoName), '-ss', "00:00:30", "-s", "128*128", "-f", "image2", str("/tmp/"+userid+"/"+fileVideoName+"/"+initialFileName+".png")]) # creating thumbnail of video using ffmpeg -======= + if duration > 30 : videoDuration = "00:00:30" else : videoDuration = "00:00:00" proc = subprocess.Popen(['ffmpeg', '-i', str("/tmp/"+userid+"/"+fileVideoName+"/"+fileVideoName), '-ss', videoDuration, "-s", "170*128", "-vframes", "1", str("/tmp/"+userid+"/"+fileVideoName+"/"+initialFileName+".png")]) # creating thumbnail of video using ffmpeg ->>>>>>> 81cd317afc582df478cf25eab36f3327d0451a63 proc.wait() background = Image.open("/tmp/"+userid+"/"+fileVideoName+"/"+initialFileName+".png") fore = Image.open(MEDIA_ROOT+"ndf/images/poster.jpg") diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/views/imageDashboard.py b/gnowsys-ndf/gnowsys_ndf/ndf/views/imageDashboard.py index 95745ba2a1..15d571b821 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/views/imageDashboard.py +++ b/gnowsys-ndf/gnowsys_ndf/ndf/views/imageDashboard.py @@ -17,7 +17,7 @@ def imageDashboard(request, group_name, image_id): imgcol = collection.File.find({'mime_type': {'$regex': 'image'}}) template = "ndf/ImageDashboard.html" - already_uploaded=request.GET.get("already_uploaded","") + already_uploaded=request.GET.getlist('var',"") variable = RequestContext(request, {'imageCollection':imgcol,'already_uploaded':already_uploaded }) return render_to_response(template, variable) diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/views/videoDashboard.py b/gnowsys-ndf/gnowsys_ndf/ndf/views/videoDashboard.py index c37fc1886f..d932d1cee2 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/views/videoDashboard.py +++ b/gnowsys-ndf/gnowsys_ndf/ndf/views/videoDashboard.py @@ -17,8 +17,8 @@ def videoDashboard(request, group_name, video_id): videocol = collection.File.find({'mime_type': {'$regex': 'video'}}) template = "ndf/videoDashboard.html" - url = request.get_full_path() - variable = RequestContext(request, {'videoCollection':videocol, 'pageUrl':url, 'stId':video_id}) + already_uploaded=request.GET.getlist('var',"") + variable = RequestContext(request, {'videoCollection':videocol,'already_uploaded':already_uploaded}) return render_to_response(template, variable) def getvideoThumbnail(request, group_name, _id): From 7055481108a214aa90392f48edf6464192e07006 Mon Sep 17 00:00:00 2001 From: krishna Date: Fri, 27 Dec 2013 15:00:37 +0530 Subject: [PATCH 3/3] Implemented delete remove bug for already uploaded files --- gnowsys-ndf/gnowsys_ndf/ndf/views/file.py | 195 +++++++++++----------- 1 file changed, 95 insertions(+), 100 deletions(-) diff --git a/gnowsys-ndf/gnowsys_ndf/ndf/views/file.py b/gnowsys-ndf/gnowsys_ndf/ndf/views/file.py index 2edaa4348a..52bd464e49 100644 --- a/gnowsys-ndf/gnowsys_ndf/ndf/views/file.py +++ b/gnowsys-ndf/gnowsys_ndf/ndf/views/file.py @@ -24,7 +24,6 @@ import os import subprocess import ox -import urllib #from string import maketrans @@ -37,8 +36,8 @@ ####################################################################################################################################### db = get_database() -gst_collection = db[GSystemType.collection_name] -gst_file = gst_collection.GSystemType.one({'name': GAPPS[1]}) +GST_COLLECTION = db[GSystemType.collection_name] +GST_FILE = GST_COLLECTION.GSystemType.one({'name': GAPPS[1]}) ####################################################################################################################################### @@ -51,124 +50,120 @@ def file(request, group_name, file_id): * Renders a list of all 'Files' available within the database. """ - if gst_file._id == ObjectId(file_id): - title = gst_file.name - filecollection=db[File.collection_name] - files=filecollection.File.find({'_type': u'File'}) - already_uploaded=request.GET.getlist('var',"") - return render_to_response("ndf/file.html", {'title': title,'files':files,'already_uploaded':already_uploaded}, context_instance=RequestContext(request)) + if GST_FILE._id == ObjectId(file_id): + title = GST_FILE.name + filecollection = db[File.collection_name] + files = filecollection.File.find({'_type': u'File'}) + already_uploaded = request.GET.getlist('var', "") + return render_to_response("ndf/file.html", {'title': title, 'files':files, 'already_uploaded':already_uploaded}, context_instance = RequestContext(request)) else: return HttpResponseRedirect(reverse('homepage')) @login_required -def uploadDoc(request,group_name): - if request.method=="GET": - page_url=request.GET.get("next","") - template="ndf/UploadDoc.html" +def uploadDoc(request, group_name): + if request.method == "GET": + page_url = request.GET.get("next", "") + template = "ndf/UploadDoc.html" if page_url: variable = RequestContext(request, {'page_url': page_url}) else: - variable=RequestContext(request,{}) - return render_to_response(template,variable) + variable = RequestContext(request, {}) + return render_to_response(template, variable) @login_required -def submitDoc(request,group_name): - alreadyUploadedFiles=[] - if request.method=="POST": - mtitle = request.POST.get("docTitle","") - userid = request.POST.get("user","") - page_url = request.POST.get("page_url","") - print "url",page_url - #memberOf = request.POST.get("memberOf","") - i=1 - for index,each in enumerate(request.FILES.getlist("doc[]","")): - if index==0: - f=save_file(each,mtitle,userid,group_name,gst_file._id.__str__()) +def submitDoc(request, group_name): + alreadyUploadedFiles = [] + if request.method == "POST": + mtitle = request.POST.get("docTitle", "") + userid = request.POST.get("user", "") + page_url = request.POST.get("page_url", "") + print "url", page_url + #memberOf = request.POST.get("memberOf", "") + i = 1 + for index, each in enumerate(request.FILES.getlist("doc[]", "")): + if mtitle: + if index == 0: + f = save_file(each, mtitle, userid, group_name, GST_FILE._id.__str__()) + title = mtitle + "_" + str(i) #increament title + f = save_file(each, title, userid, group_name, GST_FILE._id.__str__()) + i = i + 1 else: - title=mtitle+"_"+str(i) - f=save_file(each,title,userid,group_name,gst_file._id.__str__()) - i=i+1 + title = each.name + f = save_file(each, title, userid, group_name, GST_FILE._id.__str__()) if f: alreadyUploadedFiles.append(f) - #url = reverse(page_url, kwargs={'alreadyUploadedFiles':alreadyUploadedFiles}) - str1='' + title = mtitle + str1 = '' for each in alreadyUploadedFiles: - str1=str1+'var='+each+'&' + str1 = str1 + 'var=' + each + '&' print str1 return HttpResponseRedirect(page_url+'?'+str1) else: return HttpResponseRedirect(reverse('homepage')) - -def save_file(files,title,userid,memberOf,stId): - fcol=db[File.collection_name] - fileobj=fcol.File() - #gst=gst_collection.GSystemType.one({"_id":ObjectId(stId)}) - filemd5= hashlib.md5(files.read()).hexdigest() +def save_file(files, title, userid, memberOf, stId): + fcol = db[File.collection_name] + fileobj = fcol.File() + #gst=GST_COLLECTION.GSystemType.one({"_id":ObjectId(stId)}) + filemd5 = hashlib.md5(files.read()).hexdigest() files.seek(0) - size,unit=getFileSize(files) - size={'size':round(size,2),'unit':unicode(unit)} + size, unit = getFileSize(files) + size = {'size':round(size, 2), 'unit':unicode(unit)} if fileobj.fs.files.exists({"md5":filemd5}): return files.name else: try: files.seek(0) - filetype=magic.from_buffer(files.read(100000),mime='true') #Gusing filetype by python-magic + filetype = magic.from_buffer(files.read(100000), mime = 'true') #Gusing filetype by python-magic filetype1 = mimetypes.guess_type(files.name)[0] if filetype1: filetype1 = filetype1 else : filetype1 = "" - filename=files.name - - if title: - fileobj.name=unicode(title) - else: - fileobj.name=unicode(filename) - fileobj.created_by=int(userid) - fileobj.file_size=size + filename = files.name + fileobj.name = unicode(title) + fileobj.created_by = int(userid) + fileobj.file_size = size #fileobj.member_of=unicode(memberOf) #shuold be group - fileobj.mime_type=filetype + fileobj.mime_type = filetype if stId: fileobj.gsystem_type.append(ObjectId(stId)) fileobj.save() - - files.seek(0) #moving files cursor to start #this code is for storing Document in gridfs - objectid=fileobj.fs.files.put(files.read(),filename=filename,content_type=filetype) + objectid = fileobj.fs.files.put(files.read(), filename=filename, content_type=filetype) fileobj.fs_file_ids.append(objectid) fileobj.save() # code for converting video into webm and converted video assigning to varible files if 'video' in filetype or 'video' in filetype1 or filename.endswith('.webm') == True: - fileobj.mime_type="video" + fileobj.mime_type = "video" fileobj.save() - webmfiles,filetype,thumbnailvideo = convertVideo(files,userid) + webmfiles, filetype, thumbnailvideo = convertVideo(files, userid) #storing thumbnail of video with duration in saved object - tobjectid=fileobj.fs.files.put(thumbnailvideo.read(),filename=filename+"-thumbnail",content_type="thumbnail-image") # saving thumbnail in grid fs + tobjectid = fileobj.fs.files.put(thumbnailvideo.read(), filename=filename+"-thumbnail", content_type="thumbnail-image") fileobj.fs_file_ids.append(tobjectid) # saving thumbnail's id into file object fileobj.save() if filename.endswith('.webm') == False: - tobjectid=fileobj.fs.files.put(webmfiles.read(),filename=filename,content_type=filetype) # saving webm video in grid fs + tobjectid = fileobj.fs.files.put(webmfiles.read(), filename=filename, content_type=filetype) fileobj.fs_file_ids.append(tobjectid) # saving webm video id into file object fileobj.save() #storing thumbnail of image in saved object if 'image' in filetype: - thumbnailimg=convert_image_thumbnail(files) - tobjectid=fileobj.fs.files.put(thumbnailimg,filename=filename+"-thumbnail",content_type=filetype) + thumbnailimg = convert_image_thumbnail(files) + tobjectid = fileobj.fs.files.put(thumbnailimg, filename=filename+"-thumbnail", content_type=filetype) fileobj.fs_file_ids.append(tobjectid) fileobj.save() except Exception as e: - print "Not Uploaded files:",files.name,"Execption:",e + print "Some Exception:", files.name, "Execption:", e def getFileSize(File): - File.seek(0,os.SEEK_END) - num=File.tell() - for x in ['bytes','KB','MB','GB','TB']: + File.seek(0, os.SEEK_END) + num = File.tell() + for x in ['bytes', 'KB', 'MB', 'GB', 'TB']: if num < 1024.0: return (num, x) num /= 1024.0 @@ -176,26 +171,26 @@ def getFileSize(File): def convert_image_thumbnail(files): files.seek(0) - thumb_io=StringIO() - size=128,128 - img=Image.open(StringIO(files.read())) - img.thumbnail(size,Image.ANTIALIAS) - img.save(thumb_io,"JPEG") + thumb_io = StringIO() + size = 128, 128 + img = Image.open(StringIO(files.read())) + img.thumbnail(size, Image.ANTIALIAS) + img.save(thumb_io, "JPEG") thumb_io.seek(0) return thumb_io -def convertVideo(files,userid): +def convertVideo(files, userid): """ converting video into webm format, if video already in webm format ,then pass to create thumbnails """ fileVideoName = files._get_name() z = "" for each1 in fileVideoName: - if each1==" ": - z=z+'-' + if each1 == " ": + z = z+'-' else: - z=z+each1 + z = z+each1 fileVideoName = z os.system("mkdir -p "+ "/tmp"+"/"+str(userid)+"/"+fileVideoName+"/") fd = open('%s/%s/%s/%s' % (str("/tmp"), str(userid),str(fileVideoName), str(fileVideoName)), 'wb') @@ -213,7 +208,7 @@ def convertVideo(files,userid): oxData = ox.avinfo("/tmp/"+userid+"/"+fileVideoName+"/"+fileVideoName) duration = oxData['duration'] # fetching duration of video by python ox duration = int(duration) - secs,mins,hrs = 00,00,00 + secs, mins, hrs = 00, 00, 00 if duration > 60 : secs = duration % 60 mins = duration / 60 @@ -233,49 +228,49 @@ def convertVideo(files,userid): proc.wait() background = Image.open("/tmp/"+userid+"/"+fileVideoName+"/"+initialFileName+".png") fore = Image.open(MEDIA_ROOT+"ndf/images/poster.jpg") - background.paste(fore,(120,100)) + background.paste(fore, (120, 100)) draw = ImageDraw.Draw(background) - draw.text((120, 100),durationTime,(255,255,255)) # drawing duration time on thumbnail image + draw.text((120, 100), durationTime, (255, 255, 255)) # drawing duration time on thumbnail image background.save("/tmp/"+userid+"/"+fileVideoName+"/"+initialFileName+"Time.png") thumbnailvideo = open("/tmp/"+userid+"/"+fileVideoName+"/"+initialFileName+"Time.png") - return files,filetype,thumbnailvideo + return files, filetype, thumbnailvideo -def GetDoc(request,group_name): - filecollection=get_database()[File.collection_name] - files=filecollection.File.find({'_type': u'File'}) +def GetDoc(request, group_name): + filecollection = get_database()[File.collection_name] + files = filecollection.File.find({'_type': u'File'}) #return files - template="ndf/DocumentList.html" - variable=RequestContext(request,{'filecollection':files}) - return render_to_response(template,variable) + template = "ndf/DocumentList.html" + variable = RequestContext(request, {'filecollection':files}) + return render_to_response(template, variable) -def readDoc(request,_id,group_name): - filecollection=get_database()[File.collection_name] - fileobj=filecollection.File.one({"_id": ObjectId(_id)}) - fl=fileobj.fs.files.get(ObjectId(fileobj.fs_file_ids[0])) - return HttpResponse(fl.read(),content_type=fl.content_type) +def readDoc(request, _id, group_name): + filecollection = get_database()[File.collection_name] + fileobj = filecollection.File.one({"_id": ObjectId(_id)}) + grid_fs_obj = fileobj.fs.files.get(ObjectId(fileobj.fs_file_ids[0])) + return HttpResponse(grid_fs_obj.read(), content_type = grid_fs_obj.content_type) def file_search(request, group_name): - if request.method=="GET": - keyword=request.GET.get("search","") - files=db[File.collection_name] - file_search=files.File.find({'$or':[{'name':{'$regex': keyword}},{'tags':{'$regex':keyword}}]}) #search result from file - template="ndf/file_search.html" - variable=RequestContext(request,{'file_collection':file_search,'view_name':'file_search'}) - return render_to_response(template,variable) + if request.method == "GET": + keyword = request.GET.get("search", "") + files = db[File.collection_name] + file_search = files.File.find({'$or':[{'name':{'$regex': keyword}}, {'tags':{'$regex':keyword}}]}) #search result from file + template = "ndf/file_search.html" + variable = RequestContext(request, {'file_collection':file_search, 'view_name':'file_search'}) + return render_to_response(template, variable) -def delete_file(request,group_name,_id): +def delete_file(request, group_name, _id): """Delete file and its data """ file_collection = db[File.collection_name] - pageurl=request.GET.get("next","") - print "testurl",pageurl + pageurl = request.GET.get("next", "") + print "testurl", pageurl try: - cur=file_collection.File.one({'_id':ObjectId(_id)}) + cur = file_collection.File.one({'_id':ObjectId(_id)}) if cur.fs_file_ids: for each in cur.fs_file_ids: cur.fs.files.delete(each) cur.delete() except Exception as e: - print "Exception:",e + print "Exception:", e return HttpResponseRedirect(pageurl)