Guest User

Untitled

a guest
Jan 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. from django.shortcuts import render, get_object_or_404
  2. from django.http import HttpResponse, HttpResponseRedirect
  3. from django.views import View
  4. from .models import URL
  5.  
  6.  
  7. # function based view
  8. def redirect_view(request, shortcode=None, *args, **kwargs):
  9. obj = get_object_or_404(URL, shortcode=shortcode)
  10. return HttpResponse("Hello World, the shortcode is {shortcode}".format(shortcode = obj.url))
  11.  
  12.  
  13. # class based view
  14. class ShortenerView(View):
  15.  
  16. def get(self, request, shortcode=None, *args, **kwargs):
  17. obj = get_object_or_404(URL, shortcode=shortcode)
  18. return HttpResponse("Hello World 1, the shortcode is {shortcode}".format(shortcode = obj.url))
  19.  
  20. def post(self, request, *args, **kwargs):
  21. return HttpResponse()
  22.  
  23. TypeError at /b/p6jzbp/
  24. super(type, obj): obj must be an instance or subtype of type
  25. Request Method: GET
  26. Request URL: http://127.0.0.1:8000/b/p6jzbp/
  27. Django Version: 1.11
  28. Exception Type: TypeError
  29. Exception Value:
  30. super(type, obj): obj must be an instance or subtype of type
  31. Exception Location: /Users/Chaklader/Documents/Projects/UrlShortener/src/shortener/models.py in all, line 18
  32.  
  33. # will look for the "SHORTCODE_MAX" in the settings and
  34. # if not found, will put the value of 15 there
  35. SHORTCODE_MAX = getattr(settings, "SHORTCODE_MAX", 15)
  36.  
  37.  
  38.  
  39. class UrlManager(models.Manager):
  40.  
  41. def all(self, *args, **kwargs):
  42. qs_main = super(URL, self).all(*args, **kwargs)
  43. qs = qs_main.filter(active = True)
  44. return qs
  45.  
  46. def refresh_shortcodes(self, items = None):
  47.  
  48. qs = URL.objects.filter(id__gte=1)
  49. new_codes = 0
  50.  
  51. if items is not None and isinstance(items, int):
  52. qs = qs.order_by('-id')[:items]
  53.  
  54. for q in qs:
  55. q.shortcode = create_shortcode(q)
  56. print (q.id, " ", q.shortcode)
  57. q.save()
  58. new_codes += 1
  59.  
  60. return "# new codes created {id}".format(id = new_codes)
  61.  
  62.  
  63. class URL(models.Model):
  64.  
  65. url = models.CharField(max_length = 220, )
  66. shortcode = models.CharField(max_length = SHORTCODE_MAX, blank = True, unique = True)
  67. updated = models.DateTimeField(auto_now = True)
  68. timestamp = models.DateTimeField(auto_now_add = True)
  69. active = models.BooleanField(default = True)
  70.  
  71. objects = UrlManager()
  72.  
  73. def save(self, *args, **kwargs):
  74.  
  75. if self.shortcode is None or self.shortcode == "":
  76. self.shortcode = create_shortcode(self)
  77.  
  78. super(URL, self).save(*args, **kwargs)
  79.  
  80. def __str__(self):
  81. return str(self.url)
  82.  
  83. def __unicode__(self):
  84. return str(self.url)
  85.  
  86. # class Meta:
  87. # ordering = '-id'
  88.  
  89. >>> class D:
  90. ... pass
  91. ...
  92. >>> class C:
  93. ... def __init__(self):
  94. ... super(D, self).__init__()
  95. ...
  96. >>> C()
  97. Traceback (most recent call last):
  98. File "<stdin>", line 1, in <module>
  99. File "<stdin>", line 3, in __init__
  100. TypeError: super(type, obj): obj must be an instance or subtype of type
  101.  
  102. qs_main = super(UrlManager, self).all(*args, **kwargs)
  103.  
  104. qs_main = super().all(*args, **kwargs)
  105.  
  106. class A(Foo):
  107. def __init__(self):
  108. super(A, self).__init__()
  109. #...
  110.  
  111. class A(Foo):
  112. def __init__(self):
  113. super(A, self).__init__()
  114. #...
Add Comment
Please, Sign In to add comment