def delete_channel(current): """ Delete a channel .. code-block:: python # request: { 'view':'_zops_delete_channel, 'channel_key': key, } # response: { 'status': 'OK', 'code': 200 } """ ch_key = current.input['channel_key'] ch = Channel(current).objects.get(owner_id=current.user_id, key=ch_key) ch.delete() Subscriber.objects.filter(channel_id=ch_key).delete() Message.objects.filter(channel_id=ch_key).delete() current.output = {'status': 'Deleted', 'code': 200}
def show_channel(current, waited=False): """ Initial display of channel content. Returns channel description, members, no of members, last 20 messages etc. .. code-block:: python # request: { 'view':'_zops_show_channel', 'key': key, } # response: { 'channel_key': key, 'description': string, 'no_of_members': int, 'member_list': [ {'name': string, 'is_online': bool, 'avatar_url': string, }], 'name': string, 'last_messages': [MSG_DICT] 'status': 'OK', 'code': 200 } """ ch = Channel(current).objects.get(current.input['key']) sbs = ch.get_subscription_for_user(current.user_id) current.output = {'key': current.input['key'], 'description': ch.description, 'name': sbs.name, 'actions': sbs.get_actions(), 'avatar_url': ch.get_avatar(current.user), 'no_of_members': len(ch.subscriber_set), 'member_list': [{'name': sb.user.full_name, 'is_online': sb.user.is_online(), 'avatar_url': sb.user.get_avatar_url() } for sb in ch.subscriber_set.objects.all()], 'last_messages': [], 'status': 'OK', 'code': 200 } for msg in ch.get_last_messages(): current.output['last_messages'].insert(0, msg.serialize(current.user))
def sube_kanal_olustur(self): """Yeni bir şube oluşturulduğunda mesajlaşma kanalı oluşturur ve o şubenin öğretim görevlisini o kanala yönetici olarak atar.""" channel = Channel() channel.name = "%s %s" % (self.ders.kod, self.ad) channel.typ = 15 # public kanal channel.description = "%s Dersi %s Şubesi Mesajlaşma Kanalı" % (self.ders.ad, self.ad) channel.owner = self.okutman.personel.user channel.save()
def edit_channel(current): """ Update channel name or description .. code-block:: python # request: { 'view':'_zops_edit_channel, 'channel_key': key, 'name': string, 'description': string, } # response: { 'status': 'OK', 'code': 200 } """ ch = Channel(current).objects.get(owner_id=current.user_id, key=current.input['channel_key']) ch.name = current.input['name'] ch.description = current.input['description'] ch.save() for sbs in ch.subscriber_set.objects.all(): sbs.name = ch.name sbs.save() current.output = {'status': 'OK', 'code': 200}
def create_channel(current): """ Create a public channel. Can be a broadcast channel or normal chat room. Chat room and broadcast distinction will be made at user subscription phase. .. code-block:: python # request: { 'view':'_zops_create_channel', 'name': string, 'description': string, } # response: { 'description': string, 'name': string, 'no_of_members': int, 'member_list': [ {'name': string, 'is_online': bool, 'avatar_url': string, }], 'last_messages': [MSG_DICT] 'status': 'Created', 'code': 201, 'key': key, # of just created channel } """ channel = Channel(name=current.input['name'], description=current.input['description'], owner=current.user, typ=15).save() with BlockSave(Subscriber): Subscriber.objects.get_or_create(user=channel.owner, channel=channel, can_manage=True, can_leave=False) current.input['key'] = channel.key show_channel(current) current.output.update({ 'status': 'Created', 'code': 201 })
def create_test_data(): # Channels, subscribers and messages are created for test environment. ch = sb = msg = [] a = [u for u in User.objects.all() if u.username != None] for i in range(5): c = Channel(name="%i Class" % random.randrange(1000, 9000), owner=random.choice(a), typ=15).save() ch.append(c.key) for i in range(2): u = random.choice(a) s = Subscriber(channel=c, typ=15, name=u.username, user=u).save() sb.append(s.key) for i in range(2): m = Message(channel=c, typ=15, sender=random.choice(a), receiver=random.choice(a), msg_title=str(random.randrange(1, 1000)), body=str(random.randrange(1, 1000))).save() msg.append(m.key) return ch, sb, msg
def create_message(current): """ Creates a message for the given channel. .. code-block:: python # request: { 'view':'_zops_create_message', 'message': { 'channel': key, # of channel 'body': string, # message text., 'type': int, # zengine.messaging.model.MSG_TYPES, 'attachments': [{ 'description': string, # can be blank, 'name': string, # file name with extension, 'content': string, # base64 encoded file content }]} # response: { 'status': 'Created', 'code': 201, 'msg_key': key, # key of the message object, } """ msg = current.input['message'] msg_obj = Channel.add_message(msg['channel'], body=msg['body'], typ=msg['type'], sender=current.user, title=msg['title'], receiver=msg['receiver'] or None) current.output = { 'msg_key': msg_obj.key, 'status': 'Created', 'code': 201 } if 'attachment' in msg: for atch in msg['attachments']: typ = current._dedect_file_type(atch['name'], atch['content']) Attachment(channel_id=msg['channel'], msg=msg_obj, name=atch['name'], file=atch['content'], description=atch['description'], typ=typ).save()
def create_direct_channel(current): """ Create a One-To-One channel between current and selected user. .. code-block:: python # request: { 'view':'_zops_create_direct_channel', 'user_key': key, } # response: { 'description': string, 'no_of_members': int, 'member_list': [ {'name': string, 'is_online': bool, 'avatar_url': string, }], 'last_messages': [MSG_DICT] 'status': 'Created', 'code': 201, 'channel_key': key, # of just created channel 'name': string, # name of subscribed channel } """ channel, sub_name = Channel.get_or_create_direct_channel(current.user_id, current.input['user_key']) current.input['key'] = channel.key show_channel(current) current.output.update({ 'status': 'Created', 'code': 201 })