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

#744 administrative panel granting administrator rights #753

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
40 changes: 40 additions & 0 deletions BackEnd/administration/migrations/0004_moderationemail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 4.2.3 on 2024-09-01 10:51

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


class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("administration", "0003_autoapprovetask"),
]

operations = [
migrations.CreateModel(
name="ModerationEmail",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"email_moderation",
models.EmailField(max_length=254, unique=True),
),
(
"superuser",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
9 changes: 9 additions & 0 deletions BackEnd/administration/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from django.core.exceptions import ValidationError
from authentication.models import CustomUser
from profiles.models import Profile


Expand Down Expand Up @@ -32,3 +33,11 @@ def get_auto_moderation_hours(cls):
class AutoapproveTask(models.Model):
celery_task_id = models.CharField()
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)


class ModerationEmail(models.Model):
superuser = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
email_moderation = models.EmailField(unique=True)

def __str__(self):
return self.superuser.email
10 changes: 9 additions & 1 deletion BackEnd/administration/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Profile,
Region,
)
from .models import AutoModeration
from .models import AutoModeration, ModerationEmail


class AdminRegionSerialaizer(serializers.ModelSerializer):
Expand Down Expand Up @@ -128,3 +128,11 @@ class AutoModerationHoursSerializer(serializers.ModelSerializer):
class Meta:
model = AutoModeration
fields = ("auto_moderation_hours",)


class ModerationEmailSerializer(serializers.ModelSerializer):
email_moderation = serializers.EmailField()

class Meta:
model = ModerationEmail
fields = ["email_moderation"]
2 changes: 2 additions & 0 deletions BackEnd/administration/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
UsersListView,
UserDetailView,
AutoModerationHoursView,
ModerationEmailView,
)

app_name = "administration"
Expand All @@ -20,4 +21,5 @@
AutoModerationHoursView.as_view(),
name="automoderation_hours",
),
path("email/", ModerationEmailView.as_view(), name="moderation-email"),
]
11 changes: 10 additions & 1 deletion BackEnd/administration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
AdminUserListSerializer,
AdminUserDetailSerializer,
AutoModerationHoursSerializer,
ModerationEmailSerializer,
)
from administration.pagination import ListPagination
from administration.models import AutoModeration
from administration.models import AutoModeration, ModerationEmail
from authentication.models import CustomUser
from profiles.models import Profile
from .permissions import IsStaffUser, IsStaffUserOrReadOnly
Expand Down Expand Up @@ -122,3 +123,11 @@ class AutoModerationHoursView(RetrieveUpdateAPIView):

def get_object(self):
return AutoModeration.get_auto_moderation_hours()


class ModerationEmailView(RetrieveUpdateAPIView):
queryset = ModerationEmail.objects.all()
serializer_class = ModerationEmailSerializer

def get_object(self):
return ModerationEmail.objects.first()
3 changes: 2 additions & 1 deletion BackEnd/utils/moderation/send_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.template.loader import render_to_string
from administration.models import AutoModeration
from .encode_decode_id import encode_id
from administration.models import ModerationEmail


EMAIL_CONTENT_SUBTYPE = "html"
Expand Down Expand Up @@ -77,7 +78,7 @@ def send_moderation_email(profile, banner, logo, content_is_deleted):
body=email_body,
from_email=settings.EMAIL_HOST_USER,
to=[
settings.EMAIL_HOST_USER,
ModerationEmail.objects.first(),
],
)

Expand Down
4 changes: 4 additions & 0 deletions FrontEnd/src/components/adminPage/AdminPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import MainPage from './mainPage/MainPage';
import { useAuth } from '../../hooks';
import Loader from '../loader/Loader';
import AutoApproveDelay from './auto-approve-delay/AutoApproveDelay';
import ModerationEmail from './detail-view/ModerationEmail';



function AdminPage() {
const { isLoading, isAuth, isStaff } = useAuth();
Expand All @@ -24,6 +27,7 @@ function AdminPage() {
<Route path="/profiles" element={<ProfilesTable />} />
<Route path="/profile/:id" element={<ProfileDetail />} />
<Route path="/automoderation" element={<AutoApproveDelay />} />
<Route path="/email" element={<ModerationEmail />} />
</>
) : (
<Route path="/customadmin/" />
Expand Down
54 changes: 54 additions & 0 deletions FrontEnd/src/components/adminPage/detail-view/ModerationEmail.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useState } from 'react';
import { toast } from 'react-toastify';
import { Tooltip } from 'antd';
import useSWR from 'swr';
import axios from 'axios';
import css from './ModerationEmail.module.css';

const ModerationEmail = () => {
const fetcher = url => axios.get(url).then(res => res.data);
const url = `${process.env.REACT_APP_BASE_API_URL}/api/admin/email/`;
const { data, mutate } = useSWR(url, fetcher);

// Define email and setEmail using useState
const [email, setEmail] = useState(null);
const [error, setError] = useState(null);

// Update email state when data is fetched
if (data && data.email_moderation && email === null) {
setEmail(data.email_moderation);
}

const handleInputChange = (e) => {
let value = e.target.value;
setError(null);
setEmail(value);
};

const handleSubmit = () => {
// Assuming 'delay' is a typo and should be 'email'
!error && axios.patch(`${process.env.REACT_APP_BASE_API_URL}/api/admin/email/`, { 'email_moderation': email })
.then(() => {
toast.success('Зміни успішно застосовано.');
mutate({ ...data, email_moderation: email });
})
.catch((e) => toast.error(e.message));
};

return (
<div className={css['moderation_email-section']}>
<Tooltip
title={'Введіть Email'}
placement="top"
pointAtCenter={true}>
<input className={css['moderation_email-input']} type="email" onChange={handleInputChange} value={email || ''} />
</Tooltip>
{error &&
<p className={css['error-message']}>{error}</p>}
<button className={css['save-button']} onClick={handleSubmit}>Змінити</button>
</div>
);

};

export default ModerationEmail;
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
.moderation_email-section {
display: flex;
flex-direction: column;
padding: 20px;
gap: 10px;
}

.moderation_email-input {
width: 250px;
height: 22px;
padding: 0 2px;
}

.save-button {
display: flex;
padding: 5px 15px;
justify-content: center;
align-items: center;
gap: 10px;
width: 150px;

border-radius: 4px;
border: 1px solid #1F9A7C;
background: #1F9A7C;

box-shadow: 0px 2px 0px 0px rgba(0, 0, 0, 0.04);

/* Button Text */
color: #FFF;
text-align: center;
font-feature-settings: 'calt' off;
text-decoration: none;
font-family: var(--font-main);
font-size: 16px;
font-style: normal;
font-weight: 600;
line-height: 20px;
letter-spacing: -0.16px;
cursor: pointer;
}

.error-message {
color: rgb(212, 21, 21);
font-family: var(--font-main);
font-size: 14px;
}
5 changes: 5 additions & 0 deletions FrontEnd/src/components/adminPage/menu/Menu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const MENU = [
id: 'am3',
title: 'Зміна часу автомодерації',
link: '/customadmin/automoderation/'
},
{
id: 'am4',
title: 'Email',
link: '/customadmin/email/'
}
];

Expand Down
Loading