Guest User

Untitled

a guest
Aug 6th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. Request Method: POST
  2. Request URL: http://jqlc3gdp.apps.lair.io/login/set_alarm/
  3. Django Version: 2.0
  4. Exception Type: AttributeError
  5. Exception Value:
  6. 'WSGIRequest' object has no attribute 'username'
  7. Exception Location: /mnt/project/weather_alarm/views.py in get_user_timezone, line 94
  8.  
  9. class User(models.Model):
  10. """ Model representing each User """
  11. username = models.CharField(max_length=20, unique=True)
  12. password = models.CharField(max_length=30, validators=[MinLengthValidator(8)])
  13. email = models.EmailField(max_length=254)
  14. first_name = models.CharField(max_length=20)
  15. last_name = models.CharField(max_length=20)
  16.  
  17.  
  18. class Alarm(models.Model):
  19. """ Model representing each Alarm """
  20. alarm_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  21. username = models.ForeignKey(User, on_delete=models.CASCADE, null=True) # null=True is TEMP
  22. timezone = models.CharField(max_length=30)
  23. city = models.CharField(max_length=30)
  24. country = models.CharField(max_length=30)
  25. time = models.DateTimeField()
  26. temp_conditional = models.BooleanField()
  27. surf_conditional = models.BooleanField()
  28. temp_max = models.FloatField(blank=True, null=True)
  29. temp_min = models.FloatField(blank=True, null=True)
  30. surf_max = models.FloatField(blank=True, null=True)
  31. surf_min = models.FloatField(blank=True, null=True)
  32.  
  33. class SetAlarmForm(ModelForm):
  34. ...
  35. class Meta:
  36. model = Alarm
  37. exclude = ['username', 'timezone', 'city', 'country']
  38.  
  39. class AlarmCreateView(LoginRequiredMixin, CreateView):
  40. model = Alarm
  41. form_class = SetAlarmForm
  42. template_name = 'weather_alarm/set_alarm.html'
  43. success_url = reverse_lazy('weather_alarm:confirm-alarm')
  44. login_url = "/login/"
  45.  
  46. def form_valid(self, form):
  47. self.get_user_location(form)
  48. return super().form_valid(form)
  49.  
  50. def get_user_location(self, form):
  51. """ Function to get User's location from IP and create Alarm object """
  52. ...
  53. alarm_object = Alarm.objects.create(
  54. alarm_id=uuid.uuid4(),
  55. username=User.objects.get(username=self.request.username),
  56. timezone=user_timezone,
  57. city=location.raw['address']['city'],
  58. country=location.raw['address']['country'],
  59. time=form.cleaned_data['time'].astimezone(pytz.timezone(user_timezone)),
  60. temp_conditional=form.cleaned_data['temp_conditional'],
  61. surf_conditional=form.cleaned_data['surf_conditional'],
  62. temp_max=form.cleaned_data['temp_max'],
  63. temp_min=form.cleaned_data['temp_min'],
  64. surf_max=form.cleaned_data['surf_max'],
  65. surf_min=form.cleaned_data['surf_min'],
  66. )
  67. alarm_object.save()
Add Comment
Please, Sign In to add comment