# Path: letsmeet/communities/models.py
# class Community(TimeStampedModel):
#     name = models.CharField(max_length=64, unique=True)
#     slug = models.SlugField(max_length=64, unique=True)
#     description = models.TextField(null=True, blank=True, help_text='You can write markown here!')
#     subscribers = models.ManyToManyField('auth.User', through='CommunitySubscription', related_name='communities')
#     cname = models.CharField(
#         max_length=255, null=True, blank=True, verbose_name="CNAME",
#         validators=[RegexValidator(r'[a-zA-Z0-9-\.\ ]+')],
#         help_text="use your own domain to redirect to this community."
#     )
# 
#     twitter = models.CharField(max_length=128, blank=True, null=True,
#                                help_text="Twitter username (without leading @)",
#                                validators=[RegexValidator(r'[a-zA-Z0-9_]+')],)
#     github = models.CharField(max_length=128, blank=True, null=True,
#                               help_text="GitHub username or organisation name")
#     homepage = models.URLField(max_length=128, blank=True, null=True,
#                                help_text="URL of homepage (including http://)")
#     irc_channel = models.CharField(
#         max_length=128, blank=True, null=True, verbose_name="IRC channel",
#         help_text='IRC channel name',
#     )
#     irc_network = models.CharField(
#         max_length=128, blank=True, null=True,
#         help_text='Network the IRC channel is located on (e.g. "Freenode")',
#         verbose_name='IRC network',
#     )
#     slack = models.CharField(max_length=128, blank=True, null=True,
#                              help_text="Slack organisation name")
# 
#     is_deleted = models.BooleanField(default=False)
# 
#     objects = CommunityManager()
#     default = models.Manager()   # the default manager
# 
#     def __str__(self):
#         return self.name
# 
#     def save(self, *args, **kwargs):
#         if not self.id:
#             self.slug = slugify(self.name)
# 
#         super().save(*args, **kwargs)
# 
#     def subscribe_user(self, user):
#         subscription, created = CommunitySubscription.objects.get_or_create(
#             user=user, community=self,
#         )
#         if created:
#             recipients = User.objects.filter(pk__in=self.community_subscriptions.filter(
#                 role__in=[CommunitySubscription.ROLE_ADMIN, CommunitySubscription.ROLE_OWNER],
#                 user__userprofile__notify_on_new_subscription=True,
#             ).values_list('user__pk', flat=True))
#             # send notification mail to all subscribers
#             if recipients:
#                 from main.utils import send_notification
#                 send_notification(
#                     recipients=recipients,
#                     subject='New subscription to community {}'.format(self.name),
#                     template='communities/mails/new_subscription.txt',
#                     context={'subscription': subscription},
#                 )
# 
#         return subscription
# 
#     def get_user_subscription(self, user):
#         """returns the CommunitySubscription object of the user. None if user is not subscribed"""
#         return self.community_subscriptions.filter(user=user).first()
# 
#     def get_next_event(self):
#         if not hasattr(self, '_next_event'):
#             self._next_event = self.events.filter(begin__gte=timezone.now()).order_by('begin').first()
# 
#         return self._next_event
# 
#     def get_ical_url(self):
#         return reverse('community_events_ical_feed', kwargs={'community_slug': self.slug})
# 
#     def get_json_feed_url(self):
#         return reverse('community_events_json_feed', kwargs={'community_slug': self.slug})
# 
#     def get_absolute_url(self):
#         return reverse('community_detail', kwargs={'slug': self.slug})
# 
#     def get_update_url(self):
#         return reverse('community_update', kwargs={'slug': self.slug})
# 
#     def get_subscribe_url(self):
#         return reverse('community_subscribe', kwargs={'slug': self.slug})
# 
#     def get_unsubscribe_url(self):
#         return reverse('community_unsubscribe', kwargs={'slug': self.slug})
# 
#     def get_event_create_url(self):
#         return reverse('community_event_create', kwargs={'slug': self.slug})
# 
#     class Meta:
#         ordering = ['slug']
#         verbose_name_plural = 'Communities'


from django import forms
from django.template.defaultfilters import slugify
from .models import Community



class CommunityUpdateForm(forms.ModelForm):
    class Meta:
        model = Community
        fields = [
            'name', 'slug', 'description', 'cname', 'twitter', 'github', 'homepage', 'irc_channel', 'irc_network',
            'slack', ]

    def clean_slug(self):
        slug = slugify(self.cleaned_data['slug'])

