示例#1
0
def answer_question(req, id):
    """
    View for accepting answers
    """

    profile = (
        DP.objects
          .prefetch_related('answers')
          .prefetch_related('answers__question')
          .get(user=req.user)
    )
    question = get_object_or_404(Question, pk=id)

    try:
        answer = profile.answers.get(question__id=id)
    except ObjectDoesNotExist:
        answer = Answer(question=question, profile=profile)

    form = AnswerForm(req.POST, instance=answer)
    if form.is_valid():
        answer = form.save(commit=True)
        Post.create_answer_post(answer)
        dispatch('ANSWER_QUESTION', profile, question=question)
        return HttpResponse('ok')
    return HttpResponse(str(form.errors), status=400)
示例#2
0
def _add_avatar(post_data, files, profile):
    form = AvatarForm(post_data, files)
    if form.is_valid():
        image = form.cleaned_data['image']
        image_url = form.cleaned_data['image_url']
        if image:
            photo = Photo.objects.create(profile=profile,
                                         image=image)
        else:
            result = urllib.request.urlretrieve(image_url)
            photo = Photo(profile=profile)
            photo.image.save(
                'random.jpg',
                File(open(result[0], 'rb'))
            )
            photo.save()
            os.remove(result[0])
        photo.add_thumbnail(form.cleaned_data['rel_top'],
                            form.cleaned_data['rel_left'],
                            form.cleaned_data['rel_width'],
                            form.cleaned_data['rel_height'])
        photo.add_small_thumbnail()
        photo.save()
        profile.avatar = photo
        profile.save()
        Post.create_photo_post(photo)
        dispatch('ADD_AVATAR', profile)
        return photo
    return None
示例#3
0
def add_avatar(req):
    profile = req.user.profile
    should_start_emulation = profile.avatar is None
    photo = _add_avatar(req.POST, req.FILES, profile)
    if not photo:
        return HttpResponse(status=400)
    if should_start_emulation:
        start_emulation(profile)
    dispatch('ADD_AVATAR', profile)
    return JsonResponse(
        srlzrs.AvatarSerializer.serialize(photo))
示例#4
0
def like(req, profile_id):
    profile = DP.objects.get(user=req.user)
    liked_profile = get_object_or_404(DP, pk=profile_id)
    try:
        Like.objects.get(liker=profile, liked=liked_profile)
        return HttpResponse('such like already exists', status=400)
    except ObjectDoesNotExist:
        like = Like(liker=profile, liked=liked_profile)
        like.save()
        dispatch('LIKE', profile, like=like)
        return JsonResponse({'id': like.id})
示例#5
0
def new_message(req, chat_id):
    profile = DP.objects.get(user=req.user)
    data = {
        'chat': chat_id,
        'sender': profile.id,
        'body': req.POST.get('body', None)
    }
    form = MessageForm(data)
    if form.is_valid():
        instance = form.save()
        dispatch('ADD_MESSAGE', profile, message=instance)
        return JsonResponse(
            MessageSerializer.serialize(instance, profile, instance.chat))
    return HttpResponse('', status=400)
示例#6
0
def create(req):
    MAX_PHOTOS = 5
    form = PostForm(req.POST, req.FILES, max_photos=MAX_PHOTOS)
    if form.is_valid():
        post = form.save(req.user.profile)
        if post.photos.count() > 0 and post.text:
            dispatch('ADD_POST', req.user.profile, post=post)
        return JsonResponse({
            'id': post.pk,
            'photos': list(map(
                lambda photo: photo.image.url,
                post.photos.all()
            ))
        })
    return HttpResponse(str(form.errors), status=400)
示例#7
0
def index(req):
    completed_task = dispatch('VISIT_NEWS', req.user.profile)
    return render(req, 'news/index.html', {
        'title': _('Новости'),
        'active': 'news',
        'completed_task': completed_task
    })
