示例#1
0
文件: views.py 项目: Bruce94/Musician
def musician_friends(request, user_id):
    user = get_object_or_404(User, pk=user_id)

    n_req = Friend.n_req_friendship(request.user)
    user_distance = request.user.musicianprofile.musician_distance(user.musicianprofile)

    status_friend = 0
    reciver = None
    if Friend.get_friendship(request.user, user):
        fs = Friend.get_friendship(request.user, user)
        reciver = fs.reciver
        status_friend = fs.status

    friend_user = Friend.get_user_friends(user)
    fu = {}
    for f in friend_user:
        fu[f] = f.get_n_common_friend(request.user)
    fu = sorted(fu.items(), key=lambda x: x[1], reverse=True)
    return render(request, 'musician/musician_friends.html', {'user': user,
                                                              'reciver': reciver,
                                                              'fu': fu,
                                                              'status_friend': status_friend,
                                                              'n_req': n_req,
                                                              'user_distance': user_distance
                                                              })
示例#2
0
def friendship_request(request):
    friendships = Friend.get_pending_request(request.user)

    musicians = MusicianProfile.objects.all()
    profile_list = []

    if friendships:
        for friendship in friendships:
            if musicians.filter(Q(user__username=friendship.sender.username)):
                profile_list += list(
                    musicians.filter(
                        Q(user__username=friendship.sender.username)))
                friendship.seen = True
                friendship.save()

    n_req = Friend.n_req_friendship(request.user)
    return render(
        request,
        'portal/friendship_request.html',
        {
            'profile_list': profile_list,
            'n_req': n_req,
            #'n_mes': n_mes,
            #'n_comm': n_comm
        })
示例#3
0
文件: views.py 项目: Bruce94/Musician
def update_suggestions(request, user_id):
    user = get_object_or_404(User, pk=user_id)
    remove_friend_from_suggested(request.user.musicianprofile, user.id)
    remove_friend_from_suggested(user.musicianprofile, request.user.id)
    ul_friends = set(Friend.get_user_friends(request.user))
    us_friends = set(Friend.get_user_friends(user))
    users = ul_friends | us_friends | set([user.musicianprofile]) | set([request.user.musicianprofile])
    for u in users:
        u.update_suggested_musician()
示例#4
0
文件: views.py 项目: Bruce94/Musician
def profile(request, user_id):
    user = get_object_or_404(User, pk=user_id)

    n_req = Friend.n_req_friendship(request.user)
    user_distance = request.user.musicianprofile.musician_distance(user.musicianprofile)

    voted_posts = Preference.objects.filter(musician_profile=request.user.musicianprofile)

    status_friend = 0
    reciver = None

    if Friend.get_friendship(request.user, user):
        fs = Friend.get_friendship(request.user, user)
        reciver = fs.reciver
        status_friend = fs.status

    if request.method == 'POST':
        if "add-friend" in request.POST:
            friend = Friend.create(sender=request.user, reciver=user)
            friend.save()
            status_friend = 1
            reciver = user
        elif "cancel-request" in request.POST:
            fs.delete()
            status_friend = 0
            _thread.start_new_thread(update_suggestions, (request, user_id))
        elif "accept-friend" in request.POST:
            fs.status = 2
            fs.save()
            status_friend = 2
            _thread.start_new_thread(update_suggestions, (request, user_id))
        elif "send-message" in request.POST:
            return HttpResponseRedirect('/musician/messages/'+str(user.id))

        for post in user.musicianprofile.user_post.all():
            if request.POST.get('del_' + str(post.id)):
                post.delete()
                return HttpResponseRedirect('/musician/'+str(user.id))

            if request.POST.get('comment_' + str(post.id)):

                post.comment_set.create(comment_text=request.POST['comment_' + str(post.id)],
                                        musician_profile=request.user.musicianprofile,
                                        seen=(True if post.musician_profile == request.user.musicianprofile else False))
                comment = post.comment_set.filter(comment_text=request.POST['comment_' + str(post.id)],
                                               musician_profile=request.user.musicianprofile)[0]
                comment.checkForTagsInComment()

    return render(request, 'musician/profile.html', {'user': user,
                                                     'reciver': reciver,
                                                     'status_friend': status_friend,
                                                     'n_req': n_req,
                                                     'user_distance': user_distance,
                                                     'voted_posts': voted_posts
                                                     })
