コード例 #1
0
ファイル: writers.py プロジェクト: simalytics/askbot-devel
def answer(request, id, form_class=forms.AnswerForm):#process a new answer
    """view that posts new answer

    anonymous users post into anonymous storage
    and redirected to login page

    authenticated users post directly
    """
    question = get_object_or_404(models.Post, post_type='question', id=id)
    if request.method == "POST":

        #this check prevents backward compatilibility
        if form_class == forms.AnswerForm:
            custom_class_path = getattr(settings, 'ASKBOT_NEW_ANSWER_FORM', None)
            if custom_class_path:
                form_class = load_module(custom_class_path)
            else:
                form_class = forms.AnswerForm
        
        form = form_class(request.POST, user=request.user)

        if form.is_valid():
            if request.user.is_authenticated():
                # run slow task in celery, post-commit
                from askbot import tasks
                django_transaction_signals.defer(
                    tasks.remove_draft_answer.delay,
                    author=request.user,
                    thread=question.thread
                )
                try:
                    user = form.get_post_user(request.user)
                    answer = form.save(question, user)

                    signals.new_answer_posted.send(None,
                        answer=answer,
                        user=user,
                        form_data=form.cleaned_data
                    )

                    return HttpResponseRedirect(answer.get_absolute_url())
                except askbot_exceptions.AnswerAlreadyGiven, e:
                    request.user.message_set.create(message = unicode(e))
                    answer = question.thread.get_answers_by_user(request.user)[0]
                    return HttpResponseRedirect(answer.get_absolute_url())
                except exceptions.PermissionDenied, e:
                    request.user.message_set.create(message = unicode(e))
            else:
                request.session.flush()
                models.AnonymousAnswer.objects.create(
                    question=question,
                    wiki=form.cleaned_data['wiki'],
                    text=form.cleaned_data['text'],
                    session_key=request.session.session_key,
                    ip_addr=request.META['REMOTE_ADDR'],
                )
                return HttpResponseRedirect(url_utils.get_login_url())
コード例 #2
0
ファイル: storage.py プロジェクト: bwiklak/django-monitio
    def add(self, level, message, extra_tags='', subject='', user=None,
            from_user=None, expires=None, close_timeout=None,
            sse=False, email=False, url=None):
        """
        Adds or queues a message to the storage

        :param level: Level of the message
        :param message: Message text to be saved
        :param extra_tags: String with separated tags to add to message Ex: "secret classified"
        :param subject: Subject of the message 
        :param user: `auth.User` that receives the message
        :param from_user: `auth.User` that sends the message
        :param expires: Timestamp that indicates when the message expires
        :param close_timeout: Integer
        :param url: Optional string with URL leading to details of a given message

        .. note:: The message is only saved if it contains something and its level is over the recorded level (`MESSAGE_LEVEL`) `self.level`
        """
        to_user = user or get_user(self.request)
        if not to_user.is_authenticated():
            if Monit(level=level).is_persistent():
                raise NotImplementedError(
                    'Persistent message levels cannot be used for anonymous users.')
            else:
                return super(PersistentMessageStorage, self).add(level, message,
                                                                 extra_tags)
        if not message:
            return

        # Save the message only if its level is over the recorded level, see `MESSAGE_LEVEL` in Django docs
        level = int(level)
        if level < self.level:
            return

        # Add the message
        message = Monit(user=to_user, level=level, message=message,
                        extra_tags=extra_tags, subject=subject,
                        from_user=from_user, expires=expires,
                        close_timeout=close_timeout, url=url)

        # Messages need a primary key when being displayed so that they can be
        # closed/marked as read by the user. Hence, save it now instead of
        # adding it to queue:
        if STORE_WHEN_ADDING:
            message.save()

            if sse:
                # Sent delayed SSE notification
                defer(notify.via_sse, message.pk)

            if email:
                defer(notify.via_email, message.pk)

            return message
        else:
            self.added_new = True
            self._queued_messages.append(message)
