Skip to content

Commit

Permalink
Merge pull request #40 from swe574-spring23/SpaceModelUpdates
Browse files Browse the repository at this point in the history
DEV | My Own Spaces and Space Policy Update
  • Loading branch information
sehmuzc committed Mar 14, 2023
2 parents 6904d97 + aad3848 commit f0d29a9
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 66 deletions.
23 changes: 23 additions & 0 deletions posts/migrations/0005_auto_20230313_2300.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.1.1 on 2023-03-13 20:00

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('posts', '0004_auto_20230313_2153'),
]

operations = [
migrations.AlterField(
model_name='comment',
name='id',
field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='post',
name='id',
field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
]
11 changes: 5 additions & 6 deletions posts/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@


</head>

<body>
<nav class="py-2 bg-light border-bottom">
<div class="container d-flex flex-wrap justify-content-center">
Expand All @@ -36,13 +35,13 @@
</a>
</li>
<li><a href="/home" class="nav-link px-2 text-white">Main</a></li>
<li><a href="/spaces" class="nav-link px-2 text-white">Spaces</a></li>
<li><a href="/spaces" class="nav-link px-2 text-white">All Spaces</a></li>
<li><a href="/spaces/myspaces" class="nav-link px-2 text-white">My Spaces</a></li>
</ul>
<a href="{% url 'post_new' %}" class="btn btn-warning"> BOX_it =

{% include './icons/box.svg' %}
</a>
<a href="{% url 'create_space' %}" class="btn btn-warning"> SPACE_it
<a href="{% url 'create_space' %}" class="btn btn-warning"> Create Space
</a>

<form class="mb-3 mb-lg-0 me-lg-3 d-flex align-items-center" role="search" method=POST
Expand All @@ -65,7 +64,7 @@
</header>

{% comment %}<div class="page-header">
<h1><a href="/">SWE-573 Project</a></h1>
<h1><a href="/">SWE-574 Project</a></h1>
<div class="links">
{% if user.is_authenticated %}
<a href="{% url 'logout' %}">Logout</a>
Expand All @@ -91,7 +90,7 @@ <h1><a href="/">SWE-573 Project</a></h1>

<footer class="footer mt-5 py-3 bg-light">
<div class="container">
<span class="text-muted">SWE-573</span>
<span class="text-muted">SWE-574</span>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"
Expand Down
32 changes: 28 additions & 4 deletions spaces/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,38 @@ class Meta:
fields = [
"name",
"description",
"members",
"moderators",
"is_all_members_post_allowed",
"is_only_moderators_post_allowed",
"posting_permission",
]


class SpaceForm(forms.ModelForm):
class Meta:
model = Post
fields = ("title", "link", "text")


POSTING_PERMISSION_CHOICES = (
('all', 'Any member can post'),
('granted', 'Only granted members can post'),
('moderators', 'Only moderators can post'),
)


class SpacePolicyForm(forms.ModelForm):
class Meta:
model = Space
fields = ['posting_permission', 'granted_members']
widgets = {
'posting_permission': forms.RadioSelect(attrs={'class': 'form-check-input'}),
'granted_members': forms.CheckboxSelectMultiple(attrs={'class': 'form-check-input'}),
}
labels = {
'posting_permission': 'Posting Permission',
'granted_members': 'Granted Members',
}

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance.posting_permission != 'granted':
self.fields['granted_members'].widget = forms.HiddenInput()
self.fields['posting_permission'].choices = POSTING_PERMISSION_CHOICES
38 changes: 38 additions & 0 deletions spaces/migrations/0002_auto_20230313_2300.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 3.1.1 on 2023-03-13 20:00

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('spaces', '0001_initial'),
]

