Exemplo n.º 1
0
Arquivo: views.py Projeto: kpx13/solar
def profile(request):
    c = get_common_context(request)
    user = request.user
    participant_form = None
    expert_form = None
    profile = user.get_profile()
    profile_form = None
    
    if request.method == 'GET':
        if Participant.exist(user):
            participant = Participant.objects.get(user=user)
            participant_form = ParticipantForm(instance=participant)
        elif Expert.exist(user):
            expert = Expert.objects.get(user=user)
            expert_form = ExpertForm(instance=expert)
        profile_form = ProfileForm(instance=profile, initial={'name': user.first_name,
                                                          'last_name': user.last_name})
        if not (request.user.first_name or request.user.last_name):
            c['pr_msg'] = u'Необходимо заполнить поля: Имя и Фамилия.'
    else:
        if Expert.exist(user):
            expert = Expert.objects.get(user=user)
            expert_form = ExpertForm(request.POST, request.FILES)
        else:
            if Participant.exist(user):
                participant = Participant.objects.get(user=user)
                participant_form = ParticipantForm(request.POST, request.FILES)
                if participant_form.is_valid():
                    participant.about = participant_form.data['about']
                    participant.save()
            
            profile_form = ProfileForm(request.POST, request.FILES)
            if profile_form.is_valid():
                user.first_name = profile_form.data['name']
                user.last_name = profile_form.data['last_name']
                user.save()
                if 'photo' in request.FILES:
                    profile.photo = request.FILES.get('photo', '')
                profile.sex = profile_form.data.get('sex', '')
                profile.date_birth = profile_form.data['date_birth']
                profile.school = profile_form.data['school']
                profile.save()
    
    c['participant_form'] = participant_form
    c['expert_form'] = expert_form
    c['profile_form'] = profile_form
    c['user_photo'] = profile.photo
    
    return render_to_response('profile.html', c, context_instance=RequestContext(request))
Exemplo n.º 2
0
Arquivo: views.py Projeto: kpx13/solar
def get_common_context(request):
    c = {}
    c['lang'] = request.LANGUAGE_CODE
    c['request_url'] = request.path
    c['is_debug'] = settings.DEBUG
    c['recent_news'] = Article.get_list(c['lang'])[:2]
    c['recent_projects'] = Project.objects.all()[:3]
    c['recent_comments'] = ProjectComment.objects.all()[:3]
    c['ime_expert'] = Expert.exist(request.user)
    c['ime_participant'] = Participant.exist(request.user)
    c['auth'] = request.user.is_authenticated()
    c.update(csrf(request))
    return c
Exemplo n.º 3
0
Arquivo: views.py Projeto: kpx13/solar
def user(request, username):
    c = get_common_context(request)
    c['user'] = User.objects.get(username=username)
    c['profile'] = c['user'].get_profile()
    c['role'] = u'Пользователь'
    if Expert.exist(c['user']):
        c['role'] = u'Эксперт'
        c['expert'] = Expert.objects.get(user=c['user']).get_(c['lang'])
    elif Participant.exist(c['user']):
        c['role'] = u'Участник'
        c['participant'] = Participant.objects.get(user=c['user'])
        
    return render_to_response('user.html', c, context_instance=RequestContext(request))
Exemplo n.º 4
0
Arquivo: views.py Projeto: kpx13/solar
def participate(request):
    c = get_common_context(request)
    if not c['auth']:
        return HttpResponseRedirect('/accounts/register/')
    user = request.user
    profile = user.get_profile()
    if request.method == 'GET':
        if Participant.exist(user):
            return HttpResponseRedirect('/project/%s/' % Participant.get_project(user))
        elif Expert.exist(user):
            c['form'] = None
            c['msg'] = u'Вы эксперт, поэтому не можете принимать участие.'
        else:
            c['form'] = ParticipateForm(initial={'name': user.first_name,
                                             'last_name': user.last_name,
                                             'photo': profile.photo,
                                             'sex': profile.sex,
                                             'date_birth': profile.date_birth,
                                             'school': profile.school,
                                             })
            c['project_form'] = ProjectForm()
    else:
        form = ParticipateForm(request.POST, request.FILES)
        project_form = ProjectForm(request.POST)
        if form.is_valid() and project_form.is_valid():
            user.first_name = form.data['name']
            user.last_name = form.data['last_name']
            user.save()
            if 'photo' in request.FILES:
                profile.photo = request.FILES.get('photo', '')
            profile.sex = form.data.get('sex', '')
            profile.date_birth = form.data['date_birth']
            profile.school = form.data['school']
            profile.save()
            part = Participant(user=user,
                               about=form.data['about'])
            part.save()
            
            pf = project_form.save(commit=False)
            pf.participant = part
            pf.save()
            
            return HttpResponseRedirect('/project/')
        else:
            pass
        
        c['form'] = form
        c['project_form'] = project_form
    c['user_photo'] = profile.photo
    return render_to_response('participate.html', c, context_instance=RequestContext(request))