Skip to content

Commit

Permalink
Added kwargs to create and update func
Browse files Browse the repository at this point in the history
  • Loading branch information
ZuluPro committed Dec 21, 2016
1 parent a13538c commit 1c28c4a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dj_web_rich_object/__init__.py
@@ -1,5 +1,5 @@
"""Reusable app made for handle web rich objects"""
VERSION = (0, 2, 1)
VERSION = (0, 2, 2)
__version__ = '.'.join([str(i) for i in VERSION])
__author__ = 'Anthony Monthe (ZuluPro)'
__email__ = 'anthony.monthe@gmail.com'
Expand Down
18 changes: 16 additions & 2 deletions dj_web_rich_object/admin/modeladmins.py
Expand Up @@ -17,11 +17,25 @@ class WebRichObjectAdmin(admin.ModelAdmin):
(None, {
'classes': ('wide',),
'fields': (
('title', 'site_name', 'author'),
('title', 'site_name'),
'author',
'description',
'image',
)
}),
(_("Type"), {
'fields': (
('type', 'subtype'),
)
}),
(_("URLs"), {
'fields': (
('url', 'base_url'),
'image',
)
}),
(_("Dates"), {
'fields': (
('created_time', 'published_time', 'modified_time'),
)
}),
)
Expand Down
29 changes: 29 additions & 0 deletions dj_web_rich_object/migrations/0006_auto_20161219_1809.py
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('dj_web_rich_object', '0005_auto_20161215_0835'),
]

operations = [
migrations.AddField(
model_name='webrichobject',
name='created_time',
field=models.DateTimeField(default=None, null=True, verbose_name='created time', blank=True),
),
migrations.AddField(
model_name='webrichobject',
name='modified_time',
field=models.DateTimeField(default=None, null=True, verbose_name='modified time', blank=True),
),
migrations.AddField(
model_name='webrichobject',
name='published_time',
field=models.DateTimeField(default=None, null=True, verbose_name='published time', blank=True),
),
]
40 changes: 26 additions & 14 deletions dj_web_rich_object/models.py
Expand Up @@ -19,49 +19,61 @@


class WebRichObjectManager(models.Manager):
def create_from_url(self, url=None):
def create_from_url(self, url=None, **kwargs):
wro = web_rich_object.WebRichObject(url)
instance = self.create(title=wro.title,
type=wro.type,
image=wro.image,
url=wro.url,
base_url=url,
site_name=wro.site_name,
description=wro.description,
subtype=wro.subtype,
author=wro.author)
wro_attrs = {
'title': wro.title,
'type': wro.type,
'image': wro.image,
'url': wro.url,
'base_url': wro.base_url,
'site_name': wro.site_name,
'description': wro.description,
'subtype': wro.subtype,
'author': wro.author,
'created_time': wro.created_time,
'published_time': wro.published_time,
'modified_time': wro.modified_time,
}
wro_attrs.update(kwargs)
instance = self.create(**wro_attrs)
return instance

def create_or_update_from_url(self, url):
def create_or_update_from_url(self, url, **kwargs):
if self.filter(base_url=url).exists():
# TODO: Update not implemented
return self.filter(base_url=url).first()
else:
return self.create_from_url(url=url)
return self.create_from_url(url=url, **kwargs)


@python_2_unicode_compatible
class WebRichObject(models.Model):
title = models.CharField(max_length=300, verbose_name=_("title"))
type = models.CharField(max_length=30, verbose_name=_("type"))
subtype = models.CharField(max_length=30, verbose_name=_("subtype"))

image = models.URLField(null=True, blank=True, verbose_name=_("image"))
url = models.TextField(max_length=500, verbose_name=_("URL"))

base_url = models.TextField(max_length=500, verbose_name=_("Base URL"))

site_name = models.CharField(max_length=200, null=True, blank=True, verbose_name=_("site name"))
description = models.TextField(null=True, blank=True, default='', verbose_name=_("description"))
author = models.CharField(max_length=100, null=True, blank=True, default=None, verbose_name=_("author"))

created_time = models.DateTimeField(blank=True, null=True, default=None, verbose_name=_("created time"))
published_time = models.DateTimeField(blank=True, null=True, default=None, verbose_name=_("published time"))
modified_time = models.DateTimeField(blank=True, null=True, default=None, verbose_name=_("modified time"))

# Plus
create_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

is_valid = models.BooleanField(default=True)

objects = WebRichObjectManager()

class Meta:
app_label = 'dj_web_rich_object'
verbose_name = _("web rich object")
verbose_name_plural = _("web rich objects")

Expand Down

0 comments on commit 1c28c4a

Please sign in to comment.