Skip to content

Commit

Permalink
В create_user перешёл на get_or_create. Переписал логику создания…
Browse files Browse the repository at this point in the history
…/редактирования постав, завязал всё на `save`, чтобы было проще вешать триггеры для того же manticore, если будем внедрять. Не нравится 93 строчка ;((

Добавил описание в инфо для разрабов
  • Loading branch information
trin4ik committed Jun 20, 2024
1 parent 77271d5 commit d58a38e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
47 changes: 23 additions & 24 deletions authn/management/commands/import_posts_to_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def add_arguments(self, parser):
"--pages",
type=int,
default=1,
help="Количество страниц, забираемых и фида",
help="Количество страниц, забираемых из фида",
)

parser.add_argument(
Expand Down Expand Up @@ -57,12 +57,12 @@ def handle(self, *args, **options):
if not (item['_club']['is_public']):
continue

[created, author] = create_user(item['authors'][0])
author, created = create_user(item['authors'][0])
if created:
result['user_created'] += 1
self.stdout.write(" 👤 \"{}\" пользователь создан".format(author.full_name))

post_data = dict(
defaults = dict(
id=item['id'],
title=item['title'],
type=item['_club']['type'],
Expand All @@ -71,7 +71,6 @@ def handle(self, *args, **options):
html=markdown_text(item['content_text']),
image=author.avatar, # хак для постов типа "проект", чтобы не лазить по вастрику лишний раз
created_at=item['date_published'],
last_activity_at=item['date_modified'],
comment_count=item['_club']['comment_count'],
view_count=item['_club']['view_count'],
upvotes=item['_club']['upvotes'],
Expand All @@ -86,18 +85,20 @@ def handle(self, *args, **options):
coauthors=[]
)

if Post.objects.filter(pk=item['id']).exists():
if options['force']:
Post.objects.filter(pk=item['id']).update(**post_data)
else:
result['post_exists'] += 1
self.stdout.write(" 📌 \"{}\" уже существует".format(item['title']))
continue
else:
post = Post.objects.create(**post_data)
post.created_at = item['date_published']
post.last_activity_at = item['date_modified']
post.save()
exists = False
try:
post = Post.objects.get(id=item['id'])
exists = True
except Post.DoesNotExist:
post = Post.objects.create(**defaults)

if exists and not options['force']:
result['post_exists'] += 1
self.stdout.write(" 📌 \"{}\" уже существует".format(item['title']))
continue

post.__dict__.update(defaults)
post.save()

result['post_created'] += 1
self.stdout.write(" 📄 \"{}\" запись создана".format(item['title']))
Expand All @@ -113,25 +114,23 @@ def create_user(author):
split = author['url'].split('/')
slug = split[-2]

if User.objects.filter(slug=slug).count() > 0:
return [False, User.objects.get(slug=slug)]

user = User.objects.create(
defaults = dict(
slug=slug,
avatar=author['avatar'],
email=random_string(30),
full_name=author['name'],
company="FAANG",
position="Team Lead конечно",
balance=10000,
membership_started_at=datetime.utcnow(),
membership_expires_at=datetime.utcnow() + timedelta(days=365 * 100),
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
membership_started_at=datetime.now(),
membership_expires_at=datetime.utcnow() + timedelta(days=365 * 100),
is_email_verified=True,
moderation_status=User.MODERATION_STATUS_APPROVED,
roles=[],
)
user.save()

return [True, user]
user, created = User.objects.get_or_create(slug=slug, defaults=defaults)

return user, created
22 changes: 22 additions & 0 deletions docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,25 @@ To run telegram bot you have to:
## Docker-compose

Check out our [docker-compose.yml](https://github.com/vas3k/vas3k.club/blob/master/docker-compose.yml) to understand the infrastructure.

## Load posts from main vas3k.club to dev/local database

Sometimes you need fill saome posts/users data from real project. For this case you can use `import_posts_to_dev` command.

Command fetch https://vas3k.club/feed.json and copy `is_public=True` posts to your database:
```bash
# fetch first page
$ python3 manage.py import_posts_to_dev
# fetch first 10 pages
$ python3 manage.py import_posts_to_dev --pages 10
# fetch 10 pages, starts from page 5
$ python3 manage.py import_posts_to_dev --pages 10 --skip 5
# fetch 10 pages, starts from page 5 and update exists posts
$ python3 manage.py import_posts_to_dev --pages 10 --skip 5 --force
# if use docker-compose
$ docker exec -it club_app python3 manage.py import_posts_to_dev --pages 2
```

0 comments on commit d58a38e

Please sign in to comment.