def get(self, request, **kwargs): stream = target_stream(self.object).order_by('-timestamp') started = stream.last().timestamp current = started maximum = timezone.now() + datetime.timedelta(hours=12) counters = [] while current < maximum: qs = stream.filter(timestamp__year=current.year, timestamp__month=current.month, timestamp__day=current.day) counters.append(qs.count()) current += datetime.timedelta(days=1) return Response({ 'title': _(u"Actions timeline for ") + self.object.__unicode__(), 'point_interval': 24 * 3600 * 1000, 'date_started': started, 'name': _(u"Activities"), 'count': stream.count(), 'results': counters, })
def get_history(nh): """ :param nh: NodeHandle :return: List of ActStream actions """ history = list(action_object_stream(nh)) + list(target_stream(nh)) return sorted(history, key=lambda action: action.timestamp, reverse=True)
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))
def get_queryset(self): stream_type = self.request.QUERY_PARAMS.get('type') try: content_type = int(self.request.QUERY_PARAMS.get('ct')) object_id = int(self.request.QUERY_PARAMS.get('pk')) except (TypeError, ValueError): return super(ActivityViewSet, self).get_queryset() ct = ContentType.objects.get(pk=content_type) self.request_object = ct.get_object_for_this_type(pk=object_id) if stream_type == 'actor': qs = actor_stream(self.request_object) elif stream_type == 'target': qs = target_stream(self.request_object) elif stream_type == 'ngo': qs = Action.objects.ngostream(self.request_object) elif stream_type == 'location': qs = Action.objects.location(self.request_object) else: qs = Action.objects.mystream(self.request_object) content_filter = self.content_filter() if content_filter is not None: qs = qs.filter( Q(action_object_content_type__pk=content_filter) | Q(target_content_type__pk=content_filter)) date_filter = self.date_filter() if date_filter is not None: qs = qs.filter(timestamp__gte=date_filter) return qs
def get_queryset(self): stream_type = self.request.QUERY_PARAMS.get('type') try: content_type = int(self.request.QUERY_PARAMS.get('ct')) object_id = int(self.request.QUERY_PARAMS.get('pk')) except (TypeError, ValueError): return super(ActivityViewSet, self).get_queryset() ct = ContentType.objects.get(pk=content_type) self.request_object = ct.get_object_for_this_type(pk=object_id) if stream_type == 'actor': qs = actor_stream(self.request_object) elif stream_type == 'target': qs = target_stream(self.request_object) elif stream_type == 'ngo': qs = Action.objects.ngostream(self.request_object) elif stream_type == 'location': qs = Action.objects.location(self.request_object) else: qs = Action.objects.mystream(self.request_object) content_filter = self.content_filter() if content_filter is not None: qs = qs.filter(Q(action_object_content_type__pk=content_filter) | Q(target_content_type__pk=content_filter)) date_filter = self.date_filter() if date_filter is not None: qs = qs.filter(timestamp__gte=date_filter) return qs
def get_context_data(self, slug, **kwargs): context = super(ProjectHistoryView, self).get_context_data(slug, **kwargs) parent_project = self.project_translation.master context["activity"] = target_stream(parent_project) return context
def get_context_data(self, slug, **kwargs): context = super(ProjectHistoryView, self).get_context_data(slug, **kwargs) parent_project = self.project_translation.master context['activity'] = target_stream(parent_project) return context
def patch(self, request, *args, **kwargs): notifications = target_stream(request.user) for notification in notifications: # This might be slow on large queries but provides safety and should be fine notification.target = None notification.save() return Response({})
def notifications_count(request): """ Returns the amount of notifications for where the user is a target """ if request.user.is_authenticated: notifications = target_stream(request.user) return {"notifications_count": len(notifications)} else: return {"notifications": None}
def notifications(request): """ Returns all Notifications for a user specifically returns all where user is the target """ if request.user.is_authenticated: notifications = target_stream(request.user) return {"notifications": notifications} else: return {"notifications": None}
def get_activity_stream(space): """ Return a list of recent activities of interest for members of the current space. The actual events get created by the individual space_plugins """ if space: # space is none for request like e.g. GET /favicon.ico3 ret = target_stream(space)[:10] else: ret = None return ret
def test_post_with_FILE_executed_file_and_stream(self): self.test_post_with_FILE_executed_file() stream = target_stream(self.matter) self.assertEqual(stream[0].data["override_message"], u"Lawyër Tëst added a file to Test Item with Revision") revision = self.item.revision_set.all().first() self.assertEqual( revision.executed_file.name, "executed_files/v1-%s-%s-test.pdf" % (self.item.pk, self.lawyer.username) ) self.assertEqual( revision.executed_file.url, "/m/executed_files/v1-%s-%s-test.pdf" % (self.item.pk, self.lawyer.username) )
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) try: next_report = report.get_next_by_created_at() except: next_report = None try: prev_report = report.get_previous_by_created_at() except: prev_report = 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, 'next_report': next_report, 'prev_report': prev_report, }, context_instance=RequestContext(request))
def get_actions_following(request, content_type_id, object_id): activity_queryset = None ctype = get_object_or_404(ContentType, pk=content_type_id) actor = get_object_or_404(ctype.model_class(), pk=object_id) if activity_queryset is None: activity_queryset = actor.actor_actions.public() for followedObject in Follow.objects.following(user=actor): if followedObject: obj_content_type = ContentType.objects.get_for_model(followedObject) followObject = Follow.objects.get(user=actor, content_type=obj_content_type, object_id=followedObject.pk ) if followObject: stream = followedObject.actor_actions.public()#timestamp__gte=followObject.started) activity_queryset = activity_queryset | stream if not isinstance(followedObject, User) and not isinstance(followedObject, BlogPost): _follow = _Follow.objects.get_follows(followedObject) if _follow: follow = None try: follow = _follow.get(user=actor) except (MultipleObjectsReturned, _Follow.DoesNotExist) as e: if isinstance(e, MultipleObjectsReturned): follow = _follow.filter(user=actor)[0] pass else: follow = None pass if follow: stream = models.action_object_stream(followedObject)#, timestamp__gte = follow.datetime ) activity_queryset = activity_queryset | stream stream = models.target_stream(followedObject)#, timestamp__gte = follow.datetime ) activity_queryset = activity_queryset | stream allowed_verbs_for_user_in_common_feed = [settings.SAID_VERB, settings.SHARE_VERB, settings.DEAL_POST_VERB, settings.WISH_POST_VERB]# settings.REVIEW_POST_VERB, ] user_ctype = ContentType.objects.get_for_model(request.user) activity_queryset = activity_queryset.exclude(~Q(verb__in=allowed_verbs_for_user_in_common_feed) & Q(actor_content_type=user_ctype, actor_object_id=request.user.id) ) followed_blog_posts = utils.get_following_vendors_for_user(request.user) blogPostContentType = ContentType.objects.get_for_model(BlogPost) if followed_blog_posts: activity_queryset = activity_queryset.exclude(Q(verb=settings.REVIEW_POST_VERB) & Q(action_object_content_type=blogPostContentType) & Q(action_object_object_id__in=[blogpost.id for blogpost in followed_blog_posts])) activity_queryset = activity_queryset.order_by('-timestamp') return activity_queryset
def actions_chart(location): """ Summary of different kinds of actions related to particular location. """ content_items = ['ideas', 'polls', 'news', 'discussions', 'projects', ] content_types = { 'ideas': ContentType.objects.get(app_label='ideas', model='idea'), 'polls': ContentType.objects.get(app_label='polls', model='poll'), 'news': ContentType.objects.get(app_label='blog', model='news'), 'discussions': ContentType.objects.get(app_label='topics', model='discussion'), 'projects': ContentType.objects.get(app_label='projects', model='socialproject'), } stream = target_stream(location) started = stream.last().timestamp current = started maximum = timezone.now() + datetime.timedelta(hours=12) labels = { 'ideas': _(u"ideas"), 'polls': _(u"polls"), 'news': _(u"news"), 'discussions': _(u"discussions"), 'projects': _(u"projects"), } counters = { 'ideas': [], 'polls': [], 'news': [], 'discussions': [], 'projects': [], } while current < maximum: for itm in content_items: counters[itm].append(qs_by_time(stream.filter( action_object_content_type=content_types[itm]), current)) current += datetime.timedelta(days=1) return json.dumps({ 'title': location.__unicode__(), 'subtitle': _(u"Activity summary"), 'started': str(started), 'labels': labels, 'series': counters, })
def test_lawyer_patch(self): self.client.login(username=self.lawyer.username, password=self.password) data = { 'email': '*****@*****.**', 'first_name': 'Bob', 'last_name': 'Da hoon', 'message': 'Bob you are being added here please provide me with a monkey!', } resp = self.client.patch(self.endpoint, json.dumps(data), content_type='application/json') self.assertEqual(resp.status_code, 200) # ok patch accepted json_data = json.loads(resp.content) new_item = Item.objects.requested(matter=self.matter, slug=json_data['slug']).first() self.assertEqual(json_data['name'], new_item.name) # we should have this new item in the standard item objects self.assertTrue(new_item in Item.objects.filter(matter=self.matter)) outbox = mail.outbox self.assertEqual(len(outbox), 1) email = outbox[0] self.assertEqual(email.subject, u'[ACTION REQUIRED] Request to provide a document') self.assertEqual(email.recipients(), [u'*****@*****.**']) # test the custom message is present self.assertTrue(data.get('message') in email.body) # this should have created a new revision upload invite stream = action_object_stream(self.item) self.assertEqual(stream[0].data['override_message'], u'Lawyër Tëst requested a file from Bob Da hoon for Test Item No. 1') # now we patch again to remove the revision_request and see if the activity is created data = { 'is_requested': False, 'responsible_party': None } self.client.patch(self.endpoint, json.dumps(data), content_type='application/json') stream = target_stream(self.matter) self.assertEqual(stream[0].data['override_message'], u'Lawyër Tëst canceled their request for Bob Da hoon to provide a document on Test Item No. 1')
def get_first_page(obj, stream_type="actor"): """ Simplified method to get first activity stream page to display it as-is without further API calls from JavaScript. Results are serialized in the same way as in regular API view. """ if stream_type == 'actor': qs = actor_stream(obj) elif stream_type == 'target': qs = target_stream(obj) elif stream_type == 'ngo': qs = Action.objects.ngostream(obj) elif stream_type == 'location': qs = Action.objects.location(obj) else: qs = Action.objects.mystream(obj) return { 'results': ActionSerializer(qs[:15], many=True).data, 'count': len(qs), 'next': len(qs) > 15, }
def get_queryset(self): stream_type = self.request.QUERY_PARAMS.get('type') try: content_type = int(self.request.QUERY_PARAMS.get('ct')) object_id = int(self.request.QUERY_PARAMS.get('pk')) except (TypeError, ValueError): return super(ActivityViewSet, self).get_queryset() ct = ContentType.objects.get(pk=content_type) self.request_object = ct.get_object_for_this_type(pk=object_id) if stream_type == 'actor': return actor_stream(self.request_object) elif stream_type == 'target': return target_stream(self.request_object) elif stream_type == 'ngo': return Action.objects.ngostream(self.request_object) elif stream_type == 'location': return Action.objects.location(self.request_object) else: return Action.objects.mystream(self.request_object)
def projectDetail(request: HttpRequest, projectid): if not request.user.is_authenticated: return redirect('projectMgr:homepage') ''' :param request: :return: ''' thisProject = models.Project.objects.get(id=projectid) print(thisProject.name) commentForm = forms.createComment() commentSet = thisProject.comment.all() stream = target_stream(thisProject) return render(request, 'projectMgr/projectDetail.html', {'project': thisProject, 'commentForm': commentForm, 'commentSet': commentSet, 'thisUser': request.user,'nocomm':len(commentSet),'stream':stream}) pass
def actstream_following(request, content_type_id, object_id): from itertools import chain import operator ctype = get_object_or_404(ContentType, pk=content_type_id) actor = get_object_or_404(ctype.model_class(), pk=object_id) activity = actor.actor_actions.public() for followedActor in Follow.objects.following(user=actor): target_content_type = ContentType.objects.get_for_model(followedActor) prevFollowActions = Action.objects.all().filter(actor_content_type=ctype, actor_object_id=object_id,verb=settings.FOLLOW_VERB, target_content_type=target_content_type, target_object_id = followedActor.pk ).order_by('-pk') followAction = None if prevFollowActions: followAction = prevFollowActions[0] if followAction: stream = followedActor.actor_actions.public(timestamp__gte = followAction.timestamp) activity = activity | stream if not isinstance(followedActor, User): _follow = _Follow.objects.get_follows(followedActor) if _follow: try: follow = _follow.get(user=actor) except (MultipleObjectsReturned, _Follow.DoesNotExist) as e: if isinstance(e, MultipleObjectsReturned): follow = _follow.filter(user=actor)[0] pass else: follow = None pass if follow: stream = models.action_object_stream(followedActor, timestamp__gte = follow.datetime ) activity = activity | stream stream = models.target_stream(followedActor, timestamp__gte = follow.datetime ) activity = activity | stream activity = activity.order_by('-timestamp') return render_to_response(('actstream/actor_feed.html', 'activity/actor_feed.html'), { 'action_list': activity, 'actor': actor, 'ctype': ctype, 'sIndex':0 }, context_instance=RequestContext(request))
def userHome(request: HttpRequest): ''' need to display user specific information related to the actual functionality of hte product. shwo the list of projects the user is associated with. list of teams the user is a part of. options ot create new teams and projects. need to hence create list and detail views of projects, teams, etc. need to have a way to search for the users based on thier username or email id. they should also be able to search for projects in this manner. when they are in the detail view of the team, they can have an option to search for projects nad members in thier own team. in their homepage, they can search for projects that they have been a part of. the body can list all the projects they are a part of. irrespective of who has created it. :param request: :return: choice1:can either just send the user and extract the different sets form it choice2: can pass multiple contexts. need to ''' user = None print(request.user) if request.user.is_authenticated: user = request.user else: print("cannot authenticate") return redirect("projectMgr:homepage") listSet = [] followingObjects = following(request.user) for object in following(request.user): if object: listSet.append(target_stream(object)[:3]) return render(request, 'projectMgr/userhome.html', {'thisUser': user,'list':list,'following':followingObjects,'listSet':listSet})
def taskGroupDetail(request: HttpRequest, projectid, taskGroupId): if not request.user.is_authenticated: return redirect('projectMgr:homepage') ''' :param request: :return: ''' print('received', projectid, taskGroupId) thisTaskGroup = models.TaskGroup.objects.get(id=taskGroupId) project = models.Project.objects.get(id=projectid) commentForm = forms.createComment() commentSet = thisTaskGroup.comment.all() openTaskSet = thisTaskGroup.task_set.filter(stage='OpenedIssue') InProgressTaskSet = thisTaskGroup.task_set.filter(stage='InProgress') InReviewTaskSet = thisTaskGroup.task_set.filter(stage='InReview') ClosedTaskSet = thisTaskGroup.task_set.filter(stage='ClosedIssue') stream = target_stream(thisTaskGroup) return render(request, 'projectMgr/taskGroupDetail.html', {'project': project, 'taskGroup': thisTaskGroup, 'commentForm': commentForm, 'commentSet': commentSet, 'thisUser': request.user, 'OpenIssue': openTaskSet, 'InProgress': InProgressTaskSet, 'InReview': InReviewTaskSet, "ClosedIssue": ClosedTaskSet,'stream':stream})
def deleteObject(request, content_type_id, object_id ): error_codes = [] #if not request.is_ajax(): # error_codes.append(settings.AJAX_ONLY_SUPPORT) # return json_error_response(error_codes) try: ctype = ContentType.objects.get(pk=content_type_id) object = ctype.model_class().objects.get(pk=object_id) except: error_codes.append(settings.OBJECT_DOES_NOT_EXIST) return json_error_response(error_codes) owner = get_object_owner_helper(content_type_id, object_id) """ Check whehter use is authorized to delete the object. """ if request.user != owner: error_codes.append(settings.UNAUTHORIZED_OPERATION) return json_error_response(error_codes) if object: """ Delete related activities and actstream follow objects. """ stream = models.action_object_stream(object) activity = stream stream = models.target_stream(object) activity = activity | stream if activity: activity.delete() _followList = _Follow.objects.filter(content_type=ctype, object_id=str(object_id)) for _followObj in _followList: _followObj.delete() """ Delete django-follow objects. """ follow = None try: follow = Follow.objects.get_follows(object) except: pass if follow: follow.delete() """ Delete voting objects. """ voteObjects = Vote.objects.filter(content_type=ctype, object_id=object._get_pk_val() ) if voteObjects: voteObjects.delete() """ Delete comments associated with the object. """ comments_queryset = object.comments.all() for comment in comments_queryset: ctype_id = ContentType.objects.get_for_model(comment).pk deleteObject(request, ctype_id, comment.pk) #comments_queryset.delete() """ Delete related objects for Reviews. """ if isinstance(object, Review): try: requiredReviewRatingObj = RequiredReviewRating.objects.get(commentid=object._get_pk_val()) except: requiredReviewRatingObj = None pass if requiredReviewRatingObj: requiredReviewRatingCtype = ContentType.objects.get_for_model(requiredReviewRatingObj) requiredReviewRatingVoteObjects = Vote.objects.filter(content_type=requiredReviewRatingCtype, object_id=requiredReviewRatingObj._get_pk_val() ) if requiredReviewRatingVoteObjects: requiredReviewRatingVoteObjects.delete() requiredReviewRatingObj.delete() try: OptionalReviewRatingObj = OptionalReviewRating.objects.get(commentid=object._get_pk_val()) except: OptionalReviewRatingObj = None pass if OptionalReviewRatingObj: OptionalReviewRatingObj.delete() elif isinstance(object, GenericWish): if object.wishimage: try: deleteFile(object.wishimage.image) except: pass object.wishimage.delete() """ Finally nuke the actual object. """ object.delete() return json_success_response()
def test_post_with_URL_executed_file_and_stream(self): self.test_post_with_URL_executed_file() stream = target_stream(self.matter) self.assertEqual(stream[0].data["override_message"], u"Lawyër Tëst added a file to Test Item with Revision")
def get_actions(self, request): if request.user.is_authenticated(): actions = target_stream(request.user) | user_stream(request.user) else: actions = Action.objects.public() return actions
def _all_related(obj): # TODO move somewhere else registry.register(User) return actor_stream(obj) | action_object_stream(obj) | target_stream(obj)
def show_target_stream(context, target, offset=0, count=30): params = {"actions": target_stream(target)[offset:count]} context.update(params) return context
def activity(self): return target_stream(self, '')
def test_lawyer_post(self): """ This is a bit of an anti pattern we POST a username into the endpoint and the system will create an account as well as assign them as a reviewer to the item revision """ self.client.login(username=self.lawyer.username, password=self.password) # expect 1 review document at this point self.assertEqual(self.revision.reviewdocument_set.all().count(), 1) participant = mommy.make('auth.User', first_name='Participant', last_name='Number 1', email='*****@*****.**') data = { "username": participant.username } resp = self.client.post(self.endpoint, json.dumps(data), content_type='application/json') self.assertEqual(resp.status_code, 201) # created json_data = json.loads(resp.content) # expect 2 review documents at this point self.assertEqual(self.revision.reviewdocument_set.all().count(), 2) # expect the newly created review doc to be available to the reviewer self.assertEqual(participant.reviewdocument_set.all().count(), 1) ## test the order by is workng order by newest first self.assertEqual(participant.reviewdocument_set.first(), self.revision.reviewdocument_set.first()) self.assertEqual(json_data['reviewer']['name'], participant.get_full_name()) # test they are in the items reviewer set self.assertTrue(participant in self.item.latest_revision.reviewers.all()) # # Test they show in the GET # resp = self.client.get(self.endpoint) self.assertEqual(resp.status_code, 200) json_data = json.loads(resp.content) self.assertEqual(json_data['count'], 2) self.assertEqual(json_data['results'][0]['reviewer']['name'], participant.get_full_name()) # we have no reviewers and the last object in the set should be the oldest self.assertEqual(json_data['results'][1]['reviewer'], None) # user review url must be in it self.assertTrue('user_review' in json_data['results'][0]['reviewer'].keys()) # # we expect the currently logged in users url to be returned; # as the view is relative to the user # review_document = self.item.latest_revision.reviewdocument_set.all().first() expected_url = review_document.get_absolute_url(user=self.lawyer) self.assertEqual(json_data['results'][0]['reviewer']['user_review'], { 'url': expected_url, 'slug': str(review_document.slug) }) outbox = mail.outbox self.assertEqual(len(outbox), 1) email = outbox[0] self.assertEqual(email.subject, '[ACTION REQUIRED] Invitation to review a document') pq = self.pq(email.body) review_document = self.item.latest_revision.reviewdocument_set.filter(reviewers__in=[participant]).first() invite_key = InviteKey.objects.get(matter=self.matter, invited_user=participant) expected_action_url = ABSOLUTE_BASE_URL(invite_key.get_absolute_url()) self.assertEqual(pq('a')[0].attrib.get('href'), expected_action_url) self.assertEqual(invite_key.next, reverse('request:list')) # test if activity shows in stream stream = target_stream(self.matter) self.assertEqual(stream[0].data['override_message'], u'Lawyër Tëst invited Participant Number 1 to review Revision v1 of Test Item with Revision')