Skip to content

lab3 started #45

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

Open
wants to merge 3 commits into
base: main
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
Binary file modified crimsonbouquet/content/__pycache__/__init__.cpython-37.pyc
100755 → 100644
Binary file not shown.
Binary file modified crimsonbouquet/content/__pycache__/admin.cpython-37.pyc
100755 → 100644
Binary file not shown.
Binary file modified crimsonbouquet/content/__pycache__/apps.cpython-37.pyc
100755 → 100644
Binary file not shown.
Binary file modified crimsonbouquet/content/__pycache__/models.cpython-37.pyc
100755 → 100644
Binary file not shown.
Binary file modified crimsonbouquet/content/__pycache__/schema.cpython-37.pyc
100755 → 100644
Binary file not shown.
Binary file modified crimsonbouquet/content/__pycache__/urls.cpython-37.pyc
100755 → 100644
Binary file not shown.
Binary file modified crimsonbouquet/content/__pycache__/views.cpython-37.pyc
100755 → 100644
Binary file not shown.
4 changes: 4 additions & 0 deletions crimsonbouquet/content/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@

# Register your models here.

from .models import Contributor, Article

admin.site.register(Contributor)
admin.site.register(Article)
36 changes: 36 additions & 0 deletions crimsonbouquet/content/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 3.2.18 on 2023-04-08 18:49

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Contributor',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('first_name', models.CharField(max_length=100)),
('last_name', models.CharField(max_length=100)),
('middle_name', models.CharField(max_length=100)),
('bio_text', models.CharField(blank=True, max_length=500, null=True)),
('_title', models.CharField(blank=True, choices=[('cstaff', 'Crimson staff writer'), ('contrib', 'Contributing writer'), ('photog', 'Photographer'), ('design', 'Designer'), ('editor', 'Editor'), ('opinion', 'Crimson opinion writer'), ('opinion_contrib', 'Contributing opinion writer'), ('sponsored_contrib', 'Sponsored Contributor')], max_length=70, null=True, verbose_name='title')),
],
),
migrations.CreateModel(
name='Article',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200)),
('text', models.TextField()),
('created_on', models.DateTimeField(auto_now_add=True)),
('slug', models.SlugField(max_length=70)),
('contributors', models.ManyToManyField(related_name='content', to='content.Contributor')),
],
),
]
Binary file modified crimsonbouquet/content/migrations/__pycache__/0001_initial.cpython-37.pyc
100755 → 100644
Binary file not shown.
Binary file modified crimsonbouquet/content/migrations/__pycache__/__init__.cpython-37.pyc
100755 → 100644
Binary file not shown.
45 changes: 45 additions & 0 deletions crimsonbouquet/content/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,48 @@

# Create your models here.

class Contributor(models.Model):
first_name = models.CharField(max_length = 100)
last_name = models.CharField(max_length = 100)
middle_name = models.CharField(max_length = 100)

bio_text = models.CharField(blank = True, max_length = 500, null = True)

TITLE_CHOICES = (
('cstaff', 'Crimson staff writer'),
('contrib', 'Contributing writer'),
('photog', 'Photographer'),
('design', 'Designer'),
('editor', 'Editor'),
('opinion', 'Crimson opinion writer'),
('opinion_contrib', 'Contributing opinion writer'),
('sponsored_contrib', 'Sponsored Contributor'),
)

_title = models.CharField(
blank=True, null=True, max_length=70, choices=TITLE_CHOICES,
verbose_name='title'
)

@property
def title(self):
TITLE_CHOICES_MAP = dict(self.TITLE_CHOICES)

if self._title:
return TITLE_CHOICES_MAP[self._title]
# generate default title
else:
return 'Contributor'

def __str__(self):
return " ".join(filter(None, [self.first_name, self.middle_name, self.last_name]))

class Article(models.Model):
title = models.CharField(max_length = 200)
contributors = models.ManyToManyField(Contributor, related_name = 'content')
text = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(max_length = 70)

def __str__(self):
return self.title
32 changes: 32 additions & 0 deletions crimsonbouquet/content/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import graphene
from graphene_django import DjangoObjectType

from content.models import Contributor, Article

class ContributorGQL(DjangoObjectType):
class Meta:
model = Contributor

class ArticleGQL(DjangoObjectType):
class Meta:
model = Article

class Query(graphene.ObjectType):
contributor = graphene.Field(ContributorGQL, id = graphene.Int())
content = graphene.Field(ArticleGQL, slug = graphene.String(required = True))
all_content = graphene.List(ArticleGQL)
all_contributors = graphene.List(ContributorGQL)

def resolve_content(self, info, slug):
return Article.objects.get(slug = slug)

def resolve_contributor(self, info, id):
return Contributor.objects.get(pk = id)

def resolve_all_content(self, info):
return Article.objects.all()

def resolve_all_contributors(self, info):
return Contributor.objects.all()

schema = graphene.Schema(query=Query)
9 changes: 9 additions & 0 deletions crimsonbouquet/content/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path

from . import views

urlpatterns = [
path('', views.index, name='index'),
path('contrib/<int:contrib_id>/', views.display_contributor, name = "display_contributor"),
path('article/<slug:slug>/', views.display_article, name = "display_article")
]
13 changes: 13 additions & 0 deletions crimsonbouquet/content/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
from django.shortcuts import render, get_object_or_404

from .models import Contributor, Article
from django.http import HttpResponse

# Create your views here.
def index(request):
return HttpResponse("Hello lah.")

def display_contributor(request, contrib_id):
contributor = get_object_or_404(Contributor, pk = contrib_id)
return render(request, "content/contributor.html", {"contributor": contributor})

def display_article(request, slug):
article = get_object_or_404(Article, slug = slug)
return render(request, "content/article.html", {"article": article})
Binary file modified crimsonbouquet/crimsonbouquet/__pycache__/__init__.cpython-37.pyc
100755 → 100644
Binary file not shown.
Binary file modified crimsonbouquet/crimsonbouquet/__pycache__/settings.cpython-37.pyc
100755 → 100644
Binary file not shown.
Binary file modified crimsonbouquet/crimsonbouquet/__pycache__/urls.cpython-37.pyc
100755 → 100644
Binary file not shown.
Binary file modified crimsonbouquet/crimsonbouquet/__pycache__/wsgi.cpython-37.pyc
100755 → 100644
Binary file not shown.
9 changes: 5 additions & 4 deletions crimsonbouquet/crimsonbouquet/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'graphene_django'
'graphene_django',
'content.apps.ContentConfig'
]

MIDDLEWARE = [
Expand Down Expand Up @@ -114,9 +115,9 @@

USE_TZ = True

# GRAPHENE = {
# 'SCHEMA': 'content.schema.schema'
# }
GRAPHENE = {
'SCHEMA': 'content.schema.schema'
}

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
Expand Down
4 changes: 2 additions & 2 deletions crimsonbouquet/crimsonbouquet/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from graphene_django.views import GraphQLView

urlpatterns = [
# path('content/', include('content.urls')),
path('content/', include('content.urls')),
path('admin/', admin.site.urls),
# path("graphql/", csrf_exempt(GraphQLView.as_view(graphiql=True))),
path("graphql/", csrf_exempt(GraphQLView.as_view(graphiql=True))),
]
Binary file added crimsonbouquet/db.sqlite3
Binary file not shown.