示例#5
0
文件: views.py 项目: Bruce94/Musician
def messages(request):
    n_req = Friend.n_req_friendship(request.user)

    friend_user = Friend.get_user_friends(request.user)
    friend_user.sort(key=lambda x: x.user.first_name, reverse=False)

    new_mes_user = Message.new_user_message(request.user)

    return render(request, 'musician/messages.html', {'n_req': n_req,
                                                      'friend_user': friend_user,
                                                      'new_mes_user': new_mes_user
                                                      })
示例#6
0
文件: views.py 项目: Bruce94/Musician
def chat(request, user_id):
    user = get_object_or_404(User, pk=user_id)

    n_req = Friend.n_req_friendship(request.user)
    messages_of_chat = Message.messages_of_chat(request.user, user)

    for message in messages_of_chat:
        if message.reciver_message.id == request.user.id:
            message.seen = True
            message.save()

    friend_user = Friend.get_user_friends(request.user)
    friend_user.sort(key=lambda x: x.user.first_name, reverse=False)
    new_mes_user = Message.new_user_message(request.user)

    return render(request, 'musician/chat.html', {'n_req': n_req,
                                                  'friend_user': friend_user,
                                                  'selected_user': user,
                                                  'messages': messages_of_chat,
                                                  'new_mes_user': new_mes_user,
                                                })
示例#7
0
def portal_welcome(request):
    user = get_object_or_404(User, pk=request.user.id)
    n_req = Friend.n_req_friendship(request.user)
    n_first_neigh = len(Friend.get_user_friends(request.user))

    user_friends = Friend.get_user_friends(user)
    home_posts = Post.objects.filter(
        Q(musician_profile__in=user_friends)
        | Q(musician_profile=user.musicianprofile))

    voted_posts = Preference.objects.filter(
        musician_profile=request.user.musicianprofile)

    for p in home_posts:
        if request.POST.get('comment_' + str(p.id)):
            p.comment_set.create(
                comment_text=request.POST['comment_' + str(p.id)],
                musician_profile=request.user.musicianprofile,
                seen=(True if p.musician_profile
                      == request.user.musicianprofile else False))

            comment = p.comment_set.filter(
                comment_text=request.POST['comment_' + str(p.id)],
                musician_profile=request.user.musicianprofile)[0]
            comment.checkForTagsInComment()

    for p in user.musicianprofile.user_post.all():
        if request.POST.get('del_' + str(p.id)):
            p.delete()
            return HttpResponseRedirect('/portal/')

    return render(
        request, 'portal/home.html', {
            'user': user,
            'home_posts': home_posts,
            'n_req': n_req,
            'n_first_neigh': n_first_neigh,
            'voted_posts': voted_posts,
        })
示例#8
0
def tag_post(request, tag_text):
    posts = Tag.posts_and_comments_with_tag(tag_text)

    user = get_object_or_404(User, pk=request.user.id)
    n_req = Friend.n_req_friendship(request.user)
    #n_mes = Message.n_new_messages(request.user)
    #n_comm = Post.n_new_comments(request.user.musicianprofile)
    n_first_neigh = len(Friend.get_user_friends(request.user))

    user_friends = Friend.get_user_friends(user)

    for p in posts:
        if request.POST.get('comment_' + str(p.id)):
            p.comment_set.create(
                comment_text=request.POST['comment_' + str(p.id)],
                musician_profile=request.user.musicianprofile,
                seen=(True if p.musician_profile
                      == request.user.musicianprofile else False))

            comment = p.comment_set.filter(
                comment_text=request.POST['comment_' + str(p.id)],
                musician_profile=request.user.musicianprofile)[0]
            comment.checkForTagsInComment()

    return render(
        request,
        'portal/tagged_post.html',
        {
            'user': user,
            'home_posts': posts,
            'n_req': n_req,
            #'n_mes': n_mes,
            #'n_comm': n_comm,
            'n_first_neigh': n_first_neigh,
            'tag_text': tag_text
        })
示例#9
0
def comment_notifications(request):
    n_req = Friend.n_req_friendship(request.user)

    new_comments = Comment.objects.filter(
        post__musician_profile=request.user.musicianprofile, seen=False)
    new_votes = Preference.objects.filter(
        post__musician_profile=request.user.musicianprofile, seen=False)

    return render(
        request,
        'portal/comment_notifications.html',
        {
            'n_req': n_req,
            #'n_mes': n_mes,
            #'n_comm': n_comm,
            'new_votes': new_votes,
            'new_comments': new_comments
        })
