Пример #1
0
    def activation_key_expired(self):
        """
        Checks if activation key is expired.

        Returns ``True`` when the ``activation_key`` of the user is expired and
        ``False`` if the key is still valid.

        The key is expired when it's set to the value defined in
        ``ACCOUNTS_ACTIVATED`` or ``activation_key_created`` is beyond the
        amount of days defined in ``ACCOUNTS_ACTIVATION_DAYS``.

        """
        expiration_days = datetime.timedelta(days=accounts_settings.ACCOUNTS_ACTIVATION_DAYS)
        expiration_date = self.user.date_joined + expiration_days
        if self.activation_key == accounts_settings.ACCOUNTS_ACTIVATED:
            return True
        if get_datetime_now() >= expiration_date:
            return True
        return False
Пример #2
0
    def change_email(self, email):
        """
        Changes the email address for a user.

        A user needs to verify this new email address before it becomes
        active. By storing the new email address in a temporary field --
        ``temporary_email`` -- we are able to set this email address after the
        user has verified it by clicking on the verification URI in the email.
        This email gets send out by ``send_verification_email``.

        :param email:
            The new email address that the user wants to use.

        """
        self.email_unconfirmed = email

        salt, hash = generate_sha1(self.user.username)
        self.email_confirmation_key = hash
        self.email_confirmation_key_created = get_datetime_now()
        self.save()

        # Send email for activation
        self.send_confirmation_email()
Пример #3
0
def message_detail(
    request,
    username,
    page=1,
    paginate_by=10,
    template_name="umessages/message_detail.html",
    extra_context=None,
    **kwargs
):
    """
    Returns a conversation between two users

    :param username:
        String containing the username of :class:`User` of whom the
        conversation is with.

    :param page:
        Integer of the active page used for pagination. Defaults to the first
        page.

    :param paginate_by:
        Integer defining the amount of displayed messages per page.
        Defaults to 50 messages per per page.

    :param extra_context:
        Dictionary of variables that will be made available to the template.

    :param template_name:
        String of the template that is rendered to display this view.

    If the result is paginated, the context will also contain the following
    variables.

    ``paginator``
        An instance of ``django.core.paginator.Paginator``.

    ``page_obj``
        An instance of ``django.core.paginator.Page``.

    """
    recipient = get_object_or_404(User, username__iexact=username)
    queryset = Message.objects.get_conversation_between(request.user, recipient)

    # Update all the messages that are unread.
    message_pks = [m.pk for m in queryset]
    unread_list = MessageRecipient.objects.filter(message__in=message_pks, user=request.user, read_at__isnull=True)
    now = get_datetime_now()
    unread_list.update(read_at=now)

    if not extra_context:
        extra_context = dict()
    extra_context["recipient"] = recipient
    return list_detail.object_list(
        request,
        queryset=queryset,
        paginate_by=paginate_by,
        page=page,
        template_name=template_name,
        extra_context=extra_context,
        template_object_name="message",
        **kwargs
    )
Пример #4
0
def message_remove(request, undo=False):
    """
    A ``POST`` to remove messages.

    :param undo:
        A Boolean that if ``True`` unremoves messages.

    POST can have the following keys:

        ``message_pks``
            List of message id's that should be deleted.

        ``next``
            String containing the URI which to redirect to after the keys are
            removed. Redirect defaults to the inbox view.

    The ``next`` value can also be supplied in the URI with ``?next=<value>``.

    """
    message_pks = request.POST.getlist("message_pks")
    redirect_to = request.REQUEST.get("next", False)

    if message_pks:
        # Check that all values are integers.
        valid_message_pk_list = set()
        for pk in message_pks:
            try:
                valid_pk = int(pk)
            except (TypeError, ValueError):
                pass
            else:
                valid_message_pk_list.add(valid_pk)

        # Delete all the messages, if they belong to the user.
        now = get_datetime_now()
        changed_message_list = set()
        for pk in valid_message_pk_list:
            message = get_object_or_404(Message, pk=pk)

            # Check if the user is the owner
            if message.sender == request.user:
                if undo:
                    message.sender_deleted_at = None
                else:
                    message.sender_deleted_at = now
                message.save()
                changed_message_list.add(message.pk)

            # Check if the user is a recipient of the message
            if request.user in message.recipients.all():
                mr = message.messagerecipient_set.get(user=request.user, message=message)
                if undo:
                    mr.deleted_at = None
                else:
                    mr.deleted_at = now
                mr.save()
                changed_message_list.add(message.pk)

        # Send messages
        if (len(changed_message_list) > 0) and accounts_settings.ACCOUNTS_USE_MESSAGES:
            if undo:
                message = ungettext(
                    "Message is succesfully restored.", "Messages are succesfully restored.", len(changed_message_list)
                )
            else:
                message = ungettext(
                    "Message is successfully removed.", "Messages are successfully removed.", len(changed_message_list)
                )

            messages.success(request, message, fail_silently=True)

    if redirect_to:
        return redirect(redirect_to)
    else:
        return redirect(reverse("accounts_umessages_list"))