stuff
This commit is contained in:
16
16-debugging/README.md
Normal file
16
16-debugging/README.md
Normal file
@ -0,0 +1,16 @@
|
||||
Debugging Challenge
|
||||
===================
|
||||
|
||||
So you and your buddies made a sweet MMORPG. You don't have any players yet, and you wanted to test the database, so you hired someone to write code to seed it.
|
||||
|
||||
It _should_ be all done - you'll just have to make the migrations, migrate the DB, and run the seed file successfully. But _should_ is a bad word in programming. Turns out, this guy had no idea how to do it and his code is a mess.
|
||||
|
||||
To run the seed, you should just have to instantiate the class once like so:
|
||||
```
|
||||
SeedDatabase(players = 50, heroes = 150, teams = 20)
|
||||
```
|
||||
It takes three keyword arguments - number of players to create, number of heroes to create, number of teams to create. In the database, Players have many Heroes. Heroes have many Teams and Teams have many Heroes.
|
||||
|
||||
#### Goal
|
||||
|
||||
Using the error statements and your knowledge of Python and Django, go through and debug the code. When you are done, go in the Django shell and test your relations. Read through the docs from Django on [Many to Many](https://docs.djangoproject.com/en/1.4/topics/db/examples/many_to_many/) and [One to Many](https://docs.djangoproject.com/en/1.4/topics/db/examples/many_to_one/) relationships if you haven't already.
|
83
16-debugging/debugging_project/debugging_project/settings.py
Normal file
83
16-debugging/debugging_project/debugging_project/settings.py
Normal file
@ -0,0 +1,83 @@
|
||||
"""
|
||||
Django settings for debugging_project project.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/1.7/topics/settings/
|
||||
|
||||
For the full list of settings and their values, see
|
||||
https://docs.djangoproject.com/en/1.7/ref/settings/
|
||||
"""
|
||||
|
||||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
||||
import os
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
|
||||
|
||||
|
||||
# Quick-start development settings - unsuitable for production
|
||||
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = 'gr6(3zd1pbpgs(99#6g^+)=h6w7yqp38dib9pli08)(1z_24+)'
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
|
||||
TEMPLATE_DEBUG = True
|
||||
|
||||
ALLOWED_HOSTS = []
|
||||
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = (
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
)
|
||||
|
||||
MIDDLEWARE_CLASSES = (
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
)
|
||||
|
||||
ROOT_URLCONF = 'debugging_project.urls'
|
||||
|
||||
WSGI_APPLICATION = 'debugging_project.wsgi.application'
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
|
||||
}
|
||||
}
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/1.7/topics/i18n/
|
||||
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
|
||||
TIME_ZONE = 'UTC'
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
USE_L10N = True
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
# https://docs.djangoproject.com/en/1.7/howto/static-files/
|
||||
|
||||
STATIC_URL = '/static/'
|
10
16-debugging/debugging_project/debugging_project/urls.py
Normal file
10
16-debugging/debugging_project/debugging_project/urls.py
Normal file
@ -0,0 +1,10 @@
|
||||
from django.conf.urls import patterns, include, url
|
||||
from django.contrib import admin
|
||||
|
||||
urlpatterns = patterns('',
|
||||
# Examples:
|
||||
# url(r'^$', 'debugging_project.views.home', name='home'),
|
||||
# url(r'^blog/', include('blog.urls')),
|
||||
|
||||
url(r'^admin/', include(admin.site.urls)),
|
||||
)
|
14
16-debugging/debugging_project/debugging_project/wsgi.py
Normal file
14
16-debugging/debugging_project/debugging_project/wsgi.py
Normal file
@ -0,0 +1,14 @@
|
||||
"""
|
||||
WSGI config for debugging_project project.
|
||||
|
||||
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "debugging_project.settings")
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
application = get_wsgi_application()
|
0
16-debugging/debugging_project/game/__init__.py
Normal file
0
16-debugging/debugging_project/game/__init__.py
Normal file
3
16-debugging/debugging_project/game/admin.py
Normal file
3
16-debugging/debugging_project/game/admin.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
28
16-debugging/debugging_project/game/models.py
Normal file
28
16-debugging/debugging_project/game/models.py
Normal file
@ -0,0 +1,28 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
class Players(models.Model):
|
||||
username = models.CharField(max_length = 30)
|
||||
password = models.CharField(max_length = 30)
|
||||
email = models.CharField(max_length = 50)
|
||||
created_at = models.DateTimeField('date created')
|
||||
|
||||
def clean(self):
|
||||
if(len(self.password) < 8):
|
||||
raise ValidationError("password too short")
|
||||
email_validator = re.compile('^[A-Z0-9-.]+[a-z0-9-]+[a-zA-Z]{4,}$')
|
||||
if(email_validator.match(self.email) == None):
|
||||
raise ValidationError("invalid email")
|
||||
|
||||
def save(self, **kwargs):
|
||||
self.full_clean()
|
||||
return super(Players, self).save(**kwargs)
|
||||
|
||||
class Heroes(models.Model):
|
||||
player = ForeignKey(Players)
|
||||
name = models.CharField(max_length = 20)
|
||||
type = models.CharField()
|
||||
strength = models.IntegerField(default=10)
|
||||
|
||||
class Teams(models.Model):
|
||||
name = models.CharField()
|
||||
heroes = ManyToManyField(Hero)
|
56
16-debugging/debugging_project/game/seeds.py
Normal file
56
16-debugging/debugging_project/game/seeds.py
Normal file
@ -0,0 +1,56 @@
|
||||
import os
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
|
||||
from faker import Factory
|
||||
import random
|
||||
from app.models import Players, Heroes
|
||||
|
||||
class SeedDatabase:
|
||||
def __init__(self, **kwargs):
|
||||
self.fake = Factory.create()
|
||||
self.player_num = kwargs[players]
|
||||
self.hero_num = kwargs[heroes]
|
||||
self.team_num = kwargs[teams]
|
||||
player_seed = Seed.seed_players()
|
||||
hero_seed = Seed.seed_heroes()
|
||||
team_seed = Seed.seed_teams()
|
||||
if(player_seed == True) and (hero_seed == True) and (team_seed == True):
|
||||
print("Success!")
|
||||
|
||||
def seed_players():
|
||||
for i in self.player_num:
|
||||
Players.create(username = ''.split(self.fake.name())[1],\
|
||||
password = random_password(),\
|
||||
email = self.fake.email(),\
|
||||
created_at = __random_datetime())
|
||||
return True
|
||||
|
||||
def seed_heroes():
|
||||
for i in self.hero_num:
|
||||
Heroes.create(name = self.fake.text().split()[3],\
|
||||
type = self.__random_hero(),\
|
||||
strength = random.randrange(6,55),\
|
||||
player = self.__random_player())
|
||||
return True
|
||||
|
||||
def seed_teams():
|
||||
for i in self.team_num:
|
||||
team = Teams.create(name = self.fake.company())
|
||||
for j in random.randrange(3,12):
|
||||
team.heroes.add(self.__random_hero())
|
||||
return True
|
||||
|
||||
def __random_password():
|
||||
return ''.join(random.sample(string.ascii_lowercase, random.randrange(4,14)))
|
||||
|
||||
def __random_datetime():
|
||||
return datetime.now() - datetime.timedelta(random.randrange(2,3123))
|
||||
|
||||
def __random_hero_type():
|
||||
types = ['Wizard', 'Warrior', 'Ranger', 'Druid', 'Shaman', 'Bard']
|
||||
return ''.join(random.sample(heroes, 1))
|
||||
|
||||
def __random_player():
|
||||
return Players.objects.filter(pk=random.randrange(1,self.player_num))
|
||||
|
||||
def __random_hero():
|
||||
return Heroes.objects.filter(pk=random.randrange(1,self.hero_num))
|
3
16-debugging/debugging_project/game/tests.py
Normal file
3
16-debugging/debugging_project/game/tests.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
3
16-debugging/debugging_project/game/views.py
Normal file
3
16-debugging/debugging_project/game/views.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
10
16-debugging/debugging_project/manage.py
Normal file
10
16-debugging/debugging_project/manage.py
Normal file
@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env python
|
||||
import os
|
||||
import sys
|
||||
|
||||
if __name__ == "__main__":
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "debugging_project.settings")
|
||||
|
||||
from django.core.management import execute_from_command_line
|
||||
|
||||
execute_from_command_line(sys.argv)
|
Reference in New Issue
Block a user