Example #1
0
File: times.py Project: boxed/curia
 def render(self, context):
     obj = resolve_parameter_from_context(self.obj, context)
     user = resolve_parameter_from_context('user', context)
     try:
         obj2 = resolve_parameter_from_context(self.obj2, context)
         
         try:
             obj2time2 = context['time']
         except KeyError:
             obj2time2 = None
             
         obj2time = get_time_from(obj2, user).last_viewed
         
         try:
             objtime = obj.get_last_changed_for_user(user)
         except:
             objtime = obj.creation_time
 
         if obj2time is None or objtime is None:
             return ''
     
         if obj2time <= objtime:
             return ' unread'
         elif obj2time2 != None and obj2time2 <= objtime:
             return ' unread2'
         else:
             return ''
         #return smart_unicode(obj2time)+' '+unicode(objtime)+unicode(obj2time <= objtime)
     except KeyError:
         if get_last_changed(obj) > get_time_from(obj, user):
             return 'unread'
         else:
             return ''
Example #2
0
File: times.py Project: boxed/curia
    def render(self, context):
        obj = resolve_parameter_from_context(self.obj, context)
        user = resolve_parameter_from_context('user', context)
        try:
            obj2 = resolve_parameter_from_context(self.obj2, context)

            try:
                obj2time2 = context['time']
            except KeyError:
                obj2time2 = None

            obj2time = get_time_from(obj2, user).last_viewed

            try:
                objtime = obj.get_last_changed_for_user(user)
            except:
                objtime = obj.creation_time

            if obj2time is None or objtime is None:
                return ''

            if obj2time <= objtime:
                return ' unread'
            elif obj2time2 != None and obj2time2 <= objtime:
                return ' unread2'
            else:
                return ''
            #return smart_unicode(obj2time)+' '+unicode(objtime)+unicode(obj2time <= objtime)
        except KeyError:
            if get_last_changed(obj) > get_time_from(obj, user):
                return 'unread'
            else:
                return ''
Example #3
0
File: views.py Project: boxed/curia
def edit_message(request, message_id):
    message = get_object_or_404(Message, pk=message_id)
    try:
        time = datetime_from_string(request.GET["time"])
    except KeyError:
        time = get_time_from(message.parent_thread, request.user).last_viewed

    import django.forms

    class EditForm(django.forms.Form):
        message = django.forms.CharField(widget=django.forms.Textarea, label=_("Message"))

    if message.owner == request.user:
        if request.method == "POST":
            form = EditForm(request.POST)

            if form.is_valid():
                import re

                replacer = re.compile("&nbsp;")
                body = form.cleaned_data["message"]
                body = replacer.sub(" ", body)
                message.body = body
                message.save()

                return HttpResponseRedirect(
                    message.parent_thread.get_absolute_url() + "?time=" + str(time) + "#firstnew"
                )
        else:
            form = EditForm(initial={"message": message.body})
    else:
        form = EditForm(initial={})
    return render_to_response(request, "forums/edit_message.html", {"form": form})
Example #4
0
def edit_message(request, message_id):
    message = get_object_or_404(Message, pk=message_id)
    try:
        time = datetime_from_string(request.GET['time'])
    except KeyError:
        time = get_time_from(message.parent_thread, request.user).last_viewed

    import django.forms

    class EditForm(django.forms.Form):
        message = django.forms.CharField(widget=django.forms.Textarea,
                                         label=_('Message'))

    if message.owner == request.user:
        if request.method == 'POST':
            form = EditForm(request.POST)

            if form.is_valid():
                import re
                replacer = re.compile('&nbsp;')
                body = form.cleaned_data['message']
                body = replacer.sub(' ', body)
                message.body = body
                message.save()

                return HttpResponseRedirect(
                    message.parent_thread.get_absolute_url() + '?time=' +
                    str(time) + '#firstnew')
        else:
            form = EditForm(initial={'message': message.body})
    else:
        form = EditForm(initial={})
    return render_to_response(request, 'forums/edit_message.html',
                              {'form': form})
Example #5
0
File: times.py Project: boxed/curia
    def render(self, context):
        obj = resolve_parameter_from_context(self.obj, context)
        user = resolve_parameter_from_context('user', context)

        if get_time_from(obj, user).bookmark:
            return self.nodelist_true.render(context)
        else:
            return self.nodelist_false.render(context)
