Exemple #1
0
def get_details(request):
    def errorHandle(error):
        form = BadgeForm()
        return render_to_response('badge/input.html', {
            'error': error,
            'form': form,
            'login': '******'
        },
                                  context_instance=RequestContext(request))

    def handle_uploaded_file(cfh, ifh, dirname, rand, nfcol, cfcol):

        ob = createBadges.BadgeMaker(cfh,
                                     ifh,
                                     namecol=nfcol,
                                     compcol=cfcol,
                                     fontname="badge/BadgeGen/nevis.ttf",
                                     dirname=dirname + "/" + rand)
        ob.generateBadges()

        filewriter = zipfile.ZipFile("media/" + dirname + rand + ".zip", "w")
        for name in glob.glob("outputPNGs/" + dirname + "/" + rand + "/*"):
            filewriter.write(name, os.path.basename(name),
                             zipfile.ZIP_DEFLATED)
        filewriter.close()

    if request.method == 'POST':
        form = BadgeForm(request.POST, request.FILES)

        dirname = request.POST['csrfmiddlewaretoken']
        rand = random.randint(1000, 9999)
        namefontcol = '#' + str(request.POST['namefontcol'])
        compfontcol = '#' + str(request.POST['compfontcol'])

        if form.is_valid():
            uploadedImage = form.cleaned_data['templatefile']
            imageData = Image.open(uploadedImage)
            handle_uploaded_file(request.FILES['csvfile'], imageData, dirname,
                                 str(rand), namefontcol, compfontcol)
            zipfilename = dirname + str(rand) + ".zip"
            filezip = open("media/" + zipfilename)
            #  response = HttpResponse(filezip, mimetype = "application/x-zip-compressed")
            #  value = 'attachment; filename=badges_'+str(rand)+'.zip'
            #  response['Content-Disposition'] = value
            return render_to_response('badge/input.html', {
                'login': '******',
                'zipfile': zipfilename
            },
                                      context_instance=RequestContext(request))
        else:
            error = 'Form invalid'
            return errorHandle(error)

    else:
        form = BadgeForm()
        return render_to_response('badge/input.html', {
            'form': form,
            'login': '******'
        },
                                  context_instance=RequestContext(request))
Exemple #2
0
def badge(request, pk=""):
    choices = [(p.id, p.name + " - " + p.site.title) for p in request.user.players.all()]
    if request.method == "POST":
        form = BadgeForm(request.POST)
        form.fields["players"].choices = choices
        if form.is_valid():
            players = [get_object_or_404(Player, pk=int(p)) for p in form.cleaned_data["players"]]
            if pk:
                badge = get_object_or_404(Badge, pk=int(pk))
                badge.players.clear()
                badge.title = form.cleaned_data["title"]
                badge.format = form.cleaned_data["config"]
                badge.is_custom = form.custom_format
            else:
                config = form.cleaned_data["config"].replace(r"\n", "\n")
                badge = Badge(
                    user=request.user, is_custom=form.custom_format, format=config, title=form.cleaned_data["title"]
                )
                badge.save()
            for p in players:
                badge.players.add(p)
            badge.save()
            badge.url = "images/badges/badge_custom_%s_%s.png" % (request.user.pk, badge.pk)
            badge.save()
            try:
                build_badge.delay(badge)
            except Exception as e:
                logging.exception(e.args[0])
                raise e
            return HttpResponseRedirect(reverse("account_profile"))
    else:
        if pk:
            badge = get_object_or_404(Badge, pk=pk)
            if badge.is_custom:
                params = eval(badge.format)
                formData = {
                    "title": badge.title,
                    "players": [p.id for p in badge.players.all()],
                    "preset": params["template"],
                    "background": params["background"],
                    "top_color": params["color1"],
                    "bottom_color": params["color2"],
                    "text_color": params["text"],
                    "font_size": params["size"],
                }
            else:
                config = badge.format.replace("\n", "\\n")
                formData = {
                    "title": badge.title,
                    "config": config,
                    "players": [(p.id, p.name + " - " + p.site.title) for p in badge.players.all()],
                }
            form = BadgeForm(initial=formData)
        else:
            # form  = BadgeForm({'players':players})
            form = BadgeForm()
        form.fields["players"].choices = choices
    return render_to_response("badge.html", {"form": form, "pk": pk}, context_instance=RequestContext(request))
