Skip to content

Commit a5a1375

Browse files
authored
Merge branch 'master' into mandelbrot
2 parents 479a473 + c243939 commit a5a1375

File tree

360 files changed

+8525
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

360 files changed

+8525
-2
lines changed

dwitter-part-1/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Dwitter - Your tiny social network built with Django
2+
3+
This web app models the basic functionality of Twitter.
4+
5+
You can:
6+
7+
- **Post** short text-based messages
8+
- **View** all users on the platform
9+
- **Follow** and unfollow other users
10+
- **Inspect** a feed of messages from users that you follow
11+
12+
Follow the [step-by-step instructions](https://realpython.com/django-social-network-1/) on _Real Python_ to build your own version of Dwitter.
13+
14+
## Setup
15+
16+
You can run the provided example project on your local machine by following the steps outlined below.
17+
18+
Create a new virtual environment:
19+
20+
```bash
21+
python3 -m venv venv
22+
```
23+
24+
Activate the virtual environment:
25+
26+
```bash
27+
source ./venv/bin/activate
28+
```
29+
30+
Navigate to the folder for the step you're currently on.
31+
32+
Install the dependencies for this project if you haven't installed them yet:
33+
34+
```bash
35+
(venv) $ python -m pip install -r requirements.txt
36+
```
37+
38+
Make and apply the migrations for the project to build your local database:
39+
40+
```bash
41+
(venv) $ python manage.py makemigrations
42+
(venv) $ python manage.py migrate
43+
```
44+
45+
Create a superuser that allows you to log in to your Django admin portal:
46+
47+
```bash
48+
(venv) $ python manage.py createsuperuser
49+
```
50+
51+
Run the Django development server:
52+
53+
```bash
54+
(venv) $ python manage.py runserver
55+
```
56+
57+
Navigate to `http://localhost:8000/admin` and log in with your superuser credentials. You can create users through the Django admin and explore your social media platform with multiple users.

dwitter-part-1/source_code_final/dwitter/__init__.py

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from django.contrib import admin
2+
from django.contrib.auth.models import User, Group
3+
from .models import Profile
4+
5+
6+
class ProfileInline(admin.StackedInline):
7+
model = Profile
8+
9+
10+
class UserAdmin(admin.ModelAdmin):
11+
model = User
12+
fields = ["username"]
13+
inlines = [ProfileInline]
14+
15+
16+
admin.site.unregister(User)
17+
admin.site.register(User, UserAdmin)
18+
admin.site.unregister(Group)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class DwitterConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "dwitter"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Generated by Django 3.2.8 on 2021-10-08 09:55
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
import django.db.models.deletion
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
initial = True
11+
12+
dependencies = [
13+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
14+
]
15+
16+
operations = [
17+
migrations.CreateModel(
18+
name='Profile',
19+
fields=[
20+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
21+
('follows', models.ManyToManyField(blank=True, related_name='followed_by', to='dwitter.Profile')),
22+
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
23+
],
24+
),
25+
]

dwitter-part-1/source_code_final/dwitter/migrations/__init__.py

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from django.db import models
2+
from django.db.models.signals import post_save
3+
from django.dispatch import receiver
4+
from django.contrib.auth.models import User
5+
6+
7+
class Profile(models.Model):
8+
user = models.OneToOneField(User, on_delete=models.CASCADE)
9+
follows = models.ManyToManyField(
10+
"self", related_name="followed_by", symmetrical=False, blank=True
11+
)
12+
13+
def __str__(self):
14+
return self.user.username
15+
16+
17+
@receiver(post_save, sender=User)
18+
def create_profile(sender, instance, created, **kwargs):
19+
if created:
20+
user_profile = Profile(user=instance)
21+
user_profile.save()
22+
user_profile.follows.add(instance.profile)
23+
user_profile.save()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# from django.test import TestCase
2+
3+
# Create your tests here.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# from django.shortcuts import render
2+
3+
# Create your views here.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
"""Run administrative tasks."""
9+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "social.settings")
10+
try:
11+
from django.core.management import execute_from_command_line
12+
except ImportError as exc:
13+
raise ImportError(
14+
"Couldn't import Django. Are you sure it's installed and "
15+
"available on your PYTHONPATH environment variable? Did you "
16+
"forget to activate a virtual environment?"
17+
) from exc
18+
execute_from_command_line(sys.argv)
19+
20+
21+
if __name__ == "__main__":
22+
main()

0 commit comments

Comments
 (0)