Example #6
0
File: times.py Project: boxed/curia
    def render(self, context):
        obj = resolve_parameter_from_context(self.obj, context)
        user = resolve_parameter_from_context('user', context)

        if get_time_from(obj, user).bookmark:
            return self.nodelist_true.render(context)
        else:
            return self.nodelist_false.render(context)
Example #7
0
File: views.py Project: boxed/curia
def view_thread(request, thread_id, page=None):
    thread = get_object_or_404_and_check_access(request, Thread, pk=thread_id, command="view")
    page_size = get_integer(request, "page_size", default=12)
    orphans = 10

    try:
        set_time_on(thread, request.user, datetime_from_string(request.GET["set_time"]))
        set_time = False
    except KeyError:
        set_time = True

    last_viewed = get_time_from(thread, request.user).last_viewed
    try:
        time = datetime_from_string(request.GET["time"])
    except KeyError:
        time = last_viewed

    message_query = Message.objects.filter(parent_thread__pk=thread_id)
    show_deleted = get_boolean(request, "show_deleted")

    if not show_deleted:
        message_query = message_query.filter(deleted=show_deleted)

    threaded = get_boolean(request, "threaded", settings.THREADED_FORUMS)

    if threaded:
        message_order_by = "cache_hierarchy"
    else:
        message_order_by = "creation_time"

    paginator = Paginator(message_query.order_by(message_order_by), page_size, orphans=orphans)

    number_of_pages = paginator.num_pages
    got_page_size = False

    if number_of_pages != 0:
        if not page:
            page = number_of_pages
        page = int(page)

        # TODO: implement getting page ranges
        # end_page = int(request.GET['end_page'])
        end_page = page

        messages = paginator.page(page)
    else:
        end_page = 0
        page = 0
        messages = []

    next_page = page + 1
    has_next_page = paginator.page(page).has_next()

    messages = list(messages.object_list)
    if threaded:
        try:
            # get "first" (i.e. the upper most on the page) new message: the message with lowest cache_hierarchy, of the messages that have a timestamp set after "time"
            if time is None:
                time = datetime.fromordinal(1)
            first_message = Message.objects.filter(parent_thread=thread, creation_time__gt=time).order_by(
                message_order_by
            )[0]

            # if we didn't get an explicit request for a specific page and the current page does not include the "first" (i.e. the upper most on the page) unread message
            if not got_page_size:
                while first_message not in messages and page > 1:
                    # extend one page
                    end_page = page
                    page -= 1
                    tmp = messages
                    messages = paginator.page(page).object_list
                    messages.extend(tmp)

            # set firstnew on the first unread message in the page
            for message in messages:
                if time is None or message.creation_time >= last_viewed:
                    message.firstnew = True
                    break

        except IndexError:
            # for correct scrolling: set firstnew flag on the last message if there are no unread messages
            if len(messages) != 0:
                messages[-1].firstnew = True
    else:  # not threaded
        has_unread = False
        # set firstnew on the first unread message in the page
        for message in messages:
            if time is None or message.creation_time >= last_viewed:
                message.firstnew = True
                has_unread = True
                break

        # for correct scrolling: set firstnew flag on the last message if there are no unread messages
        if not has_unread:
            messages[-1].firstnew = True

    # If this is in a forum, get list of suggested_labels.
    suggested_label = None
    parents = None
    forum = False
    if thread.owner_group is None:
        forum = True
        parents = []
        try:
            suggested_label = request.GET["suggested_label"]
            tp = SuggestedLabel.objects.get(title=suggested_label)
        except (KeyError, SuggestedLabel.DoesNotExist):
            tp = None
        suggested_label = tp
        suggested_labels = get_objects_from(SuggestedLabel, deleted=False, parent=tp)

        if tp == None:
            pass
        else:
            if tp.parent:
                has_parent = True
                while has_parent:
                    parents.append(tp.parent)
                    m = tp.parent
                    if not m.parent:
                        has_parent = False
                    tp = tp.parent
                parents.reverse()

    # Add message
    import django.forms

    class EditForm(django.forms.Form):
        body = django.forms.CharField(widget=django.forms.Textarea, label=_("Message"))

    parent_message = None
    if request.REQUEST.has_key("parent_message_id") and request.REQUEST["parent_message_id"] != "":
        parent_message = Message.objects.get(pk=request.REQUEST["parent_message_id"])

    if request.method == "POST":
        form = EditForm(request.POST)
        check_access(
            request.user, command="add message", obj=thread
        )  # NOTE: same command in message.html and view_thread.html

        if form.is_valid():
            import re

            replacer = re.compile("&nbsp;")
            body = form.cleaned_data["body"]
            body = replacer.sub(" ", body)
            if body != "":
                Message.objects.create(
                    parent_thread=thread, parent_message=parent_message, owner=request.user, body=body
                )
                thread.last_changed_by = request.user
                thread.last_changed_time = datetime.now()
                thread.save()

                Thread.objects.filter(pk=thread.id).update(count=F("number_of_replies") + 1)

                set_time_on(thread, user=None)

            try:
                Watcher.objects.get(
                    object_id=thread.id, user=request.user, content_type=ContentType.objects.get(name="thread")
                )
            except Watcher.DoesNotExist:
                Watcher.objects.create(
                    user=request.user,
                    object_id=thread.id,
                    owner_user=request.user,
                    owner_group=request.community,
                    content_type=ContentType.objects.get(name="thread"),
                )

            return HttpResponseRedirect(thread.get_absolute_url() + "?time=" + str(time) + "#firstnew")

    else:
        form = EditForm(initial={})

    return render_to_response(
        request,
        "forums/view_thread.html",
        {
            "watch_object": thread,
            "thread": thread,
            "messages": messages,
            "number_of_pages": number_of_pages,
            "page": page,
            "end_page": end_page,
            "has_next_page": has_next_page,
            "has_previous_page": paginator.page(page).has_previous(),
            "next_page": next_page,
            "previous_page": page - 1,
            "page_size": page_size,
            "show_deleted": show_deleted,
            "time": time,
            "set_time": set_time,
            "owner": get_owner(thread),
            "suggested_label": suggested_label,
            "parents": parents,
            "forum": forum,
            "form": form,
            "is_paginated": True,
            "base_url": thread.get_absolute_url(),
            "threaded": threaded,
            "number_of_replies": thread.number_of_replies,
            "current_user": request.user,
            "paginator": paginator,
        },
    )