コード例 #3
0
 def register_heartbeat(cls, obj):
     state = obj.state
     state.last_seen_on = timezone.now()
     state.last_contact_on = state.last_seen_on
     state.save()
     if state.value in (cls.CRASHED, cls.OFFLINE):
         opts = type(obj)._meta
         module = '%s.%s' % (opts.app_label, opts.object_name)
         defer(get_state.delay, module, ids=[obj.pk], lock=False)
     node_heartbeat.send(sender=cls, node=obj.state.get_node())
コード例 #4
0
    def put(self, tube_name, message, delay=0, connection=None):
        if not connection:
            connection = self.connection

        connection.use(tube_name)

        if settings.USE_TRANSACTION_SIGNALS:
            import django_transaction_signals
            django_transaction_signals.defer(
                self.__put,
                tube_name, message, delay=delay, connection=connection
            )
        else:
            self.__put(tube_name, message, delay=delay, connection=connection)
コード例 #5
0
 def save(self, *args, **kwargs):
     if not self.pk:
         # generate and initialize name
         if not self.name:
             self.name = self._name
         # Try to restore object to allow update in nested serialization
         try:
             obj = TincHost.objects.get(content_type_id=self.content_type_id,
                                        object_id=self.object_id)
         except TincHost.DoesNotExist:
             pass
         else:
             self.pk = obj.pk
     super(TincHost, self).save(*args, **kwargs)
     defer(update_tincd.delay)
コード例 #6
0
 def delete(self, *args, **kwargs):
     super(TincAddress, self).delete(*args, **kwargs)
     defer(update_tincd.delay)
コード例 #7
0
ファイル: writers.py プロジェクト: simalytics/askbot-devel
def ask(request):#view used to ask a new question
    """a view to ask a new question
    gives space for q title, body, tags and checkbox for to post as wiki

    user can start posting a question anonymously but then
    must login/register in order for the question go be shown
    """
    form = forms.AskForm(request.REQUEST, user=request.user)
    if request.method == 'POST':
        if form.is_valid():
            timestamp = datetime.datetime.now()
            title = form.cleaned_data['title']
            wiki = form.cleaned_data['wiki']
            tagnames = form.cleaned_data['tags']
            text = form.cleaned_data['text']
            ask_anonymously = form.cleaned_data['ask_anonymously']
            post_privately = form.cleaned_data['post_privately']
            group_id = form.cleaned_data.get('group_id', None)
            language = form.cleaned_data.get('language', None)

            if request.user.is_authenticated():
                # run slow task in celery, post-commit
                from askbot import tasks
                django_transaction_signals.defer(
                    tasks.remove_draft_question.delay,
                    author=request.user
                )
                # drafts = models.DraftQuestion.objects.filter(
                #                                 author=request.user
                #                             )
                # drafts.delete()

                user = form.get_post_user(request.user)
                try:
                    question = user.post_question(
                        title=title,
                        body_text=text,
                        tags=tagnames,
                        wiki=wiki,
                        is_anonymous=ask_anonymously,
                        is_private=post_privately,
                        timestamp=timestamp,
                        group_id=group_id,
                        language=language
                    )
                    signals.new_question_posted.send(None,
                        question=question,
                        user=user,
                        form_data=form.cleaned_data
                    )
                    return HttpResponseRedirect(question.get_absolute_url())
                except exceptions.PermissionDenied, e:
                    request.user.message_set.create(message = unicode(e))
                    return HttpResponseRedirect(reverse('index'))

            else:
                request.session.flush()
                session_key = request.session.session_key
                models.AnonymousQuestion.objects.create(
                    session_key = session_key,
                    title       = title,
                    tagnames = tagnames,
                    wiki = wiki,
                    is_anonymous = ask_anonymously,
                    text = text,
                    added_at = timestamp,
                    ip_addr = request.META['REMOTE_ADDR'],
                )
                return HttpResponseRedirect(url_utils.get_login_url())
コード例 #8
0
ファイル: transaction.py プロジェクト: Cgruppo/askbot-devel
def defer_celery_task(task, **kwargs):
    if django_settings.CELERY_ALWAYS_EAGER:
        return task.apply_async(**kwargs)
    else:
        return defer(task.apply_async, **kwargs)