operations = [
migrations.RemoveField(
model_name='space',
name='is_all_members_post_allowed',
),
migrations.RemoveField(
model_name='space',
name='is_only_moderators_post_allowed',
),
migrations.AddField(
model_name='space',
name='granted_members',
field=models.ManyToManyField(blank=True, related_name='granted_spaces', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='space',
name='posting_permission',
field=models.CharField(choices=[('all', 'Any member can post'), ('granted', 'Only granted members can post'), ('moderators', 'Only moderators can post')], default='all', max_length=47),
),
migrations.AlterField(
model_name='space',
name='id',
field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
]
26 changes: 24 additions & 2 deletions spaces/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,39 @@


class Space(models.Model):
POSTING_PERMISSION_CHOICES = (
('all', 'Any member can post'),
('granted', 'Only granted members can post'),
('moderators', 'Only moderators can post'),
)

owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name="owned_spaces")
name = models.CharField(max_length=200)
description = models.TextField()
members = models.ManyToManyField(User, related_name="spaces")
moderators = models.ManyToManyField(User, related_name="moderated_spaces")
is_all_members_post_allowed = models.BooleanField(default=True)
is_only_moderators_post_allowed = models.BooleanField(default=False)
posting_permission = models.CharField(choices=POSTING_PERMISSION_CHOICES, max_length=47, default='all')
granted_members = models.ManyToManyField(User, related_name="granted_spaces", blank=True)
created_date = models.DateTimeField(default=timezone.now)

class Meta:
ordering = ("-created_date",)

def get_granted_members(self):
return self.granted_members.all()

def get_moderators(self):
return self.moderators.all()

def can_member_post(self, user):
if self.posting_permission == 'all':
return True
elif self.posting_permission == 'granted':
return user in self.get_granted_members() or user == self.owner
elif self.posting_permission == 'moderators':
return user in self.get_moderators() or user == self.owner
else:
return False

def __str__(self):
return self.name
22 changes: 3 additions & 19 deletions spaces/templates/create_space.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,10 @@ <h1>Create a new space</h1>
</div>

<div class="form-group">
<label for="id_members">Members:</label>
{{ form.members }}
</div>

<div class="form-group">
<label for="id_moderators">Moderators:</label>
{{ form.moderators }}
</div>

<div class="form-group">
<label for="id_is_all_members_post_allowed">Allow all members to post:</label>
{{ form.is_all_members_post_allowed }}
</div>

<div class="form-group">
<label for="id_is_only_moderators_post_allowed">Allow only moderators to post:</label>
{{ form.is_only_moderators_post_allowed }}
<label for="id_posting_permission">Posting Permission:</label>
{{ form.posting_permission }}
</div>

<button type="submit" class="btn btn-primary">Create space</button>
</form>
{% endblock %}

{% endblock %}
13 changes: 13 additions & 0 deletions spaces/templates/my_spaces_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends 'base.html' %}
{% block content %}
<h1>My Spaces</h1>
{% if spaces %}
<ul>
{% for space in spaces %}
<li><a href="{% url 'space_detail' pk=space.pk %}">{{ space.name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>You don't have any spaces yet.</p>
{% endif %}
{% endblock %}
36 changes: 20 additions & 16 deletions spaces/templates/space_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@
<h1>{{ space.name }}</h1>
<p>{{ space.description }}</p>
<hr>
{% if space.posts.all %}
{% for post in space.posts.all %}
<div class="post">
<h2><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>
<p>{{ post.text }}</p>
<p>Published by {{ post.author }} on {{ post.published_date }}</p>
{% if post.image %}
<img src="{{ post.image.url }}" alt="Post image">
{% endif %}
<p><a href="{% url 'like_post' pk=post.pk %}">Like</a> <span class="like-count">{{ post.likes.count }}</span></p>
</div>
{% endfor %}
{% else %}
<p>No posts found.</p>
{% endif %}
{% endblock %}
<div class="row row-cols-1 row-cols-sm-1 row-cols-mr-6 g-3">
{% if space.posts.all %}
{% for post in space.posts.all %}
<div class="col">
<div class="post">
<h2><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>
<p>{{ post.text }}</p>
<p>Published by {{ post.author }} on {{ post.published_date }}</p>
{% if post.image %}
<img src="{{ post.image.url }}" alt="Post image">
{% endif %}
<p><a href="{% url 'like_post' pk=post.pk %}">Like</a> <span class="like-count">{{ post.likes.count }}</span></p>
</div>
</div>
{% endfor %}
{% else %}
<p>No posts found.</p>
{% endif %}
</div>
{% endblock %}
38 changes: 23 additions & 15 deletions spaces/templates/space_list.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
{% extends 'base.html' %}

{% block content %}
<h1>Spaces</h1>
<ul>
{% for space in spaces %}
<li>
<h2>{{ space.name }}</h2>
<p>{{ space.description }}</p>
<p>Owner: {{ space.owner.username }}</p>
<p>Members: {{ space.members.count }}</p>
<p>Moderators: {{ space.moderators.count }}</p>
<p>Posts: {{ space.posts.count }}</p>
<a href="{% url 'space_detail' space.id %}">View Space</a>
</li>
{% endfor %}
</ul>
<h1>Spaces</h1>
<div class="row row-cols-1 row-cols-md-3 g-4">
{% for space in spaces %}
<div class="col">
<div class="card h-100">
<div class="card-body">
<h2 class="card-title">{{ space.name }}</h2>
<p class="card-text">{{ space.description }}</p>
<ul class="list-group list-group-flush">
<li class="list-group-item">Owner: {{ space.owner.username }}</li>
<li class="list-group-item">Members: {{ space.members.count }}</li>
<li class="list-group-item">Moderators: {{ space.moderators.count }}</li>
<li class="list-group-item">Posts: {{ space.posts.count }}</li>
</ul>
<a href="{% url 'space_detail' space.id %}" class="btn btn-primary mt-3">View Space</a>
{% if user.is_authenticated and user == space.owner %}
<a href="{% url 'space_policies' space.id %}" class="btn btn-secondary mt-3">Define Policies</a>
{% endif %}
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock %}

17 changes: 17 additions & 0 deletions spaces/templates/space_policies.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends 'base.html' %}

{% block content %}
<div style="border: 1px solid black; padding: 10px;">
<h1>Space Policies</h1>
<form method="post">
{% csrf_token %}
<div class="mb-3">
<label for="{{ form.posting_permission.id_for_label }}" class="form-label">Posting Permission:</label>
<div class="form-check form-check-inline">
{{ form.posting_permission }}
</div>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
</div>
{% endblock %}
6 changes: 4 additions & 2 deletions spaces/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django.urls import path

from spaces.views import create_space, space_detail, space_list
from spaces.views import create_space, space_detail, space_list, my_spaces_list, space_policies

urlpatterns = [
path("spaces/", space_list, name="space_list"),
path("spaces/new/", create_space, name="create_space"),
path("spaces/<int:pk>/", space_detail, name="space_detail"),
path("spaces/myspaces/", my_spaces_list, name="my_spaces_list"),
path("spaces/<int:pk>/policies/", space_policies, name="space_policies"),

]

0 comments on commit f0d29a9

Please sign in to comment.