示例#8
0
def send_message(req):
    profile = req.user.profile
    try:
        receiver_id = int(req.POST.get('to', None))
    except ValueError:
        return HttpResponse('Receiver id is not int', status=400)
    receiver = get_object_or_404(DP, pk=receiver_id)
    chat = Chat.get_chat(profile1=profile, profile2=receiver)
    form = MessageForm({
        'body': req.POST.get('message', ''),
        'sender': profile.id,
        'chat': chat.id
    })
    if form.is_valid():
        msg = form.save()
        dispatch('ADD_MESSAGE', profile, message=msg)
        return HttpResponse('ok')
    return HttpResponse(str(form.errors), status=400)
示例#9
0
def file_upload(req):
    """
    View for photo uploading
    """

    form = PhotoForm(req.POST, req.FILES)
    if form.is_valid():
        photo = form.save(commit=False)
        photo.profile = DP.objects.get(user=req.user)
        photo.save()
        dispatch('ADD_PHOTO', req.user.profile, photo=photo)
        if form.cleaned_data['is_avatar']:
            photo.set_as_avatar()
            dispatch('ADD_AVATAR', profile)
    return JsonResponse({
        'id': photo.pk,
        'url': photo.image.url,
        'deleted': photo.is_deleted,
        'isAvatar': photo.is_avatar
    })
示例#10
0
def profile(req, profile_id=None):
    """
    View for profile page
    """
    profile_id = int(profile_id) if profile_id else req.user.profile.id
    try:
        profile = (
            DP.objects
              .prefetch_related('photos')
              .prefetch_related('answers')
              .prefetch_related('answers__question')
              .select_related('avatar')
              .get(pk=profile_id)
        )
    except ObjectDoesNotExist:
        raise Http404
    context = {
        'profile': profile,
        'own': profile.id == req.user.profile.id,
        'can_see_big_photos': req.user.has_perm('see_big_photos')
    }
    if context['own']:
        answered = []
        for answer in profile.answers.all():
            answered.append(answer.question.pk)
        query = ~Q(pk__in=answered)
        unanswered_questions = Question.objects.filter(query).all()
        context['questions'] = unanswered_questions
        context['looking_for_form'] = LookingForForm(instance=profile)
        context['char_form'] = CharacterForm(instance=profile)
        context['answer_form'] = AnswerForm()
        context['photo_form'] = PhotoForm()
        context['active'] = 'my_profile'
        context['title'] = _('Моя страница')
    else:
        context['encouraged'] = (
            Encourage.objects
            .filter(encourager=req.user.profile, encouraged=profile)
            .exists()
        )
        if not req.user.profile.is_invisible:
            Visit.objects.get_or_create(visitor=req.user.profile,
                                        visited=profile)
        VisitJournal.objects.create(visitor=req.user.profile,
                                    visited=profile)
        context['completed_task'] = dispatch('VISIT',
                                             req.user.profile)
        try:
            Like.objects.get(liker=req.user.profile, liked=profile)
            context['liked'] = True
        except ObjectDoesNotExist:
            context['liked'] = False
    return render(req, 'dateprofile/profile.html', context)
示例#11
0
def info_page(req):
    completed_task = dispatch('VISIT_SYMPATHIES', req.user.profile)
    sympathies = (Like.objects.filter(
        liker=req.user.profile, liker_deleted=False).select_related(
            'liked', 'liked__avatar').order_by('-created'))
    visits = (Visit.objects.filter(visited=req.user.profile,
                                   is_deleted=False).select_related(
                                       'visitor',
                                       'visitor__avatar').order_by('-created'))
    visits.update(notified=True)
    liked_me = (Like.objects.filter(liked=req.user.profile,
                                    liked_deleted=False).select_related(
                                        'liker',
                                        'liker__avatar').order_by('-created'))
    liked_me.update(notified=True)
    return render(
        req, 'likes/info.html', {
            'sympathies': sympathies,
            'visits': visits,
            'liked_me': liked_me,
            'active': 'sympathies',
            'title': _('Симпатии'),
            'completed_task': completed_task
        })