Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Richard django lab 3 #351

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Code/Richard/django/pokedex/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_pokedex.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
Empty file.
9 changes: 9 additions & 0 deletions Code/Richard/django/pokedex/pokedex_app/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.contrib import admin
from .models import Pokemon, PokemonType
class InlineModelAdmin(admin.ModelAdmin):
#Pokemon.types(ModelAdmin.filter_horizontal)
pass

# Register your models here.
admin.site.register([Pokemon])
admin.site.register([PokemonType])
6 changes: 6 additions & 0 deletions Code/Richard/django/pokedex/pokedex_app/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class PokedexAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'pokedex_app'
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from django.core.management.base import BaseCommand
from django.db import models
import requests

from pokedex_app.models import Pokemon, PokemonType


class Command(BaseCommand):
def handle(self, *args, **kwargs):
url = 'https://raw.githubusercontent.com/PdxCodeGuild/Class_Raven/master/4%20Django/labs/03%20Pokedex/pokemon.json'

Pokemon.objects.all().delete()
PokemonType.objects.all().delete()
response= requests.get(f'https://raw.githubusercontent.com/PdxCodeGuild/Class_Raven/master/4%20Django/labs/03%20Pokedex/pokemon.json')
data=response.json()
data=data['pokemon']
#data.reverse()

for poke in data:
number=poke['number']
name = poke['name']
height = poke['height']
weight = poke['weight']
image_front = poke['image_front']
image_back = poke['image_back']
#types=[type['type']['name'] for type in poke['types']]
types=poke['types']

pokemon, created=Pokemon.objects.get_or_create(
number=number,
name=name.title(),
height=height,
weight=weight,
image_front=image_front,
image_back=image_back,
)
for type_item in types:
type_item=type_item.title()
pokemon_type, created=PokemonType.objects.get_or_create(name=type_item)
pokemon.types.add(pokemon_type)
#print(pokemon, pokemon_type)
#print(vars(pokemon))



# for type in types: #starting point with Keegan
# pokemon, created=PokemonType.objects.get_or_create(name=type)
# pokemon.types.add(name)

#print(pokemon)
#print(PokemonType.objects.all)











# response = requests.get(url)

# pokedex_data = response.json() #dict 'pokemon':[data I want as a list of dictionaries]
# pokedex_data = pokedex_data['pokemon']#list of dictionaries

#print(pokedex_data)
# create a database entry for each pokemon
# for loop to pull data from each individual pokemon dictionary
# for pokemon in pokedex_data:
# #print(pokemon)
# #print(" ")
# number = pokemon['number']
# name = pokemon['name']
# height = pokemon['height']
# weight = pokemon['weight']
# image_front = pokemon['image_front']
# image_back = pokemon['image_back']
# types = pokemon['types']
# pokemon = Pokemon.objects.create(
# number=number,
# name=name,
# height=height,
# weight=weight,
# image_front=image_front,
# image_back=image_back,
# )
#apparetly you need to save before adding manytomany data..I still don't know how to get it right
#pokemon.add(types)
#print(pokemon)
34 changes: 34 additions & 0 deletions Code/Richard/django/pokedex/pokedex_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 4.0.3 on 2022-03-22 00:50

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='PokemonType',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=30)),
],
),
migrations.CreateModel(
name='Pokemon',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('number', models.IntegerField()),
('name', models.CharField(max_length=30)),
('height', models.FloatField()),
('weight', models.FloatField()),
('image_front', models.CharField(max_length=30)),
('image_back', models.CharField(max_length=30)),
('types', models.ManyToManyField(to='pokedex_app.pokemontype')),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.3 on 2022-04-01 00:47

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('pokedex_app', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='pokemon',
name='id',
field=models.AutoField(primary_key=True, serialize=False),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.3 on 2022-04-01 00:48

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('pokedex_app', '0002_alter_pokemon_id'),
]

operations = [
migrations.AlterField(
model_name='pokemontype',
name='id',
field=models.AutoField(primary_key=True, serialize=False),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.0.3 on 2022-04-01 01:00

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('pokedex_app', '0003_alter_pokemontype_id'),
]

operations = [
migrations.AlterField(
model_name='pokemon',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='pokemontype',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
]
Empty file.
20 changes: 20 additions & 0 deletions Code/Richard/django/pokedex/pokedex_app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.db import models

# Create your models here.
class PokemonType(models.Model):
name=models.CharField(max_length=30)

def __str__(self):
return f"{self.name}"

class Pokemon(models.Model):
number=models.IntegerField()
name=models.CharField(max_length=30)
height=models.FloatField()
weight=models.FloatField()
image_front=models.CharField(max_length=30)
image_back=models.CharField(max_length=30)
types=models.ManyToManyField('PokemonType')

def __str__(self):
return f"{self.name}"
24 changes: 24 additions & 0 deletions Code/Richard/django/pokedex/pokedex_app/templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"></script>


<title>Pokedex Home</title>
</head>

<body>

</body>

</html>
3 changes: 3 additions & 0 deletions Code/Richard/django/pokedex/pokedex_app/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions Code/Richard/django/pokedex/pokedex_app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
Loading