def preprocess_collection_listings(resources): from oer.models import Link, Attachment from curriculum.models import Reference from django.contrib.contenttypes.models import ContentType link_content_type = ContentType.objects.get_for_model(Link) attachment_content_type = ContentType.objects.get_for_model(Attachment) reference_content_type = ContentType.objects.get_for_model(Reference) import oer.ResourceUtilities as ru from os.path import splitext for resource in resources: if resource.revision.content_type == link_content_type: (hostname, url_data) = ru.get_url_hostname(resource.revision.content.url) if ('docs' in hostname and 'google' in hostname) and ( 'document' in url_data.path): resource.open_url = resource.revision.content.url resource.type = 'gdoc' if ('docs' in hostname and 'google' in hostname) and ( 'presentation' in url_data.path): resource.type = 'gpres' elif 'dropbox' in hostname and 'sh' in url_data.path: resource.open_url = resource.revision.content.url resource.type = 'dropbox' elif ('drive' in hostname and 'google' in hostname) and ( 'folders' in url_data.fragment): resource.open_url = resource.revision.content.url resource.type = 'gfolder' elif resource.revision.content_type == attachment_content_type: name, resource.extension = splitext(resource.revision.content.file.name) document = [".doc", ".docx", ".rtf", "odt"] presentation = [".key", ".keynote", ".ppt", ".pptx", ".odp"] spreadsheet = [".xls", ".xlsx", ".ods", ".csv"] pdf = [".pdf"] image = [".jpg", ".jpeg", ".png", ".bmp", ".eps", ".ps", ".gif", ".tiff"] ext = str.lower(str(resource.extension)) if ext in document: resource.type = 'document' elif ext in pdf: resource.type = 'pdf' elif ext in presentation: resource.type = 'presentation' elif ext in spreadsheet: resource.type = 'spreadsheet' elif ext in image: resource.type = 'image' else: resource.type = 'upload' elif resource.revision.content_type == reference_content_type: resource.type = 'reference'
def set_resources_type(resources): from django.contrib.contenttypes.models import ContentType from oer.models import Document, Link, Attachment document_content_type = ContentType.objects.get_for_model(Document) link_content_type = ContentType.objects.get_for_model(Link) attachment_content_type = ContentType.objects.get_for_model(Attachment) for resource in resources: if resource.revision.content_type == document_content_type: resource.type = "document" elif resource.revision.content_type == link_content_type: import ResourceUtilities as ru resource.type = ru.get_resource_type_from_url(resource) elif resource.revision.content_type == attachment_content_type: resource.type = "attachment"