示例#10
0
def view_post_notificated(request, post_id):
    p = get_object_or_404(Post, pk=post_id)
    n_req = Friend.n_req_friendship(request.user)

    for comment in p.comment_set.all():
        comment.seen = True
        comment.save()

    for vote in p.preference_set.all():
        vote.seen = True
        vote.save()

    return render(
        request,
        'portal/post.html',
        {
            'n_req': n_req,
            #'n_mes': n_mes,
            #'n_comm': n_comm,
            'post': p
        })
示例#11
0
def newpost_get(request):
    user_friends = Friend.get_user_friends(request.user)
    home_post = Post.objects.filter(
        Q(musician_profile__in=user_friends)
        | Q(musician_profile=request.user.musicianprofile)).__getitem__(0)

    post_data = {}

    post_data['request_user_img_url'] = request.user.musicianprofile.img.url
    post_data['user_image_url'] = home_post.musician_profile.img.url
    post_data['user_firstname'] = home_post.musician_profile.user.first_name
    post_data['user_lastname'] = home_post.musician_profile.user.last_name
    post_data['user_post_id'] = home_post.musician_profile.user.id
    post_data['post_id'] = home_post.id
    post_data['post_n_like'] = home_post.n_like
    post_data['post_n_dislike'] = home_post.n_dislike
    post_data['text'] = home_post.post_text
    if home_post.post_image:
        post_data['img_url'] = home_post.post_image.url
    post_data['pub_date'] = str(home_post.pub_date.strftime("%Y-%m-%d %H:%M"))
    post_data['comm'] = []

    for comm in home_post.comment_set.all():
        comment_data = {}
        comment_data['user_id'] = comm.musician_profile.user.id
        comment_data['user_firstname'] = comm.musician_profile.user.first_name
        comment_data['user_lastname'] = comm.musician_profile.user.last_name
        comment_data['user_image_url'] = comm.musician_profile.img.url
        comment_data['pub_date'] = str(
            comm.pub_date.strftime("%Y-%m-%d %H:%M"))
        comment_data['text'] = comm.comment_text
        post_data['comm'].append(comment_data)

    response = {'home_posts': post_data}
    r = json.dumps(response, False)
    return JsonResponse(r, safe=False)
示例#12
0
def search_musician(request):
    profile_list = []
    checked_skills = []
    query = ""
    n_req = Friend.n_req_friendship(request.user)

    mpf = MusicianProfileForm(prefix='profile')

    skills = Skill.objects.all()

    if request.method == 'GET':
        mpf = MusicianProfileForm(request.GET, request.FILES, prefix='profile')

        query = request.GET.get('search_musician')

        if query:
            for q in query.split():
                profile_list = set(
                    chain(
                        MusicianProfile.get_musician(
                            user__username__icontains=q),
                        MusicianProfile.get_musician(
                            user__first_name__icontains=q),
                        MusicianProfile.get_musician(
                            user__last_name__icontains=q)))

        if request.GET.getlist('check_skill'):
            checked_skills = request.GET.getlist('check_skill')
            profiles = []
            # piu scalabile
            if query:
                for skill in checked_skills:
                    for single_profile in profile_list:
                        has_skills = HasSkill.objects.filter(
                            skill__name_skill=skill,
                            musicianprofile=single_profile)
                        if has_skills:
                            profiles += [single_profile]
            else:
                for skill in checked_skills:
                    has_skills = HasSkill.objects.filter(
                        skill__name_skill=skill)
                    for hs in has_skills:
                        profiles += [hs.musicianprofile]
            profiles = set(profiles)
            if profile_list:
                profile_list = profiles.intersection(profile_list)
            elif not query:
                profile_list = profiles

        if mpf.is_valid():
            if mpf.cleaned_data['country']:
                profiles = set(
                    MusicianProfile.get_musician(
                        country=mpf.cleaned_data['country']))
                if profile_list:
                    profile_list = profiles.intersection(profile_list)
                elif not request.GET.getlist('check_skill'):
                    profile_list = profiles

            if mpf.cleaned_data['gender']:
                profiles = set(
                    MusicianProfile.get_musician(
                        gender=mpf.cleaned_data['gender']))
                if profile_list:
                    profile_list = profiles.intersection(profile_list)
                elif not mpf.cleaned_data[
                        'country'] and not request.GET.getlist('check_skill'):
                    profile_list = profiles

    return render(
        request,
        'portal/search_musician.html',
        {
            'profile_list': profile_list,
            'query': query,
            'skills': skills,
            'checked_skills': checked_skills,
            'musicianprofileform': mpf,
            'n_req': n_req,
            #'n_mes': n_mes,
            #'n_comm': n_comm
        })
