Exemple #1
0
def upload(request):
    error_msg = False
    user = get_user(request)
    if user.is_authenticated():
    	if request.method == 'POST':
            form = RegisterForm(request.POST)
            if form.is_valid():
                a = Animal() 
                a.name = request.POST.get("name")
    	        #a.sex = request.POST.get("sex")
                #a.type = request.POST.get("type")
                #a.build = request.POST.get("build")
                #a.age = request.POST.get("age")
                #a.variety = request.POST.get("variety")
                #a.reason = request.POST.get("reason")
                #a.accept_num = request.POST.get("accept_num")
                #a.chip_num = request.POST.get("chip_num")
                #a.is_sterilization = request.POST.get("is_sterilization")
                #a.hair_type = request.POST.get("hair_type")
                a.note = request.POST.get("note")
                a.resettlement = request.POST.get("resettlement")
                a.phone = request.POST.get("phone")
                #a.email = request.POST.get("email")
                #a.childre_anlong = request.POST.get("childre_anlong")
                #a.nimal_anlong = request.POST.get("animal_anlong")
                #a.bodyweight = request.POST.get("bodyweight")
       	        image = request.FILES['photo']
 
	        if not ((name is none) or (note is none) or 
   	                (resettlement is none) or (phone is none) or
		        (image is none)):
                    error_msg = "some requirement fields are not filled in"
                else:
		    head, ext = os.path.splitext(image.name)
                    filename = user.get_username() + datetime.now() + ext
	            with open("src/media/" + filename, "wb") as code:
                        code.write(image)
		    a.image_name = filename
		    thumbnail(filename, "248x350")
		    thumbnail(filename, "248x350", TRUE)
		    a.save()
            else:
                print "invalided"
                error_msg = form.errors
        else:
            print "user authenticated failed"

    return render_to_response('upload.html', {'error_msg': error_msg}, context_instance=RequestContext(request))
Exemple #2
0
def upload(request):
    error_msg = False
    user = get_user(request)
    if request.method == 'POST':
        if not user.is_authenticated():
            return HttpResponseRedirect("/")
        form = UploadForm(request.POST, request.FILES)
        if form.is_valid():
            a = Animal() 
            a.name = request.POST.get("name")
    	    #a.sex = request.POST.get("sex")
            #a.type = request.POST.get("type")
            #a.build = request.POST.get("build")
            #a.age = request.POST.get("age")
            #a.variety = request.POST.get("variety")
            #a.reason = request.POST.get("reason")
            #a.accept_num = request.POST.get("accept_num")
            #a.chip_num = request.POST.get("chip_num")
            #a.is_sterilization = request.POST.get("is_sterilization")
            #a.hair_type = request.POST.get("hair_type")
            a.note = request.POST.get("note")
            a.resettlement = request.POST.get("resettlement")
            a.phone = request.POST.get("phone")
            #a.email = request.POST.get("email")
            #a.childre_anlong = request.POST.get("childre_anlong")
            #a.nimal_anlong = request.POST.get("animal_anlong")
            #a.bodyweight = request.POST.get("bodyweight")
            image = request.FILES['photo']
            head, ext = os.path.splitext(image.name)
            #filename = user.get_username() + str(int(time.time())) + ext.lower()
            filename = str(int(time.time())) + ".jpg"
            savefilename = "src/media/" + filename
            # TODO : try-catch for PIL errors
            with open(savefilename, "wb") as code:
                code.write(image.read())
            a.image_file = filename
            thumbnail(savefilename, "248x350")
            thumbnail(savefilename, "248x350", True)
            a.save()
            # TODO : return new page while upload success
            return HttpResponseRedirect("/")
        else:
            print "invalided"
            print form.errors
            return render_to_response('upload.html', {'error_msg': form.errors}, context_instance=RequestContext(request))
    return render_to_response('upload.html', context_instance=RequestContext(request))
Exemple #3
0
 def handle(self, *args, **options):
     print "start\n get data from" + self.url
     data = urllib2.urlopen(self.url)
     j = json.load(data)
     j = j["result"]["records"]
     counter = 0
     for i in j:
         a1 = Animal.objects.filter(accept_num=i["AcceptNum"])
         if not a1:
             counter += 1
             print "new ->"
             print(i["Name"]).encode('utf-8')
             print i["AcceptNum"]
             url = i["ImageName"]
             url_file = url.split("/")[-1]
             f = urllib2.urlopen(url)
             data = f.read()
             with open("src/media/" + url_file, "wb") as code:
                 code.write(data)
             print thumbnail("src/media/" + url_file, "248x350")
             print thumbnail("src/media/" + url_file, "248x350", True)
             a = Animal(
                 name=i["Name"],
                 sex=i["Sex"],
                 type=i["Type"],
                 build=i["Build"],
                 age=i["Age"],
                 variety=i["Variety"],
                 reason=i["Reason"],
                 accept_num=i["AcceptNum"],
                 chip_num=i["ChipNum"],
                 is_sterilization=i["IsSterilization"],
                 hair_type=i["HairType"],
                 note=i["Note"],
                 resettlement=i["Resettlement"],
                 phone=i["Phone"],
                 email=i["Email"],
                 childre_anlong=i["ChildreAnlong"],
                 animal_anlong=i["AnimalAnlong"],
                 bodyweight=i["Bodyweight"],
                 image_name=i["ImageName"],
                 image_file=url_file,
             )
             a.save()
     print "total %s pet updated" % counter
     self.stdout.write('end\n')
Exemple #4
0
 def handle(self, *args, **options):
     print "start\n get data from" + self.url
     data = urllib2.urlopen(self.url)
     j = json.load(data)
     j = j["result"]["records"]
     counter = 0
     for i in j:
         a1 = Animal.objects.filter(accept_num=i["AcceptNum"])
         if not a1:
             counter += 1
             print "new ->"
             print (i["Name"]).encode("utf-8")
             print i["AcceptNum"]
             url = i["ImageName"]
             url_file = url.split("/")[-1]
             f = urllib2.urlopen(url)
             data = f.read()
             with open("src/media/" + url_file, "wb") as code:
                 code.write(data)
             print thumbnail("src/media/" + url_file, "248x350")
             print thumbnail("src/media/" + url_file, "248x350", True)
             a = Animal(
                 name=i["Name"],
                 sex=i["Sex"],
                 type=i["Type"],
                 build=i["Build"],
                 age=i["Age"],
                 variety=i["Variety"],
                 reason=i["Reason"],
                 accept_num=i["AcceptNum"],
                 chip_num=i["ChipNum"],
                 is_sterilization=i["IsSterilization"],
                 hair_type=i["HairType"],
                 note=i["Note"],
                 resettlement=i["Resettlement"],
                 phone=i["Phone"],
                 email=i["Email"],
                 childre_anlong=i["ChildreAnlong"],
                 animal_anlong=i["AnimalAnlong"],
                 bodyweight=i["Bodyweight"],
                 image_name=i["ImageName"],
                 image_file=url_file,
             )
             a.save()
     print "total %s pet updated" % counter
     self.stdout.write("end\n")