def _restore_single(media): """ Requests restoration of all media associated with a video or image, except for thumbnails. If successful, the restoration requested boolean is set to False and the archive state is changed from `to_live` to `live`. """ media_restored = True for key in ["streaming", "archival", "audio", "image"]: if key not in media.media_files: continue for obj in media.media_files[key]: resource_restored = Resource.restore_resource(obj["path"]) media_restored = media_restored and resource_restored if key == "streaming": resource_restored = Resource.restore_resource( obj["segment_info"]) media_restored = media_restored and resource_restored if media_restored: media.archive_state = "live" media.restoration_requested = False media.save() return media_restored
def create_resource_from_suggestion(request, suggestion_id): user = request.user suggestion = get_object_or_404(SuggestResource, id=suggestion_id) output = {} user_data = additional_user_info(request) resource = Resource(title=suggestion.title, link=suggestion.link, abstract=suggestion.abstract, created_by=request.user) resource.save() for tag in suggestion.keywords.split(','): keyword = Keyword(content_object=resource, text=tag.strip(), user_recomendation=True) keyword.save() suggestion.status = 1 suggestion.save() output['alert'] = _("Resource created.") output['alerttype'] = "alert-success" return redirect('main.views.create_edit_resource', resource_id=resource.id)
def setUp(self): self.c = Client() self.u = User.objects.create_user("tester", "", "testerpass") self.p = Package.objects.create(name="Test Package", description="This is a test.", owner=self.u) self.p.resources.add(Resource.get("Celtic Art & Cultures")) self.p.resources.add(Resource.get("Monasticon Hibernicum")) self.p.save()
def frontend_urls(request): """ Our javascript needs a global dictionary of url templates like: FRONTEND_URLS = { 'reorder_section_node': '/casebooks/_CASEBOOK_ID/sections/_SECTION_ORDINALS/reorder/_CHILD_ORDINALS/' } These can't be generated directly from reverse() because reverse('reorder_section_node', args=['_CASEBOOK_ID', ...]) doesn't resolve -- we need to pass in args as ints or ordinals or Casebook instances or whatever. This function calls reverse() with valid inputs for each url template we need, and then replaces the strings in the resulting url with the desired placeholders. """ global _frontend_urls if not _frontend_urls: urls_in = { # key: [url_name, reverse_args, strings, placeholders] 'reorder_section_node': [ 'reorder_node', [1, "2", "3"], ["1", "2", "3"], ['_CASEBOOK_ID', '_SECTION_ORDINALS', '_CHILD_ORDINALS'] ], 'reorder_casebook_node': [ 'reorder_node', [1, "2"], ["1", "2"], ['_CASEBOOK_ID', '_CHILD_ORDINALS'] ], 'new_section_or_resource': ['new_section_or_resource', [1], ["1"], ['$CASEBOOK_ID']], 'search': ['search', [], [], []], 'new_casebook': ['new_casebook', [], [], []], 'section': ['section', [1, "2"], ["1", "2"], ['CASEBOOK_ID', 'SECTION_ID']], 'casebook': ['casebook', [1], ["1"], ['_ID']], 'export_casebook': [ 'export', [Casebook(id=1), "docx"], ["1", "docx"], ['_ID', '_FORMAT'] ], 'export_section': [ 'export', [Section(id=1), "docx"], ["1", "docx"], ['_ID', '_FORMAT'] ], 'export_resource': [ 'export', [Resource(id=1), "docx"], ["1", "docx"], ['_ID', '_FORMAT'] ], } urls_out = {} for key, [url_name, reverse_args, strings, placeholders] in urls_in.items(): url = reverse(url_name, args=reverse_args) for string, placeholder in zip(strings, placeholders): url = url.replace(str(string), placeholder, 1) urls_out[key] = url _frontend_urls = mark_safe(json.dumps(urls_out)) return {'frontend_urls': _frontend_urls}
def _resources_from_paths(paths): paths = [ os.readlink(path) if os.path.islink(path) else path for path in paths ] exists = list( Resource.objects.filter(path__in=paths).values_list('path', flat=True)) needs_create = list(set(paths).difference(exists)) paths = [] return [Resource(path=p) for p in needs_create]
def _archive_single(media): """ Attempts to archive all media associated with a video or image, except for thumbnails. If successful, the archive state of the media is changed from `to_archive` to `archived`. """ media_archived = True for key in ["streaming", "archival", "audio", "image"]: if key not in media.media_files: continue for obj in media.media_files[key]: resource_archived = Resource.archive_resource(obj["path"]) media_archived = media_archived and resource_archived if key == "streaming": resource_archived = Resource.archive_resource( obj["segment_info"]) media_archived = media_archived and resource_archived if media_archived: media.archive_state = "archived" media.save() return media_archived
def create_resource_from_suggestion(request, suggestion_id): user = request.user suggestion = get_object_or_404(SuggestResource, id=suggestion_id) output = {} user_data = additional_user_info(request) resource = Resource(title=suggestion.title, link=suggestion.link, abstract=suggestion.abstract, created_by=request.user) resource.save(); for tag in suggestion.keywords.split(','): keyword = Keyword(content_object=resource, text=tag.strip(), user_recomendation=True) keyword.save() suggestion.status = 1 suggestion.save(); output['alert'] = _("Resource created.") output['alerttype'] = "alert-success" return redirect('main.views.create_edit_resource', resource_id=resource.id)
def _request_restore_single(media, expiry_days): """ Requests restoration of all media associated with a video or image, except for thumbnails. If successful, the restoration requested boolean is set to True and the archive state remains `to_live`. """ media_requested = True for key in ["streaming", "archival", "audio", "image"]: if key not in media.media_files: continue for obj in media.media_files[key]: resource_requested = Resource.request_restoration(obj["path"], expiry_days) media_requested = media_requested and resource_requested if key == "streaming": resource_requested = Resource.request_restoration(obj["segment_info"], expiry_days) media_requested = media_requested and resource_requested if media_requested: media.archive_status_date = datetime.now(timezone.utc) media.restoration_requested = True media.save() return media_requested
from main import db, create_app from main.models import (Resource, Users) app = create_app() app.app_context().push() db.drop_all() db.create_all() resource = Resource() resource.ResName = "hello" db.session.add(resource) db.session.commit() user = Users() user.username = "******" user.age = 22 db.session.add(user) db.session.commit() user = Users() user.username = "******" user.age = 21 db.session.add(user) db.session.commit()