

import datetime
from rapidsms.apps.base import AppBase
from .models import Message


class App(AppBase):
    def _who(self, msg):
        to_return = {}
        if msg.contact:    to_return["contact"]    = msg.contact 
        if msg.connection: to_return["connection"] = msg.connection 
        if not to_return:
            raise ValueError
        return to_return

    def _log(self, direction, who, text):
        return Message.objects.create(
            date=datetime.datetime.now(),
            direction=direction,
            text=text,
            **who)

    def parse(self, msg):
        # annotate the message as we log them in case any other apps
        # want a handle to them
        msg.logger_msg = self._log("I", self._who(msg), msg.raw_text)

    def outgoing(self, msg): 
        msg.logger_msg = self._log("O", self._who(msg), msg.text)



import datetime
from rapidsms.apps.base import AppBase
from .models import Message


try:
    from django.utils.timezone import now as datetime_now
except ImportError:
    datetime_now = datetime.datetime.now


class App(AppBase):
    def _who(self, msg):
        to_return = {}
        if msg.contact:    to_return["contact"]    = msg.contact 
        if msg.connection: to_return["connection"] = msg.connection 
        if not to_return:
            raise ValueError
        return to_return

    def _log(self, direction, who, text):
        return Message.objects.create(
            date=datetime_now(),
            direction=direction,
            text=text,
            **who)

    def parse(self, msg):
        # annotate the message as we log them in case any other apps
        # want a handle to them
        msg.logger_msg = self._log("I", self._who(msg), msg.raw_text)

    def outgoing(self, msg): 
        msg.logger_msg = self._log("O", self._who(msg), msg.text)

Use new timezone-aware datetime.now with Django versions that support it
Use new timezone-aware datetime.now with Django versions that support it

  
  
  import datetime
  from rapidsms.apps.base import AppBase
  from .models import Message
+ 
+ 
+ try:
+     from django.utils.timezone import now as datetime_now
+ except ImportError:
+     datetime_now = datetime.datetime.now
  
  
  class App(AppBase):
      def _who(self, msg):
          to_return = {}
          if msg.contact:    to_return["contact"]    = msg.contact 
          if msg.connection: to_return["connection"] = msg.connection 
          if not to_return:
              raise ValueError
          return to_return
  
      def _log(self, direction, who, text):
          return Message.objects.create(
-             date=datetime.datetime.now(),
+             date=datetime_now(),
              direction=direction,
              text=text,
              **who)
  
      def parse(self, msg):
          # annotate the message as we log them in case any other apps
          # want a handle to them
          msg.logger_msg = self._log("I", self._who(msg), msg.raw_text)
  
      def outgoing(self, msg): 
          msg.logger_msg = self._log("O", self._who(msg), msg.text)
  
Use new timezone-aware datetime.now with Django versions that support it
## Code Before:


import datetime
from rapidsms.apps.base import AppBase
from .models import Message


class App(AppBase):
    def _who(self, msg):
        to_return = {}
        if msg.contact:    to_return["contact"]    = msg.contact 
        if msg.connection: to_return["connection"] = msg.connection 
        if not to_return:
            raise ValueError
        return to_return

    def _log(self, direction, who, text):
        return Message.objects.create(
            date=datetime.datetime.now(),
            direction=direction,
            text=text,
            **who)

    def parse(self, msg):
        # annotate the message as we log them in case any other apps
        # want a handle to them
        msg.logger_msg = self._log("I", self._who(msg), msg.raw_text)

    def outgoing(self, msg): 
        msg.logger_msg = self._log("O", self._who(msg), msg.text)

## Instruction:
Use new timezone-aware datetime.now with Django versions that support it
## Code After:


import datetime
from rapidsms.apps.base import AppBase
from .models import Message


try:
    from django.utils.timezone import now as datetime_now
except ImportError:
    datetime_now = datetime.datetime.now


class App(AppBase):
    def _who(self, msg):
        to_return = {}
        if msg.contact:    to_return["contact"]    = msg.contact 
        if msg.connection: to_return["connection"] = msg.connection 
        if not to_return:
            raise ValueError
        return to_return

    def _log(self, direction, who, text):
        return Message.objects.create(
            date=datetime_now(),
            direction=direction,
            text=text,
            **who)

    def parse(self, msg):
        # annotate the message as we log them in case any other apps
        # want a handle to them
        msg.logger_msg = self._log("I", self._who(msg), msg.raw_text)

    def outgoing(self, msg): 
        msg.logger_msg = self._log("O", self._who(msg), msg.text)
