Centarra/panel2 — pull request #39 by nasonfish (2014-06-16 07:32:04+00:00)
Title: support: allow admins to manage ticket statuses that can be used

Here's a fun merge!

Assuming this is puffrfish merging..:

First, pull the files in with git, and hit `./create-all.py`

```git
cd ~/panel2/ # or wherever
git pull git@github.com:Centarra/panel2.git master
python create-all.py
```
Now, you're going to need to go into the ` panel2/support/models.py` file, and edit line 37:
```python
# comment out this line by prepending it with '#'
# status = db.relationship('TicketStatus', backref='tickets')
# and add:
status = db.Column(db.String(255))
```

Now, create a new file in the base panel2 directory, with these contents:

```python
from panel2 import db
from panel2.support.models import Ticket, TicketStatus
cl = TicketStatus("Awaiting Client Reply", cls="warning")
st = TicketStatus("Awaiting Staff Reply", cls="warning", staff_reply=cl, default=True)
cl.client_reply_id = st.id
cl.client_reply = st
db.session.add(cl)
db.session.commit()

for i in Ticket.query:
if i.is_open is False:
st = cl
else:
st = TicketStatus.query.filter_by(name=i.status).first()
if not st:
st = TicketStatus(i.status, cls="warning")
print("UPDATE ticket SET status_id={} WHERE id={};".format(st.id, i.id))

print("ALTER TABLE ticket DROP COLUMN status;")
```

This file should print out a list of SQL commands to execute (this is kind of dumb because I can't really update the SQL command with the status column filled with the string, so it spits out SQL :| )

Execute the SQL statements printed, then, revert your change to the `models.py` file.

```python
status = db.relationship('TicketStatus', backref='tickets')
# and no line with status = db.Column(db.String(255))
```

Then, you should be done converting.

This seems really dumb overall and some column changes might have been easier, but I've used this multiple times and it's converted the db over.

Sorry. :|