示例#13
0
文件: views.py 项目: Bruce94/Musician
def musician_info(request, user_id):
    user = get_object_or_404(User, pk=user_id)

    n_req = Friend.n_req_friendship(request.user)
    user_distance = request.user.musicianprofile.musician_distance(user.musicianprofile)

    mpf = MusicianProfileForm(prefix='profile')

    skills = Skill.objects.all()

    status_friend = 0
    reciver = None

    if Friend.get_friendship(request.user, user):
        fs = Friend.get_friendship(request.user, user)
        reciver = fs.reciver
        status_friend = fs.status

    if request.method == 'POST':

        mpf = MusicianProfileForm(request.POST, request.FILES, prefix='profile')

        # Personal Info edit post
        if "first_name" in request.POST:
            user.first_name = request.POST['first_name']
            user.save()
        if "last_name" in request.POST:
            user.last_name = request.POST['last_name']
            user.save()
        if "city" in request.POST:
            user.musicianprofile.city = request.POST['city']
            user.musicianprofile.save()
        if "bio" in request.POST:
            user.musicianprofile.bio = request.POST['bio']
            user.musicianprofile.save()

        # skill list post
        if request.POST.getlist('check_skill'):
            checked_skills = request.POST.getlist('check_skill')
            for user_skill in user.musicianprofile.skills.all():
                if not(user_skill.name_skill in checked_skills):
                    hs = HasSkill.objects.all().filter(musicianprofile=user.musicianprofile, skill=user_skill).__getitem__(0)
                    hs.delete()
            for skill in skills:
                if skill.name_skill in checked_skills:
                    HasSkill.create(musicianprofile=user.musicianprofile, skill=skill)

        if mpf.is_valid():
            if mpf.cleaned_data['gender']:
                user.musicianprofile.gender = mpf.cleaned_data['gender']
                user.musicianprofile.save()
            if mpf.cleaned_data['country']:
                user.musicianprofile.country = mpf.cleaned_data['country']
                user.musicianprofile.save()
            if mpf.cleaned_data['data']:
                print(mpf.cleaned_data['data'])
                user.musicianprofile.data = mpf.cleaned_data['data']
                user.musicianprofile.save()
            # General Info edit post
            if mpf.cleaned_data['phone_number']:
                user.musicianprofile.phone_number = mpf.cleaned_data['phone_number']
                user.musicianprofile.save()

        # handle the endorsements of users on skills
        for skill in skills:
            if ("btn_"+skill.name_skill) in request.POST:
                has_skill = HasSkill.objects.filter(musicianprofile=user.musicianprofile, skill=skill)
                for hs in has_skill:
                    hs.endorse_user.add(request.user.musicianprofile)
            elif ("btn_"+skill.name_skill+"_rem") in request.POST:
                has_skill = HasSkill.objects.filter(musicianprofile=user.musicianprofile, skill=skill)
                for hs in has_skill:
                    hs.endorse_user.remove(request.user.musicianprofile)

    user_has_skill = HasSkill.objects.filter(musicianprofile=user.musicianprofile)
    return render(request, 'musician/musician_info.html', {'user': user,
                                                           'reciver': reciver,
                                                           'user_has_skill': user_has_skill,
                                                           'musicianprofileform': mpf,
                                                           'skills': skills,
                                                           'status_friend': status_friend,
                                                           'n_req': n_req,
                                                           'user_distance': user_distance})
示例#14
0
文件: views.py 项目: Bruce94/Musician
def fs_request(request):
    n_fs = Friend.n_req_friendship(request.user)
    response = {'n_fs': n_fs}
    r = json.dumps(response, False)
    return JsonResponse(r, safe=False)