本文整理汇总了Python中documents.models.Document类的典型用法代码示例。如果您正苦于以下问题:Python Document类的具体用法?Python Document怎么用?Python Document使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Document类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_or_create_document
def update_or_create_document(yaml_obj):
"""
Submit an object read from our YAML files and it will update it in the
database, creating it if it doesn't already exist.
Returns the database object, and a boolean that is true if a new object
was created.
"""
# Check if the table already exists in the datastore
obj = Document.get_by_key_name(yaml_obj.get('slug'))
# Update the obj if it exists
if obj:
# Loop through the keys and update the object one by one.
for key in yaml_obj.keys():
# With some special casing for projects...
if key == 'project_slug':
proj = Project.get_by_key_name(yaml_obj.get('project_slug'))
obj.project = proj
# ...and for tags.
elif key == 'tags':
obj.tags = get_tag_keys(yaml_obj.get("tags"))
else:
setattr(obj, key, yaml_obj.get(key))
# Save it out
obj.put()
created = False
# Create it if it doesn't
else:
# If it has tags....
if yaml_obj.has_key('tags'):
# Convert to database keys
tags = get_tag_keys(yaml_obj.pop("tags"))
# Load the data
obj = Document(key_name=yaml_obj.get('slug'), **yaml_obj)
# Set the tags
obj.tags = tags
# Otherwise....
else:
# Update the basic values
obj = Document(key_name=yaml_obj.get('slug'), **yaml_obj)
# And clear out the tag data
obj.tags = []
obj.similar_documents = []
# Connected it to a project, if it exists
if yaml_obj.has_key('project_slug'):
proj = Project.get_by_key_name(yaml_obj.get('project_slug'))
obj.project = proj
# Save it out
obj.put()
created = True
# Update the similarity lists of documents with the same tags
taskqueue.add(
url='/_/document/update-similar/',
params=dict(key=obj.key()),
method='GET'
)
# Pass it out
return obj, created
示例2: save_document
def save_document(provider, title, href, images, embed, desc, duration, views, categories):
from documents.models import Document, Category, Image
try:
doc = Document.objects.get(href=href)
except Document.DoesNotExist:
doc = Document()
doc.provider = provider
doc.title = title
doc.href = href
doc.embed = embed
doc.desc = desc
doc.duration = duration
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
doc.views = locale.atoi(views)
doc.save()
if doc.images.count() == 0:
for image_url in images:
response = requests.get(image_url)
if response.status_code == 200:
image = Image()
image.document = doc
image.url = image_url
image.save()
for category in categories:
cat = Category.objects.get_or_create(title=category)
doc.categories.add(cat[0])
示例3: split_doc
def split_doc(request):
if request.is_ajax():
results = {}
cut = int(request.GET["modal_split_cut"][0])
doc = Document.objects.get(pk=int(request.GET["modal_split_doc_id"]))
doc.name = request.GET["modal_split_name"]
new_doc = Document(
name=request.GET["modal_split_new_name"],
owner=request.user,
refer_category=doc.refer_category,
complete=True,
)
pages = doc.pages.all()
new_doc.save()
i = 0
for p in pages:
if i >= cut:
new_doc.pages.add(p)
new_doc.size += p.get_size()
p.refer_document = new_doc
p.save()
doc.size -= p.get_size()
doc.pages.remove(p)
i += 1
doc.refer_category.documents.add(new_doc)
doc.save()
new_doc.save()
results["valid"] = True
return HttpResponse(json.dumps(results))
示例4: DocumentSearchTestCase
class DocumentSearchTestCase(unittest.TestCase):
def setUp(self):
# Start the OCR queue
self.default_queue = DocumentQueue.objects.get(name='default')
def test_do_document_ocr(self):
self.document_type = DocumentType(name='test doc type')
self.document_type.save()
self.document = Document(
document_type=self.document_type,
description='description',
)
self.document.save()
file_object = open(os.path.join(settings.SITE_ROOT, 'contrib', 'sample_documents', 'title_page.png'))
new_version = self.document.new_version(file=File(file_object, name='title_page.png'))
file_object.close()
self.failUnlessEqual(self.default_queue.queuedocument_set.count(), 1)
do_document_ocr(self.default_queue.queuedocument_set.all()[0])
self.assertTrue(u'Mayan EDMS' in self.document.pages.all()[0].content)
def tearDown(self):
self.document.delete()
self.document_type.delete()
示例5: report
def report(request, lot_number):
report = Report.objects.get(lot_number=lot_number)
audit_log = target_stream(report)
orders = OrderLineItem.objects.filter(report=report)
newdoc = NewDocumentForm(None)
if 'document_button' in request.POST:
newdoc = NewDocumentForm(request.POST, request.FILES)
if newdoc.is_valid():
doc = Document(type=newdoc.cleaned_data['type'],
file=request.FILES['file'], created_by=request.user)
doc.save()
action.send(request.user, verb="created document", action_object=doc)
rd = ReportDocument(report=report, document=doc, primary_document=newdoc.cleaned_data['primary'], created_by=request.user, internal_cert=newdoc.cleaned_data['internal'])
rd.save()
action.send(request.user, verb="attached document", action_object=rd, target=report)
report.save()
messages.success(request, 'Document upload successful.')
return HttpResponseRedirect(reverse('reports.views.report',
args=[report.lot_number]))
return render_to_response('reports/report.html',
{
'report': report,
'new_document_form': newdoc,
'orders': orders,
'audit_log': audit_log,
},
context_instance=RequestContext(request))
示例6: test_create_document_object
def test_create_document_object(self):
doc = Document(title="foo", document_type=DocumentType.objects.all()[0],
notes="bonk")
doc.file.save(name="whee", content=ContentFile("wee willy wonka"))
doc.programs = Program.objects.all()[:2]
doc.authors = [self.john.id]
self.assertItemsEqual([doc], Document.objects.all())
示例7: add_documents
def add_documents(request, category_id):
if request.is_ajax():
files = request.GET.getlist('files', False)
cat = Category.objects.get(id=category_id)
l_doc = []
l_pdf = []
cmds = []
paths = []
for f in list(files):
mime = MimeTypes()
path = os.path.join(settings.MEDIA_ROOT, settings.UPLOAD_DIR, f)
m = mime.guess_type(path)[0]
d = Document(name=f.encode('ascii', 'ignore'), owner=request.user, refer_category=cat)
d.save()
cat.add_doc(d)
if m == 'application/pdf':
l_pdf.append(([cat], path, f, [d]))
elif m in ['image/png', 'image/jpeg', 'image/bmp']:
im = Image.open(path)
w, h = im.size
new_filename = str(d.id) + '_' + f
new_path = os.path.join(cat.get_absolute_path(), new_filename)
shutil.copy2(path, new_path)
d.add_page(d.get_npages() + 1, new_filename, w, h)
for fu in FileUpload.objects.all():
if fu.file.path == path:
fu.delete()
d.complete = True
d.save()
remove_fileupload([path])
elif m in ['application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']:
p = re.compile(r'.[Dd][Oo][Cc][xX]?$')
new_f = p.sub('.pdf', f)
new_path = path.replace(f, new_f)
cmd = 'soffice --headless --convert-to pdf %s --outdir %s/upload' % (path, settings.MEDIA_ROOT)
cmds.append(cmd)
paths.append(path)
l_doc.append(([cat], new_path, new_f, [d]))
elif m in ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']:
p = re.compile(r'.[Xx][Ll][Ss][xX]?$')
new_f = p.sub('.pdf', f)
new_path = path.replace(f, new_f)
cmd = 'soffice --headless --convert-to pdf %s --outdir %s/upload' % (path, settings.MEDIA_ROOT)
cmds.append(cmd)
paths.append(path)
l_doc.append(([cat], new_path, new_f, [d]))
else:
print 'ERREUR FORMAT FICHIER'
if len(l_doc):
thread1 = Timer(0, manage_convert_doc_to_pdf, (cmds, paths, l_doc,))
thread1.start()
if len(l_pdf):
thread = Timer(0, manage_convert_pdf_to_jpg, (l_pdf,))
thread.start()
results = {'doc_list': [d.as_json() for d in cat.get_docs()], 'n': cat.count_docs()}
return HttpResponse(json.dumps(results))
示例8: DocumentTestCase
class DocumentTestCase(TestCase):
def setUp(self):
self.document_type = DocumentType(name='test doc type')
self.document_type.save()
self.document = Document(
document_type=self.document_type,
description='description',
)
self.document.save()
with open(TEST_DOCUMENT_PATH) as file_object:
self.document.new_version(file=File(file_object, name='mayan_11_1.pdf'))
with open(TEST_KEY_FILE) as file_object:
gpg.import_key(file_object.read())
def test_document_no_signature(self):
self.failUnlessEqual(DocumentVersionSignature.objects.has_detached_signature(self.document), False)
def test_new_document_version_signed(self):
with open(TEST_SIGNED_DOCUMENT_PATH) as file_object:
new_version_data = {
'comment': 'test comment 1',
'version_update': VERSION_UPDATE_MAJOR,
'release_level': RELEASE_LEVEL_FINAL,
'serial': 0,
}
self.document.new_version(file=File(file_object, name='mayan_11_1.pdf.gpg'), **new_version_data)
self.failUnlessEqual(DocumentVersionSignature.objects.has_detached_signature(self.document), False)
self.failUnlessEqual(DocumentVersionSignature.objects.verify_signature(self.document).status, SIGNATURE_STATE_VALID)
def test_detached_signatures(self):
new_version_data = {
'comment': 'test comment 2',
'version_update': VERSION_UPDATE_MAJOR,
'release_level': RELEASE_LEVEL_FINAL,
'serial': 0,
}
with open(TEST_DOCUMENT_PATH) as file_object:
self.document.new_version(file=File(file_object), **new_version_data)
# GPGVerificationError
self.failUnlessEqual(DocumentVersionSignature.objects.verify_signature(self.document), None)
with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object:
DocumentVersionSignature.objects.add_detached_signature(self.document, File(file_object))
self.failUnlessEqual(DocumentVersionSignature.objects.has_detached_signature(self.document), True)
self.failUnlessEqual(DocumentVersionSignature.objects.verify_signature(self.document).status, SIGNATURE_STATE_VALID)
def tearDown(self):
self.document.delete()
self.document_type.delete()
示例9: __upload_core
def __upload_core(request):
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
u = request.user
doc = Document(owner=u)
file = request.FILES['file']
doc.filename = request.FILES['file'].name
doc.save()
doc.file.save('file_' + str(doc.id), file)
return doc
else:
return Exception()
示例10: document_clear_image_cache
def document_clear_image_cache(request):
check_permissions(request.user, [PERMISSION_DOCUMENT_TOOLS])
previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/')))
if request.method == 'POST':
try:
Document.clear_image_cache()
messages.success(request, _(u'Document image cache cleared successfully'))
except Exception, msg:
messages.error(request, _(u'Error clearing document image cache; %s') % msg)
return HttpResponseRedirect(previous)
示例11: FolderTestCase
class FolderTestCase(unittest.TestCase):
def setUp(self):
self.document_type = DocumentType(name='test doc type')
self.document_type.save()
self.document = Document(
document_type=self.document_type,
description='description',
)
self.document.save()
file_object = open(os.path.join(settings.SITE_ROOT, 'contrib', 'sample_documents', 'mayan_11_1.pdf'))
new_version = self.document.new_version(file=File(file_object, name='mayan_11_1.pdf'))
file_object.close()
def test_creation_of_folder(self):
user = User.objects.all()[0]
folder = Folder.objects.create(title='test', user=user)
self.assertEqual(Folder.objects.all().count(), 1)
self.assertEqual(list(Folder.objects.all()), [folder])
folder.delete()
def test_addition_of_documents(self):
user = User.objects.all()[0]
folder = Folder.objects.create(title='test', user=user)
folder.add_document(self.document)
self.assertEqual(folder.documents.count(), 1)
self.assertEqual(list(folder.documents), [self.document])
folder.delete()
def test_addition_and_deletion_of_documents(self):
user = User.objects.all()[0]
folder = Folder.objects.create(title='test', user=user)
folder.add_document(self.document)
self.assertEqual(folder.documents.count(), 1)
self.assertEqual(list(folder.documents), [self.document])
folder.remove_document(self.document)
self.assertEqual(folder.documents.count(), 0)
self.assertEqual(list(folder.documents), [])
folder.delete()
def tearDown(self):
self.document.delete()
self.document_type.delete()
示例12: DocumentTestCase
class DocumentTestCase(TestCase):
def setUp(self):
self.document_type = DocumentType(name="test doc type")
self.document_type.save()
self.document = Document(document_type=self.document_type, description="description")
self.document.save()
with open(TEST_DOCUMENT_PATH) as file_object:
self.document.new_version(file_object=File(file_object, name="mayan_11_1.pdf"))
with open(TEST_KEY_FILE) as file_object:
gpg.import_key(file_object.read())
def test_document_no_signature(self):
self.failUnlessEqual(DocumentVersionSignature.objects.has_detached_signature(self.document), False)
def test_new_document_version_signed(self):
with open(TEST_SIGNED_DOCUMENT_PATH) as file_object:
new_version_data = {"comment": "test comment 1"}
self.document.new_version(file_object=File(file_object, name="mayan_11_1.pdf.gpg"), **new_version_data)
self.failUnlessEqual(DocumentVersionSignature.objects.has_detached_signature(self.document), False)
self.failUnlessEqual(
DocumentVersionSignature.objects.verify_signature(self.document).status, SIGNATURE_STATE_VALID
)
def test_detached_signatures(self):
new_version_data = {"comment": "test comment 2"}
with open(TEST_DOCUMENT_PATH) as file_object:
self.document.new_version(file_object=File(file_object), **new_version_data)
# GPGVerificationError
self.failUnlessEqual(DocumentVersionSignature.objects.verify_signature(self.document), None)
with open(TEST_SIGNATURE_FILE_PATH, "rb") as file_object:
DocumentVersionSignature.objects.add_detached_signature(self.document, File(file_object))
self.failUnlessEqual(DocumentVersionSignature.objects.has_detached_signature(self.document), True)
self.failUnlessEqual(
DocumentVersionSignature.objects.verify_signature(self.document).status, SIGNATURE_STATE_VALID
)
def tearDown(self):
self.document.delete()
self.document_type.delete()
示例13: form_valid
def form_valid(self, form):
new_homework=Homework(
entered_by=self.request.user,
entered_on=datetime.today(),
)
new_homework.save()
new_details=form.save(commit=False)
new_details.deleted=False
new_details.hwk=new_homework
if self.request.user.has_perm('classlists.is_kksastaff'):
#creating multiple copies of the same hwk details record, one for each class
for k in form.cleaned_data['klass']:
new_details.pk=None
new_details.klass=k
new_details.save()
if form.cleaned_data['link']:
new_link=Link(
link=form.cleaned_data['link'],
description=form.cleaned_data['link_description'],
homework=new_homework,
subject=new_details.subject,
)
new_link.save()
for k in form.cleaned_data['klass']:
new_link.klass.add(k)
if form.cleaned_data['attached_file']:
new_document=Document(
attached_file=form.cleaned_data['attached_file'],
filename=form.cleaned_data['attached_file'].name,
description=form.cleaned_data['document_description'],
homework=new_homework,
subject=new_details.subject,
)
new_document.save()
for k in form.cleaned_data['klass']:
new_document.klass.add(k)
else:
new_details.klass=Klass.objects.get(klass_name=self.kwargs['class_url'])
new_details.save()
return HttpResponseRedirect(reverse('homework_view', args=(self.kwargs['class_url'],),))
示例14: upload_single_file
def upload_single_file(self, file_object, filename=None, use_file_name=False, document_type=None, metadata_dict_list=None, user=None, document=None, new_version_data=None):
if not document:
document = Document()
if document_type:
document.document_type = document_type
document.save()
if metadata_dict_list:
save_metadata_list(metadata_dict_list, document, create=True)
warnings = update_indexes(document)
if user:
document.add_as_recent_document_for_user(user)
create_history(HISTORY_DOCUMENT_CREATED, document, {'user': user})
else:
create_history(HISTORY_DOCUMENT_CREATED, document)
else:
if use_file_name:
filename = None
else:
filename = filename if filename else document.latest_version.filename
if not new_version_data:
new_version_data = {}
new_version = document.new_version(file=file_object, **new_version_data)
if filename:
new_version.filename = filename
new_version.save()
transformations, errors = self.get_transformation_list()
new_version.apply_default_transformations(transformations)
示例15: setUp
def setUp(self):
self.document_type = DocumentType(name="test doc type")
self.document_type.save()
self.document = Document(document_type=self.document_type, description="description")
self.document.save()
with open(TEST_DOCUMENT_PATH) as file_object:
self.document.new_version(file_object=File(file_object))