示例#1
0
def village(request, student_id):
    """The main chat view for a student/village."""
    try:
        rel = get_relationship_or_404(student_id, request.user.profile)
    except http.Http404:
        if not request.user.is_superuser:
            raise
        rel = None
        student = get_object_or_404(model.Profile, pk=student_id)
    else:
        student = rel.student

    group = get_querystring_group(request, student)

    posts = _get_posts(request.user.profile, student=student)

    if rel and not request.impersonating:
        model.unread.mark_village_read(rel.student, rel.elder)

    return TemplateResponse(
        request,
        'village/post_list/village.html',
        {
            'student': student,
            'group': group,
            'relationship': rel,
            'elders': model.contextualized_elders(
                student.elder_relationships).order_by('school_staff', 'name'),
            'read_only': rel is None,
            'posts': posts,
            'post_char_limit': model.post_char_limit(rel) if rel else 0,
            'posting_url': reverse(
                'create_post', kwargs={'student_id': student.id}),
            },
        )
示例#2
0
def group(request, group_id=None):
    """The main chat view for a group."""
    if group_id is None:
        group = model.AllStudentsGroup(request.user.profile)
        posting_url = reverse('create_post')
    else:
        group = get_object_or_404(
            model.Group.objects.filter(owner=request.user.profile),
            id=group_id,
            )
        posting_url = reverse('create_post', kwargs={'group_id': group.id})

    return TemplateResponse(
        request,
        'village/post_list/group.html',
        {
            'group': group,
            'elders': model.contextualized_elders(
                group.all_elders).order_by('school_staff', 'name'),
            'posts': _get_posts(request.user.profile, group=group),
            'post_char_limit': model.post_char_limit(request.user.profile),
            'posting_url': posting_url,
            },
        )
示例#3
0
def create_post(request, student_id=None, group_id=None):
    """
    Create a post.

    If ``student_id`` is provided in the URL, the post will be a single-village
    post. If ``group_id`` is provided, it will be a group bulk post. If neither
    is provided, it will be an all-students bulk post.

    POST parameters accepted:

    ``text``

        The text of the post to create. Must be few enough characters that,
        when the user's auto-signature is appended, the resulting full SMS
        message is <160 characters.

    ``type``

        The type of post to create: "message", "note", "call", or
        "meeting". This parameter is ignored for bulk posts; all bulk posts are
        of type "message".

    ``elder``

        A list of elder IDs connected with this post. For a "message" type
        post, these users will receive the post via SMS. For a "meeting" or
        "call" type post, these are the users who were present on the call or
        at the meeting.

    ``extra_name``

       A list of additional names connected with this post. (For instance, for
       a "meeting" or "call" type post, these are names of additional people
       present at the meeting or on the call, who are not actually elders in
       the village.)

    ``author_sequence_id``

       An increasing numeric ID for posts authored by this user in this browser
       session. This value is opaque to the server and not stored anywhere, but
       is round-tripped through Pusher back to the client, to simplify
       matching up post data and avoid creating duplicates on the client.

    For non-bulk posts, an ``attachment`` file-upload parameter is also
    optionally accepted.

    Returns JSON object with boolean key ``success``. If ``success`` is
    ``False``, a human-readable message will be provided in the ``error``
    key. If ``success`` is ``True``, the ``objects`` key will be a list
    containing one JSON-serialized post object. (Even though this view will
    only ever return one post, it still returns a list for better compatibility
    with other client-side JSON-handling code.)

    """
    if 'text' not in request.POST:
        return http.HttpResponseBadRequest(
            json.dumps(
                {
                    'error': "Must provide a 'text' querystring parameter.",
                    'success': False,
                    }
                ),
            content_type='application/json',
            )

    extra_kwargs = {}
    group = None
    rel = None
    post_model = model.BulkPost
    profile_ids = 'all'
    if student_id is not None:
        rel = get_relationship_or_404(student_id, request.user.profile)
        post_model = model.Post
        target = rel.student
        profile_ids = request.POST.getlist('elder')
        extra_kwargs['extra_names'] = request.POST.getlist('extra_name')
        extra_kwargs['post_type'] = request.POST.get('type')
        if 'attachment' in request.FILES:
            extra_kwargs['attachments'] = request.FILES.getlist('attachment')
        redirect_url = reverse('village', kwargs={'student_id': student_id})
        qs_group = get_querystring_group(request, rel.student)
        if qs_group:
            redirect_url += "?group=%s" % qs_group.id
    elif group_id is not None:
        group = get_object_or_404(
            model.Group.objects.filter(owner=request.user.profile), pk=group_id)
        target = group
        redirect_url = reverse('group', kwargs={'group_id': group_id})
    else:
        target = None
        redirect_url = reverse('all_students')

    text = request.POST['text']
    sequence_id = request.POST.get('author_sequence_id')
    limit = model.post_char_limit(rel or request.user.profile)
    if len(text) > limit:
        return http.HttpResponseBadRequest(
            json.dumps(
                {
                    'error': 'Posts are limited to %s characters.' % limit,
                    'success': False,
                    }
                ),
            content_type='application/json',
            )

    with xact.xact():
        post = post_model.create(
            request.user.profile,
            target,
            text,
            profile_ids=profile_ids,
            sequence_id=sequence_id,
            **extra_kwargs)

    if request.is_ajax():
        data = {
            'success': True,
            'objects': [
                serializers.post2dict(
                    post, author_sequence_id=sequence_id, unread=False, mine=True)
                ],
            }

        return http.HttpResponse(
            json.dumps(data), content_type='application/json')
    else:
        return http.HttpResponseRedirect(redirect_url)