# Path: letsmeet/communities/models.py
# class Community(TimeStampedModel):
#     name = models.CharField(max_length=64, unique=True)
#     slug = models.SlugField(max_length=64, unique=True)
#     description = models.TextField(null=True, blank=True, help_text='You can write markown here!')
#     subscribers = models.ManyToManyField('auth.User', through='CommunitySubscription', related_name='communities')
#     cname = models.CharField(
#         max_length=255, null=True, blank=True, verbose_name="CNAME",
#         validators=[RegexValidator(r'[a-zA-Z0-9-\.\ ]+')],
#         help_text="use your own domain to redirect to this community."
#     )
# 
#     twitter = models.CharField(max_length=128, blank=True, null=True,
#                                help_text="Twitter username (without leading @)",
#                                validators=[RegexValidator(r'[a-zA-Z0-9_]+')],)
#     github = models.CharField(max_length=128, blank=True, null=True,
#                               help_text="GitHub username or organisation name")
#     homepage = models.URLField(max_length=128, blank=True, null=True,
#                                help_text="URL of homepage (including http://)")
#     irc_channel = models.CharField(
#         max_length=128, blank=True, null=True, verbose_name="IRC channel",
#         help_text='IRC channel name',
#     )
#     irc_network = models.CharField(
#         max_length=128, blank=True, null=True,
#         help_text='Network the IRC channel is located on (e.g. "Freenode")',
#         verbose_name='IRC network',
#     )
#     slack = models.CharField(max_length=128, blank=True, null=True,
#                              help_text="Slack organisation name")
# 
#     is_deleted = models.BooleanField(default=False)
# 
#     objects = CommunityManager()
#     default = models.Manager()   # the default manager
# 
#     def __str__(self):
#         return self.name
# 
#     def save(self, *args, **kwargs):
#         if not self.id:
#             self.slug = slugify(self.name)
# 
#         super().save(*args, **kwargs)
# 
#     def subscribe_user(self, user):
#         subscription, created = CommunitySubscription.objects.get_or_create(
#             user=user, community=self,
#         )
#         if created:
#             recipients = User.objects.filter(pk__in=self.community_subscriptions.filter(
#                 role__in=[CommunitySubscription.ROLE_ADMIN, CommunitySubscription.ROLE_OWNER],
#                 user__userprofile__notify_on_new_subscription=True,
#             ).values_list('user__pk', flat=True))
#             # send notification mail to all subscribers
#             if recipients:
#                 from main.utils import send_notification
#                 send_notification(
#                     recipients=recipients,
#                     subject='New subscription to community {}'.format(self.name),
#                     template='communities/mails/new_subscription.txt',
#                     context={'subscription': subscription},
#                 )
# 
#         return subscription
# 
#     def get_user_subscription(self, user):
#         """returns the CommunitySubscription object of the user. None if user is not subscribed"""
#         return self.community_subscriptions.filter(user=user).first()
# 
#     def get_next_event(self):
#         if not hasattr(self, '_next_event'):
#             self._next_event = self.events.filter(begin__gte=timezone.now()).order_by('begin').first()
# 
#         return self._next_event
# 
#     def get_ical_url(self):
#         return reverse('community_events_ical_feed', kwargs={'community_slug': self.slug})
# 
#     def get_json_feed_url(self):
#         return reverse('community_events_json_feed', kwargs={'community_slug': self.slug})
# 
#     def get_absolute_url(self):
#         return reverse('community_detail', kwargs={'slug': self.slug})
# 
#     def get_update_url(self):
#         return reverse('community_update', kwargs={'slug': self.slug})
# 
#     def get_subscribe_url(self):
#         return reverse('community_subscribe', kwargs={'slug': self.slug})
# 
#     def get_unsubscribe_url(self):
#         return reverse('community_unsubscribe', kwargs={'slug': self.slug})
# 
#     def get_event_create_url(self):
#         return reverse('community_event_create', kwargs={'slug': self.slug})
# 
#     class Meta:
#         ordering = ['slug']
#         verbose_name_plural = 'Communities'


# Path: letsmeet/communities/forms.py
from django import forms
from django.template.defaultfilters import slugify
from .models import Community




class CommunityUpdateForm(forms.ModelForm):
    class Meta:
        model = Community
        fields = [
            'name', 'slug', 'description', 'cname', 'twitter', 'github', 'homepage', 'irc_channel', 'irc_network',
            'slack', ]

    def clean_slug(self):
        slug = slugify(self.cleaned_data['slug'])

        if Community.objects.exclude(pk=self.instance.pk).filter(slug=slug):