def mutate_and_get_payload(self, info, name, object_id, comment, targets): if not '.' in name: raise SngyException('Неправильное имя') app_label, model = name.lower().split('.') content_type = ContentType(app_label=app_label, model=model) content_object = content_type.get_object_for_this_type(id=object_id) Comment.objects.create(content_object=content_object, user=info.context.user, comment=comment) users_id = [u.id for u in User.objects.all()] if not targets: targets = list( set([ c.user_id for c in Comment.objects.filter( content_type__app_label=app_label, content_type__model=model, object_id=object_id) ])) if info.context.user.id in targets: targets.remove(info.context.user.id) for t in targets: if t in users_id: text = 'Комментарий: ' + comment Notification.objects.create(purpose_id=t, created=info.context.user, text=text) return CreateComments(result=True)
def mooclet_create(request, **kwargs): ''' create a new mooclet required kwargs: quiz, type, parent object ''' quiz = get_object_or_404(Quiz,pk=kwargs['quiz_id']) mooclet_type = get_object_or_404(MoocletType,name=kwargs['type']) # object class that the mooclet is attached to parent_content_type = mooclet_type.content_type parent_content = ContentType.get_object_for_this_type(parent_content_type, pk=kwargs[parent_content_type.name+'_id']) if request.method == 'GET': mooclet_form = MoocletForm(initial={'type':mooclet_type}) context = { 'quiz':quiz, 'mooclet_form':mooclet_form, } if 'question' in kwargs: context['question'] = get_object_or_404(Question,pk=kwargs['question_id']) if 'answer' in kwargs: context['answer'] = get_object_or_404(Answer,pk=kwargs['answer_id']) return render(request, 'engine/mooclet_create.html', context) elif request.method == 'POST': mooclet_form = MoocletForm(request.POST) mooclet = mooclet_form.save() prev_url = request.POST['prev'] return redirect(prev_url)
def create_vote(self, vote): content_type = ContentType(app_label=self.app, model=self.model) content_object = content_type.get_object_for_this_type( id=self.instance.id) return Vote.objects.create(vote=vote, content_object=content_object, user_id=self.user_id)
def get_descr(self): cache_key = self.get_descr_cache_key() descr = cache.get(cache_key) if not descr: descr = ContentType.get_object_for_this_type( self.content_type, id=self.object_id).get_descr() cache.set(cache_key, descr, 5) return descr
def execute(self): app_label, model = self.model_name.lower().split('.') content_type = ContentType(app_label=app_label, model=model) content_object = content_type.get_object_for_this_type(id=self.instance_id) return Comment.objects.create( content_object=content_object, user_id=self.user_id, text=self.text, parent_id=self.parent_id )
def mooclet_detail(request, **kwargs): ''' mooclet home page required kwargs: quiz, mooclet, and all parent objects ''' quiz = get_object_or_404(Quiz,pk=kwargs['quiz_id']) mooclet = get_object_or_404(Mooclet,pk=kwargs['mooclet_id']) # look up mooclet type and identify associated parent object # object class that the mooclet is attached to parent_content_type = mooclet.type.parent_content_type parent_content = ContentType.get_object_for_this_type(parent_content_type, pk=kwargs[parent_content_type.name+'_id']) # populate a mooclet context dict mooclet_context = {} if parent_content_type.name == 'question': mooclet_context['question'] = parent_content if parent_content_type.name == 'answer': mooclet_context['answer'] = parent_content mooclet_context['question'] = parent_content.question versions = mooclet.version_set.all() version_content_type = ContentType.objects.get_for_model(Version) mooclet_policy_form = MoocletPolicyForm(instance=mooclet) # page used to display variables that are version-specific if request.method == 'GET': context = { 'quiz':quiz, 'mooclet':mooclet, 'versions':versions, 'mooclet_policy_form':mooclet_policy_form, } # pass additional context variables for navigation context.update(mooclet_context) if 'question' in kwargs: context['question'] = get_object_or_404(Question,pk=kwargs['question_id']) if 'answer' in kwargs: context['answer'] = get_object_or_404(Answer,pk=kwargs['answer_id']) return render(request, 'engine/mooclet_detail.html',context) elif request.method == 'POST': # process mooclet policy form for adjusting policy mooclet_policy_form = MoocletPolicyForm(request.POST, instance=mooclet) mooclet = mooclet_policy_form.save() # converts dict of objs to dict of pks mooclet_context_pk = {name+'_id':obj.pk for name,obj in mooclet_context.items()} return redirect('engine:mooclet_detail', quiz_id=quiz.pk, mooclet_id=mooclet.pk, **mooclet_context_pk)
def get_mooclet_context(mooclet): # content_type for model that the mooclet is attached to parent_content_type = mooclet.type.content_type # parent object instance parent_content = ContentType.get_object_for_this_type(parent_content_type, pk=parent_content_id) if parent_content_type.name == 'question': question = parent_content elif parent_content_type.name == 'answer': answer = parent_content question = answer.question
def get_mooclet_context(mooclet): # content_type for model that the mooclet is attached to parent_content_type = mooclet.type.content_type # parent object instance parent_content = ContentType.get_object_for_this_type(parent_content_type, pk=parent_content_id) if parent_content_type.name == 'question': question = parent_content elif parent_content_type.name == 'answer': answer = parent_content question = answer.question
def repopulate_timeline_content(content_type: ContentType, object_id, user_id, action): """Based on the follow/unfollow action. Only repopulates if a User object is being followed or unfollowed. Tags are TODO. """ def push_action_in_user_feed(user, action): # Push activities if they don't exist if user.feed_entries.filter(action_id=action.pk).exists(): log.debug('Skipping existing activity in user feed') return else: log.debug('Populating timeline with action %i' % action.pk) user.feed_entries.create( category='timeline', action=action, ) def pull_action_from_user_feed(user, action): # Do a lookup on the current user's feed, and remove any matching activity found. try: log.debug('Removing action %i from user %i feed' % (action.pk, user.pk)) user.feed_entries.filter(action_id=action.pk).delete() except dillo.models.feeds.FeedEntry.DoesNotExist: pass content_type_class = content_type.model_class() try: target = content_type.get_object_for_this_type(pk=object_id) except content_type_class.DoesNotExist: log.debug("Skipping timeline repopulation, content was deleted") return # If follow User if action == 'follow' and isinstance(target, User): # If following user, get 10 posts and check if their creation activity is already in the # users timeline feed. If not, add push to the timeline actions = models_actstream.Action.objects.filter( verb='posted', actor_object_id=target.pk)[:10] user = User.objects.get(pk=user_id) for action in actions: push_action_in_user_feed(user, action) elif action == 'follow' and isinstance(target, models_taggit.Tag): # Get 10 posts with that tag posts = dillo.models.posts.Post.objects.filter( tags__name__in=[target.name])[:10] for post in posts: # Find post action (get only the first, as the same post could be # connected with multiple tags) action = models_actstream.Action.objects.filter( verb='posted', action_object_object_id=post.pk).first() push_action_in_user_feed(User.objects.get(pk=user_id), action) # If unfollow User elif action == 'unfollow' and isinstance(target, User): # Fetch all actions from the unfollowed users actions = models_actstream.Action.objects.filter( verb='posted', actor_object_id=target.pk).all() # Fetch current user user = User.objects.get(pk=user_id) for action in actions: pull_action_from_user_feed(user, action) elif action == 'unfollow' and isinstance(target, models_taggit.Tag): # Get the latest 10 posts with that tag posts = dillo.models.posts.Post.objects.filter( tags__name__in=[target.name]).order_by('-created_at')[:10] for post in posts: # Find post action action = models_actstream.Action.objects.get( verb='posted', action_object_object_id=post.pk) user = User.objects.get(pk=user_id) pull_action_from_user_feed(user, action)