# Path: _15thnight/util.py
# def extend(dict1, dict2):
#     dict1.update(dict2)
#     return dict1
# 
# def to_local_datetime(dt):
#     """
#     datetime.isoformat does not append +0000 when using UTC, javascript
#     needs it, or the date is parsed as if it were in the local timezone
#     """
#     if not dt:
#         return None
#     ldt = dt.isoformat()
#     return ldt if ldt[-6] == "+" else "%s+0000" % ldt


import uuid
from datetime import datetime, timedelta
from flask import url_for
from sqlalchemy import (
    Column, DateTime, Enum, ForeignKey, Integer, String, Table, Text, desc,
    Boolean
)
from sqlalchemy.orm import backref, relationship
from werkzeug.security import check_password_hash, generate_password_hash
from _15thnight.database import Model
from _15thnight.util import extend, to_local_datetime


class NeedProvided(Model):
    """A need provided in a response."""

    __tablename__ = 'need_provided'
    id = Column(Integer, primary_key=True)
    need_id = Column(ForeignKey('need.id'))
    need = relationship('Need', backref='provisions')
    response_id = Column(ForeignKey('responses.id'))
    response = relationship('Response', backref='needs_provided')
    message = Column(Text, nullable=False, default='')
    selected = Column(Boolean)

    @classmethod
    def get_by_need_and_provider(cls, need, provider):
        return cls.query \
            .filter(cls.need == need) \
            .join(cls.response) \
            .filter(Response.user == provider) \
            .all()

    def to_json(self):
        return dict(
            id=self.id,
            created_at=to_local_datetime(self.response.created_at),
            message=self.message
        )

    def to_advocate_json(self):

# Path: _15thnight/util.py
# def extend(dict1, dict2):
#     dict1.update(dict2)
#     return dict1
# 
# def to_local_datetime(dt):
#     """
#     datetime.isoformat does not append +0000 when using UTC, javascript
#     needs it, or the date is parsed as if it were in the local timezone
#     """
#     if not dt:
#         return None
#     ldt = dt.isoformat()
#     return ldt if ldt[-6] == "+" else "%s+0000" % ldt


# Path: _15thnight/models.py
import uuid
from datetime import datetime, timedelta
from flask import url_for
from sqlalchemy import (
    Column, DateTime, Enum, ForeignKey, Integer, String, Table, Text, desc,
    Boolean
)
from sqlalchemy.orm import backref, relationship
from werkzeug.security import check_password_hash, generate_password_hash
from _15thnight.database import Model
from _15thnight.util import extend, to_local_datetime



class NeedProvided(Model):
    """A need provided in a response."""

    __tablename__ = 'need_provided'
    id = Column(Integer, primary_key=True)
    need_id = Column(ForeignKey('need.id'))
    need = relationship('Need', backref='provisions')
    response_id = Column(ForeignKey('responses.id'))
    response = relationship('Response', backref='needs_provided')
    message = Column(Text, nullable=False, default='')
    selected = Column(Boolean)

    @classmethod
    def get_by_need_and_provider(cls, need, provider):
        return cls.query \
            .filter(cls.need == need) \
            .join(cls.response) \
            .filter(Response.user == provider) \
            .all()

    def to_json(self):
        return dict(
            id=self.id,
            created_at=to_local_datetime(self.response.created_at),
            message=self.message
        )

    def to_advocate_json(self):

        return extend(self.to_json(), dict(