-
Notifications
You must be signed in to change notification settings - Fork 79
✨ Adding importer models #111
base: develop
Are you sure you want to change the base?
Changes from 13 commits
625482a
3db638d
fc20123
cf3e487
f5d169b
d9d3bc2
fd81a2a
61a237d
a0c9843
64b562b
04d86d2
34d6624
50a1fbe
b00bb95
3ebb620
1a0c798
eefb3f2
0ea5ef8
46d74a7
b58a397
be5e82d
cf48fe1
2f97590
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Generated by Django 2.1.5 on 2019-01-11 17:42 | ||
|
|
||
| from django.db import migrations, models | ||
| import django.db.models.deletion | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ('openbook_posts', '0001_initial'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='Import', | ||
| fields=[ | ||
| ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('created', models.DateTimeField(editable=False)), | ||
| ], | ||
| ), | ||
| migrations.CreateModel( | ||
| name='ImportedPost', | ||
| fields=[ | ||
| ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('data_import', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='imported_posts', to='openbook_importer.Import')), | ||
| ('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='openbook_posts.Post')), | ||
| ], | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Generated by Django 2.1.5 on 2019-01-18 19:57 | ||
|
|
||
| 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), | ||
| ('openbook_importer', '0001_initial'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='ImportedFriend', | ||
| fields=[ | ||
| ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('friend_hash', models.CharField(max_length=64, unique=True)), | ||
| ('data_import', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='friend_import', to='openbook_importer.Import')), | ||
| ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='imported_friends', to=settings.AUTH_USER_MODEL)), | ||
| ], | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # Generated by Django 2.1.5 on 2019-01-25 11:16 | ||
|
|
||
| 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), | ||
| ('openbook_importer', '0002_importedfriend'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RemoveField( | ||
| model_name='importedfriend', | ||
| name='data_import', | ||
| ), | ||
| migrations.RemoveField( | ||
| model_name='importedfriend', | ||
| name='user', | ||
| ), | ||
| migrations.AddField( | ||
| model_name='importedfriend', | ||
| name='user1', | ||
| field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='imported_friends', to=settings.AUTH_USER_MODEL), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='importedfriend', | ||
| name='user2', | ||
| field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='connected_friend', to=settings.AUTH_USER_MODEL), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Generated by Django 2.1.5 on 2019-02-02 12:20 | ||
|
|
||
| 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), | ||
| ('openbook_importer', '0003_auto_20190125_1216'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name='import', | ||
| name='user', | ||
| field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, related_name='import_owner', to=settings.AUTH_USER_MODEL), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Generated by Django 2.1.5 on 2019-02-02 15:44 | ||
|
|
||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
| import django.db.models.deletion | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('openbook_importer', '0004_import_user'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name='import', | ||
| name='user', | ||
| field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Generated by Django 2.1.5 on 2019-02-02 15:47 | ||
|
|
||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
| import django.db.models.deletion | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('openbook_importer', '0005_auto_20190202_1644'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name='import', | ||
| name='user', | ||
| field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Generated by Django 2.1.5 on 2019-02-02 17:48 | ||
|
|
||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
| import django.db.models.deletion | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('openbook_importer', '0006_auto_20190202_1647'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name='import', | ||
| name='user', | ||
| field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='imports', to=settings.AUTH_USER_MODEL), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| from django.db import models | ||
| from django.utils import timezone | ||
|
|
||
| from openbook_auth.models import User | ||
| from openbook_posts.models import Post | ||
|
|
||
|
|
||
| class Import(models.Model): | ||
|
|
||
| created = models.DateTimeField(editable=False) | ||
| user = models.ForeignKey(User, on_delete=models.CASCADE, | ||
| related_name='imports', null=True) | ||
|
|
||
| @classmethod | ||
| def create_import(cls, user): | ||
| imported = Import.objects.create(user=user) | ||
|
|
||
| return imported | ||
|
|
||
| def save(self, *args, **kwargs): | ||
| ''' On save, update timestamps ''' | ||
| if not self.id and not self.created: | ||
| self.created = timezone.now() | ||
|
|
||
| return super(Import, self).save(*args, **kwargs) | ||
|
|
||
|
|
||
| class ImportedPost(models.Model): | ||
|
|
||
| data_import = models.ForeignKey(Import, on_delete=models.CASCADE, | ||
| related_name='imported_posts') | ||
| post = models.ForeignKey(Post, on_delete=models.CASCADE) | ||
|
|
||
| @classmethod | ||
| def create_imported_post(cls, post, data_import): | ||
| imported_post = ImportedPost.objects.create(post=post, | ||
| data_import=data_import) | ||
|
|
||
| return imported_post | ||
|
|
||
|
|
||
| class ImportedFriend(models.Model): | ||
|
|
||
| # check if both uid fields are null, delete row | ||
| friend_hash = models.CharField(max_length=64, unique=True) | ||
| user1 = models.ForeignKey(User, on_delete=models.SET_NULL, | ||
| related_name='imported_friends', | ||
| null=True) | ||
| user2 = models.ForeignKey(User, on_delete=models.SET_NULL, | ||
| related_name='connected_friend', | ||
| null=True) | ||
|
|
||
| @classmethod | ||
| def find_friend(cls, friend_hash, user): | ||
|
|
||
| friend = ImportedFriend.objects.filter(friend_hash=friend_hash) | ||
|
|
||
| if friend.exists(): | ||
| friend = friend[0] | ||
|
|
||
| if friend.user1_id == user.pk and friend.user2_id: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider simplifying this complex logical expression. |
||
| return True | ||
|
|
||
| elif friend.user2_id == user.pk and friend.user1_id: | ||
| return True | ||
|
|
||
| elif friend.user1_id == user.pk and not friend.user2_id: | ||
| return True | ||
|
|
||
| elif friend.user2_id == user.pk and not friend.user1_id: | ||
| return True | ||
|
|
||
| elif not friend.user1_id and friend.user2_id != user.pk: | ||
| friend.user1 = user | ||
| friend.save() | ||
|
|
||
| return True | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid too many |
||
|
|
||
| elif not friend.user2_id and friend.user1_id != user.pk: | ||
| friend.user2 = user | ||
| friend.save() | ||
|
|
||
| return True | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid too many
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume the |
||
|
|
||
| else: | ||
| return False | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid too many |
||
|
|
||
| @classmethod | ||
| def create_imported_friend(cls, friend_hash, user1): | ||
| imported_friend = ImportedFriend.objects.create( | ||
| friend_hash=friend_hash, | ||
| user1=user1) | ||
|
|
||
| return imported_friend | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,18 @@ | ||
| from rest_framework import serializers | ||
| from openbook_importer.models import Import | ||
|
|
||
|
|
||
| class ZipfileSerializer(serializers.Serializer): | ||
|
|
||
| serializers.FileField(max_length=20, required=True, | ||
| allow_empty_file=False) | ||
|
|
||
|
|
||
| class ImportSerializer(serializers.ModelSerializer): | ||
|
|
||
| class Meta: | ||
| model = Import | ||
| fields = ( | ||
| 'id', | ||
| 'created' | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if there's going to be multiple friends, call it
friends... if not you can useImportedFriend.objects.getto get just one then you dont have to usefriend[0].. Also the get method raises aImportedFriend.DoesNotExisterror if nothing is found,