Ejemplo n.º 1
0
def createcreature(request):

    # vars:
    auth = "<p>This page is only accessible for admin users</p>"
    creature = []
    mycreature = []
    go = 'createcreature.html'

    if request.user.is_authenticated() and request.user.username == "admin":
        auth = "<p>Welcome <b>"+request.user.username+"</b></p>"
        go = 'createcreature.html'
        userid = request.user.id

        # Building 50 random chromosomes with 39 gens each of them with the structure 3x3x3
        struc_base = ["01", "0101", "010101", "010102", "010103", "0102", "010201", "010202", "010203", "0103", "010301", "010302", "010303", "02", "0201","020101", "020102", "020103", "0202", "020201", "020202", "020203", "0203", "020301", "020302", "020303", "03", "0301", "030101","030102", "030103","0302", "030201", "030202", "030203","0303", "030301", "030302", "030303",];
        tags_ids = ["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39"]
        chromosomes = []
        max = int(config("CHROMOSOMES_X_GENERATION"))
        for n in range(1, max+1): ## FIXME this is for testiog. The good values is range(1,51)
            shuffle(tags_ids)
            chromosome = []
            c = 0
            for d in struc_base:
                chromosome.append(d+tags_ids[c])
                c = c + 1
            chromosomes.append(chromosome)
        # Inserting new creature (generation 0) in Creature Table
        now = datetime.datetime.now()
        datenow = str(now.year)+'-'+str(now.month)+'-'+str(now.day)
        new = Creature.objects.create(creation_date=datenow, current_generation=0)
        new.save()
        # Inserting the new 50 chromosome and the new 50 pending tasks in Tasks
        mycreature = Creature.objects.latest('id')
        for c in chromosomes:
            newChromosomes = Chromosome(data=str(c), creature_id_id=str(mycreature))
            newChromosomes.save()
            latestId=newChromosomes.id
            newTasks = Tasks(user_id_id=0, chromosome_id_id=str(latestId))
            newTasks.save()


        # Getting Chromosomes list

    else:
        auth = "<p>This page is only accessible for admin users</p>"
        go = 'noaccess.html'

    return render_to_response(go, {'auth': auth, 'mycreature': mycreature})
Ejemplo n.º 2
0
	def saveToBD(self):
		print("Save chromosomes reproduced")
		print(self.new_chrom)
		# add one to define that is a newer generation
		self.current_generation +=1
		# Inserting new creature (generation +1) in Creature Table
		now = datetime.datetime.now()
		datenow = str(now.year)+'-'+str(now.month)+'-'+str(now.day)
		new = Creature.objects.create(creation_date=datenow, current_generation=self.current_generation)
		new.save()
		# Inserting the new 50 chromosome and the new 50 pending tasks in Tasks
		mycreature = Creature.objects.latest('id')
		for c in self.new_chrom:
			newChromosomes = Chromosome(data=str(c), creature_id_id=str(mycreature), generation=self.current_generation)
			newChromosomes.save()
			latestId=newChromosomes.id
			newTasks = Tasks(user_id_id=0, chromosome_id_id=str(latestId))
			newTasks.save()