def render_one(self, request, asset, notes=None): try: bundle = Bundle(request=request) bundle = self.build_bundle(obj=asset, request=request) dehydrated = self.full_dehydrate(bundle) the_json = self._meta.serializer.to_simple(dehydrated, None) try: metadata = json.loads(bundle.obj.metadata_blob) metadata = [{"key": k, "value": v} for k, v in metadata.items()] the_json["metadata"] = metadata except ValueError: pass if notes: note_resource = SherdNoteResource() for note in notes: note_ctx = note_resource.render_one(request, note, "") if note.is_global_annotation(): the_json["global_annotation"] = note_ctx the_json["global_annotation_analysis"] = ( len(note_ctx["vocabulary"]) > 0 or len(note_ctx["metadata"]["body"]) > 0 or len(note_ctx["metadata"]["tags"]) > 0 ) else: the_json["annotations"].append(note_ctx) return the_json except Source.DoesNotExist: return None
def render_one(self, request, asset, notes=None): self.request = request try: bundle = self.build_bundle(obj=asset, request=request) dehydrated = self.full_dehydrate(bundle) the_json = self._meta.serializer.to_simple(dehydrated, None) try: metadata = json.loads(bundle.obj.metadata_blob) metadata = [{ 'key': k, 'value': v } for k, v in metadata.items()] the_json['metadata'] = metadata except ValueError: pass if notes: note_resource = SherdNoteResource() for note in notes: note_ctx = note_resource.render_one(request, note, "") the_json = add_note_ctx_to_json(note_ctx, the_json) return the_json except Source.DoesNotExist: return None
def render_list(self, request, me, assets, notes): note_resource = SherdNoteResource() ctx = {} for note in notes.all(): if note.asset.id not in ctx: abundle = self.build_bundle(obj=note.asset, request=request) dehydrated = self.full_dehydrate(abundle) asset_ctx = self._meta.serializer.to_simple(dehydrated, None) ctx[note.asset.id] = asset_ctx is_global = note.is_global_annotation() if not is_global: ctx[note.asset.id]['annotation_count'] += 1 if note.author == me: ctx[note.asset.id]['my_annotation_count'] += 1 if note.modified > note.asset.modified: ctx[note.asset.id]['modified'] = \ self.format_time(note.modified) if self.include_annotations: note_ctx = note_resource.render_one(request, note, "") if is_global: ctx[note.asset.id]['global_annotation'] = note_ctx else: ctx[note.asset.id]['annotations'].append(note_ctx) values = ctx.values() return sorted(values, key=lambda value: self.to_time(value['modified']), reverse=True)
def render_list(self, request, me, assets, notes): note_resource = SherdNoteResource() ctx = {} for note in notes.all(): if note.asset.id not in ctx: abundle = self.build_bundle(obj=note.asset, request=request) dehydrated = self.full_dehydrate(abundle) asset_ctx = self._meta.serializer.to_simple(dehydrated, None) ctx[note.asset.id] = asset_ctx is_global = note.is_global_annotation() if not is_global: ctx[note.asset.id]["annotation_count"] += 1 if note.author == me: ctx[note.asset.id]["my_annotation_count"] += 1 if note.modified > note.asset.modified: ctx[note.asset.id]["modified"] = self.format_time(note.modified) if self.include_annotations: note_ctx = note_resource.render_one(request, note, "") if is_global: ctx[note.asset.id]["global_annotation"] = note_ctx else: ctx[note.asset.id]["annotations"].append(note_ctx) values = ctx.values() return sorted(values, key=lambda value: self.to_time(value["modified"]), reverse=True)
def threaded_comment_json(request, comment): viewer = request.user coll = ContentType.objects.get_for_model(Collaboration) all_comments = ThreadedComment.objects.filter( content_type=coll, object_pk=comment.content_object.pk, site__pk=settings.SITE_ID) all_comments = fill_tree(all_comments) all_comments = list(annotate_tree_properties(all_comments)) rand = ''.join([choice(letters) for i in range(5)]) citations = threaded_comment_citations(all_comments, viewer) asset_resource = AssetResource() sherd_resource = SherdNoteResource() return { 'type': 'discussion', 'form': comments.get_form()(comment.content_object).__unicode__(), 'editing': True, 'can_edit': True, 'discussion': { 'id': comment.id, 'max_length': COMMENT_MAX_LENGTH, 'thread': [{ 'open': obj.open if hasattr(obj, "open") else None, 'close': [i for i in obj.close] if hasattr(obj, "close") else None, 'id': obj.id, 'author': obj.name, 'author_username': obj.user.username, 'submit_date': pretty_date(obj.submit_date), 'title': obj.title, 'content': obj.comment, 'can_edit': True if obj.user == viewer else False } for obj in all_comments] }, 'assets': dict([('%s_%s' % (rand, ann.asset.pk), asset_resource.render_one(request, ann.asset)) for ann in citations if (ann.title != "Annotation Deleted" and ann.title != 'Asset Deleted')]), 'annotations': [sherd_resource.render_one(request, ann, rand) for ann in citations], }
def related_assets_notes(self, request, project): asset_resource = AssetResource() sherd_resource = SherdNoteResource() rand = ''.join([choice(letters) for i in range(5)]) assets = {} notes = [] for note in project.citations(): notes.append(sherd_resource.render_one(request, note, rand)) if (note.title not in ["Annotation Deleted", 'Asset Deleted']): key = '%s_%s' % (rand, note.asset.pk) if key not in assets.keys(): assets[key] = \ asset_resource.render_one(request, note.asset) return assets, notes
def related_assets_notes(self, request, project): asset_resource = AssetResource() sherd_resource = SherdNoteResource() rand = ''.join([choice(ascii_letters) for i in range(5)]) # nosec assets = {} notes = [] for note in project.citations(): notes.append(sherd_resource.render_one(request, note, rand)) if (note.title not in ["Annotation Deleted", 'Asset Deleted']): key = '%s_%s' % (rand, note.asset.pk) if key not in assets.keys(): assets[key] = \ asset_resource.render_one(request, note.asset) return assets, notes
def threaded_comment_json(request, comment): viewer = request.user coll = ContentType.objects.get_for_model(Collaboration) all_comments = ThreadedComment.objects.filter( content_type=coll, object_pk=comment.content_object.pk, site__pk=settings.SITE_ID) all_comments = fill_tree(all_comments) all_comments = list(annotate_tree_properties(all_comments)) rand = ''.join([choice(letters) for i in range(5)]) citations = threaded_comment_citations(all_comments, viewer) asset_resource = AssetResource() sherd_resource = SherdNoteResource() return { 'type': 'discussion', 'form': django_comments.get_form()( comment.content_object).__unicode__(), 'editing': True, 'can_edit': True, 'discussion': { 'id': comment.id, 'max_length': COMMENT_MAX_LENGTH, 'thread': [{'open': obj.open if hasattr(obj, "open") else None, 'close': [i for i in obj.close] if hasattr(obj, "close") else None, 'id': obj.id, 'author': obj.name, 'author_username': obj.user.username, 'submit_date': pretty_date(obj.submit_date), 'title': obj.title, 'content': obj.comment, 'can_edit': True if obj.user == viewer else False} for obj in all_comments] }, 'assets': dict([('%s_%s' % (rand, ann.asset.pk), asset_resource.render_one(request, ann.asset)) for ann in citations if (ann.title != "Annotation Deleted" and ann.title != 'Asset Deleted')]), 'annotations': [sherd_resource.render_one(request, ann, rand) for ann in citations], }
def render_one(self, request, asset, notes=None): try: bundle = Bundle(request=request) bundle = self.build_bundle(obj=asset, request=request) dehydrated = self.full_dehydrate(bundle) the_json = self._meta.serializer.to_simple(dehydrated, None) try: metadata = json.loads(bundle.obj.metadata_blob) metadata = [{ 'key': k, 'value': v } for k, v in metadata.items()] the_json['metadata'] = metadata except ValueError: pass if notes: note_resource = SherdNoteResource() for note in notes: note_ctx = note_resource.render_one(request, note, "") if note.is_global_annotation(): the_json['global_annotation'] = note_ctx the_json['global_annotation_analysis'] = ( len(note_ctx['vocabulary']) > 0 or len(note_ctx['metadata']['body']) > 0 or len(note_ctx['metadata']['tags']) > 0) else: the_json['annotations'].append(note_ctx) return the_json except Source.DoesNotExist: return None
def render_one(self, request, asset, notes=None): try: bundle = Bundle(request=request) bundle = self.build_bundle(obj=asset, request=request) dehydrated = self.full_dehydrate(bundle) the_json = self._meta.serializer.to_simple(dehydrated, None) try: metadata = json.loads(bundle.obj.metadata_blob) metadata = [{'key': k, 'value': v} for k, v in metadata.items()] the_json['metadata'] = metadata except ValueError: pass if notes: note_resource = SherdNoteResource() for note in notes: note_ctx = note_resource.render_one(request, note, "") the_json = add_note_ctx_to_json(note_ctx, the_json) return the_json except Source.DoesNotExist: return None
def render_one(self, request, project, version_number=None): bundle = self.build_bundle(obj=project, request=request) dehydrated = self.full_dehydrate(bundle) project_ctx = self._meta.serializer.to_simple(dehydrated, None) project_ctx["body"] = project.body project_ctx["public_url"] = project.public_url() project_ctx["current_version"] = version_number project_ctx["visibility"] = project.visibility_short() project_ctx["type"] = "assignment" if project.is_assignment(request) else "composition" rand = "".join([choice(letters) for i in range(5)]) asset_resource = AssetResource() sherd_resource = SherdNoteResource() assets = {} notes = [] for note in project.citations(): notes.append(sherd_resource.render_one(request, note, rand)) if note.title not in ["Annotation Deleted", "Asset Deleted"]: key = "%s_%s" % (rand, note.asset.pk) if key not in assets.keys(): assets[key] = asset_resource.render_one(request, note.asset) data = { "project": project_ctx, "type": "project", "can_edit": self.editable, "annotations": notes, "assets": assets, } data["responses"] = [] for response in project.responses(request): if response.can_read(request): obj = { "url": response.get_absolute_url(), "title": response.title, "modified": response.modified.strftime(self.date_fmt), "attribution_list": [], } last = len(response.attribution_list()) - 1 for idx, author in enumerate(response.attribution_list()): obj["attribution_list"].append({"name": get_public_name(author, request), "last": idx == last}) data["responses"].append(obj) data["response_count"] = len(data["responses"]) my_responses = [] for response in project.responses_by(request, request.user): obj = { "url": response.get_absolute_url(), "title": response.title, "modified": response.modified.strftime(self.date_fmt), "attribution_list": [], } last = len(response.attribution_list()) - 1 for idx, author in enumerate(response.attribution_list()): obj["attribution_list"].append({"name": get_public_name(author, request), "last": idx == last}) my_responses.append(obj) if len(my_responses) == 1: data["my_response"] = my_responses[0] elif len(my_responses) > 1: data["my_responses"] = my_responses data["my_responses_count"] = len(my_responses) if project.is_participant(request.user): data["revisions"] = [ { "version_number": v.version_number, "versioned_id": v.versioned_id, "author": get_public_name(v.instance().author, request), "modified": v.modified.strftime("%m/%d/%y %I:%M %p"), } for v in project.versions.order_by("-change_time") ] if self.editable: projectform = ProjectForm(request, instance=project) data["form"] = { "participants": projectform["participants"].__unicode__(), "publish": projectform["publish"].__unicode__(), } return data
def composition_project_json(request, project, can_edit, version_number=None): ''' JSON representation for a project. Includes: * basic project information * assets * annotations * responses (if applicable & permissable) * revisions (if permissable) * a project participant form (if permissable) ''' rand = ''.join([choice(letters) for i in range(5)]) versions = project.versions.order_by('-change_time') participants = project.attribution_list() is_assignment = project.is_assignment(request) course = request.course if request.course \ else request.collaboration_context.content_object asset_resource = AssetSummaryResource() sherd_resource = SherdNoteResource() citations = project.citations() data = { 'project': { 'id': project.pk, 'title': project.title, 'url': project.get_absolute_url(), 'due_date': project.get_due_date(), 'body': project.body, 'participants': [{ 'name': p.get_full_name(), 'username': p.username, 'public_name': get_public_name(p, request), 'is_viewer': request.user.username == p.username, 'last': idx == (len(participants) - 1) } for idx, p in enumerate(participants)], 'public_url': project.public_url(), 'visibility': project.visibility_short(), 'username': request.user.username, 'type': 'assignment' if is_assignment else 'composition', 'description': project.description(), 'current_version': version_number if version_number else None, 'create_selection': is_assignment, 'is_assignment': is_assignment, 'course_title': course.title }, 'type': 'project', 'can_edit': can_edit, 'annotations': [sherd_resource.render_one(request, ann, rand) for ann in citations], } assets = {} for ann in citations: if ann.title != "Annotation Deleted" and ann.title != 'Asset Deleted': key = '%s_%s' % (rand, ann.asset.pk) if key not in assets: assets[key] = asset_resource.render_one(request, ann.asset) data['assets'] = assets data['responses'] = [] for r in project.responses(request): if r.can_read(request): obj = {'url': r.get_absolute_url(), 'title': r.title, 'modified': r.modified.strftime("%m/%d/%y %I:%M %p"), 'attribution_list': []} x = len(r.attribution_list()) - 1 for idx, author in enumerate(r.attribution_list()): obj['attribution_list'].append({ 'name': get_public_name(author, request), 'last': idx == x}) data['responses'].append(obj) data['response_count'] = len(data['responses']) my_responses = [] for r in project.responses_by(request, request.user): obj = {'url': r.get_absolute_url(), 'title': r.title, 'modified': r.modified.strftime("%m/%d/%y %I:%M %p"), 'attribution_list': []} x = len(r.attribution_list()) - 1 for idx, author in enumerate(r.attribution_list()): obj['attribution_list'].append({'name': get_public_name(author, request), 'last': idx == x}) my_responses.append(obj) if len(my_responses) == 1: data['my_response'] = my_responses[0] elif len(my_responses) > 1: data['my_responses'] = my_responses data['my_responses_count'] = len(my_responses) if project.is_participant(request.user): data['revisions'] = [{ 'version_number': v.version_number, 'versioned_id': v.versioned_id, 'author': get_public_name(v.instance().author, request), 'modified': v.modified.strftime("%m/%d/%y %I:%M %p")} for v in versions] if can_edit: projectform = ProjectForm(request, instance=project) data['form'] = { 'participants': projectform['participants'].__unicode__(), 'publish': projectform['publish'].__unicode__() } return data
def render_one(self, request, project, version_number=None): bundle = self.build_bundle(obj=project, request=request) dehydrated = self.full_dehydrate(bundle) project_ctx = self._meta.serializer.to_simple(dehydrated, None) project_ctx['body'] = project.body project_ctx['public_url'] = project.public_url() project_ctx['current_version'] = version_number project_ctx['visibility'] = project.visibility_short() project_ctx['type'] = ('assignment' if project.is_assignment(request) else 'composition') rand = ''.join([choice(letters) for i in range(5)]) asset_resource = AssetResource() sherd_resource = SherdNoteResource() assets = {} notes = [] for note in project.citations(): notes.append(sherd_resource.render_one(request, note, rand)) if (note.title not in ["Annotation Deleted", 'Asset Deleted']): key = '%s_%s' % (rand, note.asset.pk) if key not in assets.keys(): assets[key] = \ asset_resource.render_one(request, note.asset) data = { 'project': project_ctx, 'type': 'project', 'can_edit': self.editable, 'annotations': notes, 'assets': assets } data['responses'] = [] for response in project.responses(request): if response.can_read(request): obj = { 'url': response.get_absolute_url(), 'title': response.title, 'modified': response.modified.strftime(self.date_fmt), 'attribution_list': [] } last = len(response.attribution_list()) - 1 for idx, author in enumerate(response.attribution_list()): obj['attribution_list'].append({ 'name': get_public_name(author, request), 'last': idx == last }) data['responses'].append(obj) data['response_count'] = len(data['responses']) my_responses = [] for response in project.responses_by(request, request.user): obj = { 'url': response.get_absolute_url(), 'title': response.title, 'modified': response.modified.strftime(self.date_fmt), 'attribution_list': [] } last = len(response.attribution_list()) - 1 for idx, author in enumerate(response.attribution_list()): obj['attribution_list'].append({ 'name': get_public_name(author, request), 'last': idx == last }) my_responses.append(obj) if len(my_responses) == 1: data['my_response'] = my_responses[0] elif len(my_responses) > 1: data['my_responses'] = my_responses data['my_responses_count'] = len(my_responses) if project.is_participant(request.user): data['revisions'] = [{ 'version_number': v.version_number, 'versioned_id': v.versioned_id, 'author': get_public_name(v.instance().author, request), 'modified': v.modified.strftime("%m/%d/%y %I:%M %p") } for v in project.versions.order_by('-change_time')] if self.editable: projectform = ProjectForm(request, instance=project) data['form'] = { 'participants': projectform['participants'].__unicode__(), 'publish': projectform['publish'].__unicode__() } return data
def composition_project_json(request, project, can_edit, version_number=None): ''' JSON representation for a project. Includes: * basic project information * assets * annotations * responses (if applicable & permissable) * revisions (if permissable) * a project participant form (if permissable) ''' rand = ''.join([choice(letters) for i in range(5)]) participants = project.attribution_list() is_assignment = project.is_assignment(request) course = request.course if request.course \ else request.collaboration_context.content_object asset_resource = AssetSummaryResource() sherd_resource = SherdNoteResource() citations = project.citations() data = { 'project': { 'id': project.pk, 'title': project.title, 'url': project.get_absolute_url(), 'due_date': project.get_due_date(), 'body': project.body, 'participants': [{ 'name': p.get_full_name(), 'username': p.username, 'public_name': get_public_name(p, request), 'is_viewer': request.user.username == p.username, 'last': idx == (len(participants) - 1) } for idx, p in enumerate(participants)], 'public_url': project.public_url(), 'visibility': project.visibility_short(), 'username': request.user.username, 'type': 'assignment' if is_assignment else 'composition', 'description': project.description(), 'current_version': version_number if version_number else None, 'create_selection': is_assignment, 'is_assignment': is_assignment, 'course_title': course.title }, 'type': 'project', 'can_edit': can_edit, 'annotations': [sherd_resource.render_one(request, ann, rand) for ann in citations], } assets = {} for ann in citations: if ann.title != "Annotation Deleted" and ann.title != 'Asset Deleted': key = '%s_%s' % (rand, ann.asset.pk) if key not in assets: assets[key] = asset_resource.render_one(request, ann.asset) data['assets'] = assets data['responses'] = [] for response in project.responses(request): if response.can_read(request): obj = { 'url': response.get_absolute_url(), 'title': response.title, 'modified': response.modified.strftime("%m/%d/%y %I:%M %p"), 'attribution_list': [] } last = len(response.attribution_list()) - 1 for idx, author in enumerate(response.attribution_list()): obj['attribution_list'].append({ 'name': get_public_name(author, request), 'last': idx == last }) data['responses'].append(obj) data['response_count'] = len(data['responses']) my_responses = [] for response in project.responses_by(request, request.user): obj = { 'url': response.get_absolute_url(), 'title': response.title, 'modified': response.modified.strftime("%m/%d/%y %I:%M %p"), 'attribution_list': [] } last = len(response.attribution_list()) - 1 for idx, author in enumerate(response.attribution_list()): obj['attribution_list'].append({ 'name': get_public_name(author, request), 'last': idx == last }) my_responses.append(obj) if len(my_responses) == 1: data['my_response'] = my_responses[0] elif len(my_responses) > 1: data['my_responses'] = my_responses data['my_responses_count'] = len(my_responses) if project.is_participant(request.user): data['revisions'] = [{ 'version_number': v.version_number, 'versioned_id': v.versioned_id, 'author': get_public_name(v.instance().author, request), 'modified': v.modified.strftime("%m/%d/%y %I:%M %p") } for v in project.versions.order_by('-change_time')] if can_edit: projectform = ProjectForm(request, instance=project) data['form'] = { 'participants': projectform['participants'].__unicode__(), 'publish': projectform['publish'].__unicode__() } return data