Beispiel #1
0
def AddNewProject(request):

    if 'login' in request.session:
            user=User.objects.get(login=request.session['login'])
            f = forms.ProjectRegisterForm(prefix='project')
            fr = forms.ProjectPerks(prefix='perk')
            context = RequestContext(request, {'formset': f, 'form1': fr})
            if request.method == 'POST':
                f = forms.ProjectRegisterForm(request.POST, prefix='project')
                p = Project()
                if(f.is_valid()):
                    p.title = f.cleaned_data['title']
                    p.short_description = f.cleaned_data['short_description']
                    p.funding_goal = f.cleaned_data['funding_goal']
                    p.full_description = f.cleaned_data['description']
                    p.category = f.cleaned_data['category']
                    p.user =user
                    Project.save(p)
                    if (request.FILES.getlist('file')!=""):
                        if(not os.path.exists(p.title)):
                            os.mkdir(p.title)
                        for file in request.FILES.getlist('file'):
                            l = open(p.title+'\\'+file.name, 'wb+')
                            for chunk in file.chunks():
                                l.write(chunk)
                            l.close()
                            atachment=Atachment()
                            atachment.url=p.title+'\\'+file.name
                            atachment.project=p
                            Atachment.save(atachment)
                    for urlfile in request.POST.getlist('urlfile'):
                        atachment=Atachment()
                        atachment.url=urlfile
                        atachment.project=p
                        Atachment.save(atachment)
                    j=0
                    perkvalue=Perk()
                    for perk in request.POST.getlist('perk'):

                        if j==0:
                            perkvalue.project=p
                            perkvalue.title= perk
                            j+=1
                        elif j==1:
                            perkvalue.description=perk
                            j+=1
                        elif j==2:
                            j+=1
                            perkvalue.amount= int(perk)
                        else:
                            j=0
                            if int(perk)>0:
                                perkvalue.number_available=int(perk)
                            perkvalue.save()
                            perkvalue=Perk()
                return redirect('/', request)
            else:
                return render_to_response('AddNewProject.html', context)
    else:
        return redirect('/logowanie', request)
Beispiel #2
0
def saveeditedproject(request,project_id):
    if 'login' in request.session:
        if request.method == 'POST':
            user=User.objects.get(login=request.session['login'])
            f = forms.ProjectRegisterForm(request.POST, prefix='project')
            p = Project.objects.get(id=project_id)
            if(f.is_valid()):
                p.title = f.cleaned_data['title']
                p.short_description = f.cleaned_data['short_description']
                p.funding_goal = f.cleaned_data['funding_goal']
                p.full_description = f.cleaned_data['description']
                p.category = f.cleaned_data['category']
                p.user =user
                p.save()
                for removedperk in request.POST.getlist('removedperk'):
                    Perk.objects.get(id=int(removedperk))
                for removedatachment in request.POST.getlist('removedatachment'):
                    atachment=Atachment.objects.get(id=int(removedatachment))
                    if atachment.url.startswith('http') or atachment.url.__contains__('www'):
                        atachment.delete()
                    else:
                        os.remove(atachment.url)
                        atachment.delete()
                if (request.FILES.getlist('file')!=""):
                    if(not os.path.exists(p.title)):
                        os.mkdir(p.title)
                    for file in request.FILES.getlist('file'):
                        l = open(p.title+'\\'+file.name, 'wb+')
                        for chunk in file.chunks():
                            l.write(chunk)
                        l.close()
                        atachment=Atachment()
                        atachment.url=p.title+'\\'+file.name
                        atachment.project=p
                        Atachment.save(atachment)
                for urlfile in request.POST.getlist('urlfile'):
                    atachment=Atachment()
                    atachment.url=urlfile
                    atachment.project=p
                    Atachment.save(atachment)
                j=0
                perkvalue=Perk()
                for perk in request.POST.getlist('perk'):
                    if j==0:
                        perkvalue.project=p
                        perkvalue.title= perk
                        j+=1
                    elif j==1:
                        perkvalue.description=perk
                        j+=1
                    elif j==2:
                        j+=1
                        perkvalue.amount= int(perk)
                    else:
                        j=0
                        if int(perk)>0:
                            perkvalue.number_available=int(perk)
                        perkvalue.save()
                        perkvalue=Perk()
                return redirect('/', request)
            else:
                return redirect('/editproject/'+str(project_id)+'/',request)
        else:
            return redirect('/', request)
    else:
        return redirect('/logowanie', request)
Beispiel #3
0
def index(request):
    template = loader.get_template('index.html')
    now = datetime.now().date()
    deadline_project_list = Project.objects.filter(deadline__gte=now).order_by('deadline')[:3]

    popular_project_list = Project.objects.filter(deadline__gte=now).order_by('-visit_counter')[:3]
    popular_project=list()
    deadline_project=list()
    #wyswietla tylko prjekty niezakonczone, posortowane wzgledem licznika odwiedzin malejąco
    for p in deadline_project_list:
        deadline_project_dictionary={}
        perc = (p.money_raised / p.funding_goal) * 100
        percentage = int(perc)
        diff = p.deadline - now
        daysLeft = diff.days
        if daysLeft < 0:
            daysLeft = 0
        setattr(p, 'toEnd', daysLeft)
        setattr(p, 'percentage', percentage)
        atachments=Atachment.objects.filter(project=p)
        hasatachment=False
        for a in atachments:
            if "jpg" in a.url or 'png' in a.url or 'gif' in a.url:
                deadline_project_dictionary['Atachment']=a
                hasatachment=True
                break;
        if not hasatachment:
            at=Atachment()
            at.url='project.jpg'
            at.project=p
            deadline_project_dictionary['Atachment']=at
        deadline_project_dictionary['Project']=p
        deadline_project.append(deadline_project_dictionary)
    for p in popular_project_list:
        popular_project_dictionary={}
        perc = (p.money_raised / p.funding_goal) * 100
        percentage = int(perc)
        diff = p.deadline - now
        daysLeft = diff.days
        if daysLeft < 0:
            daysLeft = 0
        setattr(p, 'toEnd', daysLeft)
        setattr(p, 'percentage', percentage)
        atachments=Atachment.objects.filter(project=p)
        hasatachment=False
        for a in atachments:
            if "jpg" in a.url or 'png' in a.url or 'gif' in a.url:
                popular_project_dictionary['Atachment']=a
                hasatachment=True
                break;
        if not hasatachment:
            at=Atachment()
            at.url='project.jpg'
            at.project=p
            popular_project_dictionary['Atachment']=at
        popular_project_dictionary['Project']=p
        popular_project.append(popular_project_dictionary)
    context = RequestContext(request, {
        'deadline_project_list': deadline_project,
        'popular_project_list': popular_project,
    })
    return HttpResponse(template.render(context))