Example #8
0
def view_thread(request, thread_id, page=None):
    thread = get_object_or_404_and_check_access(request,
                                                Thread,
                                                pk=thread_id,
                                                command='view')
    page_size = get_integer(request, 'page_size', default=12)
    orphans = 10

    try:
        set_time_on(thread, request.user,
                    datetime_from_string(request.GET['set_time']))
        set_time = False
    except KeyError:
        set_time = True

    last_viewed = get_time_from(thread, request.user).last_viewed
    try:
        time = datetime_from_string(request.GET['time'])
    except KeyError:
        time = last_viewed

    message_query = Message.objects.filter(parent_thread__pk=thread_id)
    show_deleted = get_boolean(request, 'show_deleted')

    if not show_deleted:
        message_query = message_query.filter(deleted=show_deleted)

    threaded = get_boolean(request, 'threaded', settings.THREADED_FORUMS)

    if threaded:
        message_order_by = 'cache_hierarchy'
    else:
        message_order_by = 'creation_time'

    paginator = Paginator(message_query.order_by(message_order_by),
                          page_size,
                          orphans=orphans)

    number_of_pages = paginator.num_pages
    got_page_size = False

    if number_of_pages != 0:
        if not page:
            page = number_of_pages
        page = int(page)

        # TODO: implement getting page ranges
        #end_page = int(request.GET['end_page'])
        end_page = page

        messages = paginator.page(page)
    else:
        end_page = 0
        page = 0
        messages = []

    next_page = page + 1
    has_next_page = paginator.page(page).has_next()

    messages = list(messages.object_list)
    if threaded:
        try:
            # get "first" (i.e. the upper most on the page) new message: the message with lowest cache_hierarchy, of the messages that have a timestamp set after "time"
            if time is None:
                time = datetime.fromordinal(1)
            first_message = Message.objects.filter(
                parent_thread=thread,
                creation_time__gt=time).order_by(message_order_by)[0]

            # if we didn't get an explicit request for a specific page and the current page does not include the "first" (i.e. the upper most on the page) unread message
            if not got_page_size:
                while first_message not in messages and page > 1:
                    # extend one page
                    end_page = page
                    page -= 1
                    tmp = messages
                    messages = paginator.page(page).object_list
                    messages.extend(tmp)

            # set firstnew on the first unread message in the page
            for message in messages:
                if time is None or message.creation_time >= last_viewed:
                    message.firstnew = True
                    break

        except IndexError:
            # for correct scrolling: set firstnew flag on the last message if there are no unread messages
            if len(messages) != 0:
                messages[-1].firstnew = True
    else:  # not threaded
        has_unread = False
        # set firstnew on the first unread message in the page
        for message in messages:
            if time is None or message.creation_time >= last_viewed:
                message.firstnew = True
                has_unread = True
                break

        # for correct scrolling: set firstnew flag on the last message if there are no unread messages
        if not has_unread:
            messages[-1].firstnew = True

    # If this is in a forum, get list of suggested_labels.
    suggested_label = None
    parents = None
    forum = False
    if thread.owner_group is None:
        forum = True
        parents = []
        try:
            suggested_label = request.GET['suggested_label']
            tp = SuggestedLabel.objects.get(title=suggested_label)
        except (KeyError, SuggestedLabel.DoesNotExist):
            tp = None
        suggested_label = tp
        suggested_labels = get_objects_from(SuggestedLabel,
                                            deleted=False,
                                            parent=tp)

        if tp == None:
            pass
        else:
            if tp.parent:
                has_parent = True
                while has_parent:
                    parents.append(tp.parent)
                    m = tp.parent
                    if not m.parent:
                        has_parent = False
                    tp = tp.parent
                parents.reverse()

    #Add message
    import django.forms

    class EditForm(django.forms.Form):
        body = django.forms.CharField(widget=django.forms.Textarea,
                                      label=_('Message'))

    parent_message = None
    if request.REQUEST.has_key('parent_message_id') and request.REQUEST[
            'parent_message_id'] != '':
        parent_message = Message.objects.get(
            pk=request.REQUEST['parent_message_id'])

    if request.method == 'POST':
        form = EditForm(request.POST)
        check_access(
            request.user, command='add message', obj=thread
        )  # NOTE: same command in message.html and view_thread.html

        if form.is_valid():
            import re
            replacer = re.compile('&nbsp;')
            body = form.cleaned_data['body']
            body = replacer.sub(' ', body)
            if body != '':
                Message.objects.create(parent_thread=thread,
                                       parent_message=parent_message,
                                       owner=request.user,
                                       body=body)
                thread.last_changed_by = request.user
                thread.last_changed_time = datetime.now()
                thread.save()

                Thread.objects.filter(pk=thread.id).update(
                    count=F('number_of_replies') + 1)

                set_time_on(thread, user=None)

            try:
                Watcher.objects.get(
                    object_id=thread.id,
                    user=request.user,
                    content_type=ContentType.objects.get(name="thread"))
            except Watcher.DoesNotExist:
                Watcher.objects.create(
                    user=request.user,
                    object_id=thread.id,
                    owner_user=request.user,
                    owner_group=request.community,
                    content_type=ContentType.objects.get(name="thread"))

            return HttpResponseRedirect(thread.get_absolute_url() + '?time=' +
                                        str(time) + '#firstnew')

    else:
        form = EditForm(initial={})

    return render_to_response(
        request, 'forums/view_thread.html', {
            'watch_object': thread,
            'thread': thread,
            'messages': messages,
            'number_of_pages': number_of_pages,
            'page': page,
            'end_page': end_page,
            'has_next_page': has_next_page,
            'has_previous_page': paginator.page(page).has_previous(),
            'next_page': next_page,
            'previous_page': page - 1,
            'page_size': page_size,
            'show_deleted': show_deleted,
            'time': time,
            'set_time': set_time,
            'owner': get_owner(thread),
            'suggested_label': suggested_label,
            'parents': parents,
            'forum': forum,
            'form': form,
            'is_paginated': True,
            'base_url': thread.get_absolute_url(),
            'threaded': threaded,
            'number_of_replies': thread.number_of_replies,
            'current_user': request.user,
            'paginator': paginator
        })