Exemple #3
0
def profile(request, id):
    person = TeamMember.objects.get(pk=id)
    if request.method == 'POST':
        form = BadgeForm(request.POST, request.FILES)
        if form.is_valid():
            b = Badge(badge_name=form.cleaned_data['name'], description=form.cleaned_data['description'], badge_image=form.cleaned_data['image'])
            b.save()
            person.badge.add(b)
            person.save()
    upload_form = BadgeForm()
    tasks = person.tasks_assigned.filter(status="open")
    return render(request,'profiles/profile_base.html', {'person': person,
                                                        'avg': person.feedback_averages(),
                                                        'form': upload_form,
                                                        'tasks': tasks})
Exemple #4
0
 def errorHandle(error):
     form = BadgeForm()
     return render_to_response('badge/input.html', {
         'error': error,
         'form': form,
         'login': '******'
     },
                               context_instance=RequestContext(request))
Exemple #5
0
def get_details(request):

    def errorHandle(error):
        form = BadgeForm()
        return render_to_response('badge/input.html', {'error':error, 'form':form, 'login':'******'}, context_instance=RequestContext(request))

    def handle_uploaded_file(cfh, ifh, dirname, rand, nfcol, cfcol):

        ob = createBadges.BadgeMaker(cfh, ifh, namecol = nfcol, compcol = cfcol, fontname = "badge/BadgeGen/nevis.ttf", dirname = dirname+"/"+rand )
        ob.generateBadges()
        
        filewriter = zipfile.ZipFile("media/"+dirname+rand+".zip", "w")
        for name in glob.glob("outputPNGs/"+dirname+"/"+rand+"/*"):
            filewriter.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED)
        filewriter.close()


    if request.method == 'POST':
        form = BadgeForm(request.POST, request.FILES)
        
        dirname = request.POST['csrfmiddlewaretoken']
        rand = random.randint(1000,9999)
        namefontcol = '#'+str(request.POST['namefontcol'])
        compfontcol = '#'+str(request.POST['compfontcol'])

        if form.is_valid():      
            uploadedImage = form.cleaned_data['templatefile']
            imageData = Image.open(uploadedImage)
            handle_uploaded_file(request.FILES['csvfile'], imageData, dirname, str(rand), namefontcol, compfontcol)
            zipfilename = dirname+str(rand)+".zip"
            filezip = open("media/"+zipfilename)
          #  response = HttpResponse(filezip, mimetype = "application/x-zip-compressed")
          #  value = 'attachment; filename=badges_'+str(rand)+'.zip'
          #  response['Content-Disposition'] = value
            return render_to_response('badge/input.html',{'login':'******','zipfile':zipfilename}, context_instance=RequestContext(request))
        else:
            error = 'Form invalid'
            return errorHandle(error)
   
    else:
       form = BadgeForm()
       return render_to_response('badge/input.html', {'form':form, 'login':'******'}, context_instance=RequestContext(request))
Exemple #6
0
def badge_preview(request):
    choices = [(p.id, p.name + " - " + p.site.title) for p in request.user.players.all()]
    url = "images/badges/badge_temp_%s.png" % hash(request.META["REMOTE_ADDR"])
    if request.method == "POST":
        form = BadgeForm(request.POST)
        form.fields["players"].choices = choices
        if form.is_valid():

            class Temp(object):
                pass

            badge = Temp()
            players = [get_object_or_404(Player, pk=int(p)) for p in form.cleaned_data["players"]]
            badge.players = Temp()
            badge.players.all = lambda: players
            badge.title = form.cleaned_data["title"]
            badge.format = form.cleaned_data["config"]
            badge.is_custom = form.custom_format
            badge.url = url
            badge.user = request.user
            build_badge.delay(badge)
        else:
            print form.errors
    return HttpResponse(url + "?" + str(hash(datetime.datetime.now())))